{"id":815,"date":"2013-10-19T01:31:36","date_gmt":"2013-10-18T23:31:36","guid":{"rendered":"http:\/\/www.shukko.com\/x3\/?p=815"},"modified":"2013-11-13T17:08:25","modified_gmt":"2013-11-13T15:08:25","slug":"linux-block-port-with-iptables","status":"publish","type":"post","link":"https:\/\/www.shukko.com\/x3\/2013\/10\/19\/linux-block-port-with-iptables\/","title":{"rendered":"Linux: Block Port With IPtables"},"content":{"rendered":"<p><a title=\"http:\/\/www.cyberciti.biz\/faq\/iptables-block-port\/\" href=\"http:\/\/www.cyberciti.biz\/faq\/iptables-block-port\/\" target=\"_blank\">http:\/\/www.cyberciti.biz\/faq\/iptables-block-port\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Block Incoming Request From IP 1.2.3.4<\/h2>\n<p>The following command will drop any packet coming from the IP address 1.2.3.4:<\/p>\n<pre>\u00a0\r\n\/sbin\/iptables -I INPUT -s {IP-HERE} -j DROP\r\n\/sbin\/iptables -I INPUT -s 1.2.3.4 -j DROP<\/pre>\n<p>You can also specify an interface such as eth1 via which a packet was received:<\/p>\n<pre>\u00a0\r\n\/sbin\/iptables -I INPUT -i {INTERFACE-NAME-HERE} -s {IP-HERE} -j DROP\r\n\/sbin\/iptables -I INPUT -i eth1 -s 1.2.3.4 -j DROP<\/pre>\n<p>Please note that when the &#8220;!&#8221; argument is used before the interface name, the sense is inverted:<\/p>\n<pre>\u00a0\r\n\/sbin\/iptables -I INPUT ! -i {INTERFACE-NAME-HERE} -s {IP-HERE} -j DROP\r\n\/sbin\/iptables -I INPUT ! -i eth1 -s 1.2.3.4 -j DROP<\/pre>\n<p>If the interface name ends in a &#8220;+&#8221;, then any interface which begins with this name will match. If this option is omitted, any interface name will match:<\/p>\n<pre>\u00a0\r\n\/sbin\/iptables -I INPUT  -i {INTERFACE-NAME-HERE}+ -s {IP-HERE} -j DROP\r\n\/sbin\/iptables -I INPUT  -i br+ -s 1.2.3.4 -j DROP<\/pre>\n<p>You can replace -I INPUT (insert) with -A INPUT (append) rule as follows:<\/p>\n<pre>\u00a0\r\n\/sbin\/iptables -A INPUT  -s 1.2.3.4 -j DROP\r\n\/sbin\/iptables -i eth1 -A INPUT  -s 1.2.3.4 -j DROP<\/pre>\n<h3>How Do I Block Subnet (xx.yy.zz.ww\/ss)?<\/h3>\n<p>Use the following syntax to block 10.0.0.0\/8 on eth1 public interface:<br \/>\n<code># \/sbin\/iptables -i eth1 -A INPUT -s 10.0.0.0\/8 -j DROP<\/code><\/p>\n<h3>How Do I Block and Log Dropped IP Address Information?<\/h3>\n<p>You can turn on kernel logging of matching packets with LOG target as follows:<br \/>\n<code># \/sbin\/iptables -i eth1 -A INPUT -s 10.0.0.0\/8 -j LOG --log-prefix \"IP DROP SPOOF A:\"<\/code><br \/>\nThe next rule will actually drop the ip \/ subnet:<br \/>\n<code># \/sbin\/iptables -i eth1 -A INPUT -s 10.0.0.0\/8 -j DROP<\/code><\/p>\n<h3>How Do I View Blocked IP Address?<\/h3>\n<p>Simply use the following command:<br \/>\n<code># \/sbin\/iptables -L -v<\/code><br \/>\nOR<br \/>\n<code># \/sbin\/iptables -L INPUT -v<\/code><br \/>\nOR<br \/>\n<code># \/sbin\/iptables -L INPUT -v -n<\/code><br \/>\nSample outputs:<\/p>\n<pre>Chain INPUT (policy ACCEPT 3107K packets, 1847M bytes)\r\n pkts bytes target     prot opt in     out     source               destination\r\n    0     0 DROP       all  --  br+    any     1.2.3.4              anywhere\r\n    0     0 DROP       all  --  !eth1  any     1.2.3.4              anywhere\r\n    0     0 DROP       all  --  !eth1  any     1.2.3.4              anywhere<\/pre>\n<h3>How Do I Search For Blocked IP Address?<\/h3>\n<p>Use the <a href=\"http:\/\/www.cyberciti.biz\/faq\/howto-use-grep-command-in-linux-unix\/\">grep command<\/a> as follows:<br \/>\n<code># \/sbin\/iptables -L INPUT -v -n | grep 1.2.3.4<\/code><\/p>\n<h3>How Do I Delete Blocked IP Address?<\/h3>\n<p>First, you <a title=\"Iptables: Unblock \/ Delete an IP Address Listed in IPtables Tables\" href=\"http:\/\/www.cyberciti.biz\/faq\/iptables-delete-ip-address-subnet-from-linux-firewall\/\">need to display blocked IP address along with line number<\/a> and other information, enter:<br \/>\n<code># iptables -L INPUT -n --line-numbers<br \/>\n# iptables -L INPUT -n --line-numbers | grep 1.2.3.4<\/code><br \/>\nSample outputs:<\/p>\n<pre>num   pkts bytes target     prot opt in     out     source               destination\r\n1        0     0 DROP       0    --  *      *       116.199.128.1        0.0.0.0\/0\r\n2        0     0 DROP       0    --  *      *       116.199.128.10       0.0.0.0\/0\r\n3        0     0 DROP       0    --  *      *       123.199.2.255        0.0.0.0\/0<\/pre>\n<p>To delete line number 3 (123.199.2.255), enter:<br \/>\n<code># iptables -D INPUT 3<\/code><br \/>\nVerify the same, enter:<br \/>\n<code># iptables -L INPUT -v -n<\/code><br \/>\nYou can also use the following syntax:<br \/>\n<code># iptables -D INPUT -s 1.2.3.4 -j DROP<\/code><\/p>\n<h2>How Do I Save Blocked IP Address?<\/h2>\n<p>If you are using Redhat \/ RHEL \/ CentOS \/ Fedora Linux, type the following command:<br \/>\n<code># iptables -D INPUT -s 1.2.3.4 -j DROP<br \/>\n##########################<br \/>\n#\/\/\/\/\/\/ command to save iptables \/\/\/\/\/\/\/#<br \/>\n##########################<br \/>\n# \/sbin\/service iptables save<br \/>\n# less \/etc\/sysconfig\/iptables<br \/>\n# grep '1.2.3.4' \/etc\/sysconfig\/iptables<\/code><br \/>\nFor all <strong>other Linux distributions<\/strong> use <a title=\"How do I save iptables rules or settings?\" href=\"http:\/\/www.cyberciti.biz\/faq\/how-do-i-save-iptables-rules-or-settings\/\">the iptables-save command to dump the contents of an IP Table<\/a> to a file:<br \/>\n<code># iptables-save &gt; \/root\/myfirewall.conf<\/code><br \/>\nPlease not that you need to run the &#8216;iptables-save&#8217; or &#8216;service iptables save&#8217; as soon as you add or delete the ip address.<\/p>\n<h4>A Note About Restoring Firewall<\/h4>\n<p>To restore your firewall use the <a href=\"http:\/\/www.cyberciti.biz\/faq\/how-to-save-restore-iptables-firewall-config-ubuntu\/\">iptables-restore command to restore IP Tables from a file called \/root\/myfirewall.conf<\/a>, enter:<br \/>\n<code># iptables-restore &lt; \/root\/myfirewall.conf<\/code><\/p>\n<h2>How Do I Block Large Number Of IP Address or Subnets?<\/h2>\n<p>You need to write a shell script as follows:<\/p>\n<pre>#!\/bin\/bash\r\n_input=\"\/root\/blocked.ip.db\"\r\nIPT=\/sbin\/iptables\r\n$IPT -N droplist\r\negrep -v \"^#|^$\" x | while IFS= read -r ip\r\ndo\r\n\t$IPT -A droplist -i eth1 -s $ip -j LOG --log-prefix \"IP BlockList \"\r\n\t$IPT -A droplist -i eth1 -s $ip -j DROP\r\ndone &lt; \"$_input\"\r\n# Drop it\r\n$IPT -I INPUT -j droplist\r\n$IPT -I OUTPUT -j droplist\r\n$IPT -I FORWARD -j droplist<\/pre>\n<p>See also: <a href=\"http:\/\/www.cyberciti.biz\/faq\/iptables-read-and-block-ips-subnets-from-text-file\/\">iptables: Read a List of IP Address From File And Block<\/a><\/p>\n<h2>Block Outgoing Request From LAN IP 192.168.1.200?<\/h2>\n<p>Use the following syntax:<br \/>\n<code># \/sbin\/iptables -A OUTPUT -s 192.168.1.200 -j DROP<br \/>\n# \/sbin\/service iptables save<\/code><br \/>\nYou can also use FORWARD default chainswhen packets send through another interface. Usually FORWARD used when you setup Linux as a router:<br \/>\n<code># \/sbin\/iptables -A FORWARD -s 192.168.1.200 -j DROP<br \/>\n# \/sbin\/service iptables save<\/code><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.cyberciti.biz\/faq\/iptables-block-port\/ &nbsp; &nbsp; Block Incoming Request From IP 1.2.3.4 The following command will drop any packet coming from the IP address 1.2.3.4: \u00a0 \/sbin\/iptables -I INPUT -s {IP-HERE} -j DROP \/sbin\/iptables -I INPUT -s 1.2.3.4 -j DROP You can also specify an interface such as eth1 via which a packet was received: \u00a0 \/sbin\/iptables -I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-815","post","type-post","status-publish","format-standard","hentry","category-kategerisiz"],"_links":{"self":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":2,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"predecessor-version":[{"id":821,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/815\/revisions\/821"}],"wp:attachment":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}