How to use IPTABLES

To Drop IP in iptables

Port Specific:

iptables -A INPUT -p tcp -s xx.xx.xx.xx –dport {PORT} -j DROP
iptables -A INPUT -p tcp -s xx.xx.xx.xx –dport 80 -j DROP
/sbin/service iptables save

No Port Specified:

iptables -A INPUT -s xx.xx.xx.xx -j DROP
/sbin/service iptables save

Example:

/sbin/iptables -I INPUT -s {IP-HERE} -j DROP
/sbin/iptables -I INPUT -s 1.2.3.4 -j DROP
/sbin/service iptables save

To Allow IP in iptables

iptables -A INPUT -p tcp -s xx.xx.xx.xx –dport {PORT} -j DROP
iptables -A INPUT -s xx.xx.xx.xx -j ACCEPT
/sbin/service iptables save


Unblock / Delete an IP Address Listed in IPtables Tables

– To display line number along with other information, type the command below.

iptables -L INPUT -n –line-numbers
iptables -L OUTPUT -n –line-numbers

Search for the IP Address that you want to delete. Example 192.168.2.4

iptables -L INPUT -n –line-numbers | grep “192.168.2.4”
12056 DROP all — 192.168.2.4 0.0.0.0/0

2. IP address 192.168.2.4 is in line 12056 so we will use the command below to delete line 12056.

iptables -D INPUT 12056
/sbin/service iptables save

Other way to remove the IP is like this one.

iptables -D INPUT -s xx.xxx.xx.xx -j DROP
/sbin/service iptables save

Leave a Reply