How to block only one IP using iptables?


 
Thread Tools Search this Thread
Operating Systems Linux How to block only one IP using iptables?
# 1  
Old 10-21-2015
How to block only one IP using iptables?

Hi friends,

I have a linux machine without iptables running and we have a new requirement to block a remote machine ( IP = 172.1.1.1 ) completely accessing our linux machine in both directions. So I need to allow "everything" except that IP address. So i tried below:

If I set the below in /etc/sysconfig/iptables file and do service iptables restart then everything is allowed (included the blocked IP)

Code:
# Default IPtables config
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [26:8868]

-A INPUT -j ACCEPT
-A OUTPUT -j ACCEPT

-A INPUT --src 172.1.1.1 -j REJECT
-A OUTPUT --dst 172.1.1.1 -j REJECT

COMMIT

If I try the below then it blocks everything

Code:
# Default IPtables config
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [26:8868]

-A INPUT --src 172.1.1.1 -j REJECT
-A OUTPUT --dst 172.1.1.1 -j REJECT

-A INPUT -j ACCEPT
-A OUTPUT -j ACCEPT


COMMIT

I'm new to iptables and not sure what am I doing wrong, appreciate if any expert could help me out here please Smilie
# 2  
Old 10-21-2015
your 3rd,4th & 5th lines are culprits. Try this:

Code:
# Default IPtables config
*filter

-A INPUT --src 172.1.1.1 -j REJECT
-A OUTPUT --dst 172.1.1.1 -j REJECT

-A INPUT -j ACCEPT
-A OUTPUT -j ACCEPT

COMMIT

This User Gave Thanks to prvnrk For This Post:
# 3  
Old 10-21-2015
Thanks, that seems to have solved my prob but i need more tests to do to ensure. Is there any tool or software or anything that can help me setup iptables easily? somehow I find iptables very uncomfortable Smilie
# 4  
Old 11-06-2015
How to block only one IP using iptables?

In order to block an IP on your Linux server you need to use iptables tools (administration tool for IPv4 packet filtering and NAT) and netfilter firewall. First you need to log into shell as root user. To block an IP address you need to type the iptables command as follows:


Syntax to block an IP address under Linux

Code:
iptables -A INPUT -s IP-ADDRESS -j DROP

Replace IP-ADDRESS with your actual IP address. For example, if you wish to block an ip address 65.55.44.100 for whatever reason then type the command as follows:
Code:
# iptables -A INPUT -s 65.55.44.100 -j DROP

If you have IP tables firewall script, add the above rule to your script.

If you just want to block access to one port from an ip 65.55.44.100 to port 25 then type command:
Code:
# iptables -A INPUT -s 65.55.44.100 -p tcp --destination-port 25 -j DROP

The above rule will drop all packets coming from IP 65.55.44.100 to port mail server port 25.

CentOS / RHEL / Fedora Block An IP And Save It To Config File

Type the following two command:
Code:
# iptables -A INPUT -s 65.55.44.100 -j DROP
# service iptables save


Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 11-06-2015 at 03:55 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Red Hat

Block an IP for a subnet by iptables

Hi I am looking to block an incomming IP for all the subnet in lab area. Only single Ip from Lab should be access to this incomming IP. Block IP=10.20.50.xx Subnet=10.30.40.xx ............................ ........................... Subnet=10.40.50.xx can anyone explain how to do... (2 Replies)
Discussion started by: boby.kumar
2 Replies

2. Cybersecurity

iptables: block/allow ftp

I have 2 LAN's, seperated by a firewall, running iptables on it. I want only allow ftp access from one to the other LAN. Server 1 in LAN 1 should have ftp access to Server 2 in LAN 2 Server 2 in LAN 2 should not have ftp access to Server 1 in LAN 1. Can someone tell me how to set up the... (5 Replies)
Discussion started by: sTorm
5 Replies
Login or Register to Ask a Question