{"id":2118,"date":"2012-03-06T22:25:11","date_gmt":"2012-03-06T14:25:11","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=2118"},"modified":"2023-04-28T09:49:42","modified_gmt":"2023-04-28T09:49:42","slug":"how-to-install-configure-and-use-linux-iptables-firewall-on-centos-6-2","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-install-configure-and-use-linux-iptables-firewall-on-centos-6-2\/","title":{"rendered":"How to Install, Configure and Use Linux Iptables Firewall on CentOS 6.2"},"content":{"rendered":"<p>Iptables is the most popular packet filtering firewall package in linux. It can be used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. Iptables interfaces to the Linux netfilter module to perform filtering of network packets. In this post, i will show on how to install, configure and use Iptables Firewall on CentOS 6.2 server :<\/p>\n<p>1. Check iptables installed package :<\/p>\n<pre>\n[root@centos62 ~]# rpm -qa | grep iptables\niptables-1.4.7-4.el6.i686\niptables-ipv6-1.4.7-4.el6.i686\n<\/pre>\n<p>2. Check Iptables version :<\/p>\n<pre>\n[root@centos62 ~]# iptables --version\niptables v1.4.7\n<\/pre>\n<p>3. If Iptables not installed, simply run this command to install :<\/p>\n<pre>\n[root@centos62 ~]# yum install iptables\n<\/pre>\n<p>4. Check Iptables status whether up or not :<\/p>\n<pre>\n[root@centos62 ~]# service iptables status\nTable: filter\nChain INPUT (policy ACCEPT)\nnum  target     prot opt source               destination\n1    ACCEPT     all  --  0.0.0.0\/0            0.0.0.0\/0           state RELATED,ESTABLISHED\n2    ACCEPT     icmp --  0.0.0.0\/0            0.0.0.0\/0\n3    ACCEPT     all  --  0.0.0.0\/0            0.0.0.0\/0\n4    ACCEPT     tcp  --  0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:22\n5    REJECT     all  --  0.0.0.0\/0            0.0.0.0\/0           reject-with icmp-host-prohibited\n\nChain FORWARD (policy ACCEPT)\nnum  target     prot opt source               destination\n1    REJECT     all  --  0.0.0.0\/0            0.0.0.0\/0           reject-with icmp-host-prohibited\n\nChain OUTPUT (policy ACCEPT)\nnum  target     prot opt source               destination\n<\/pre>\n<p>If Iptables not running, it will return this message :<\/p>\n<pre>\n[root@centos62 ~]# service iptables status\niptables: Firewall is not running.\n<\/pre>\n<p>5. Display Default Iptables rules:<\/p>\n<pre>\n[root@centos62 ~]# cat \/etc\/sysconfig\/iptables\n# Firewall configuration written by system-config-firewall\n# Manual customization of this file is not recommended.\n*filter\n:INPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT\n-A INPUT -p icmp -j ACCEPT\n-A INPUT -i lo -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n-A INPUT -j REJECT --reject-with icmp-host-prohibited\n-A FORWARD -j REJECT --reject-with icmp-host-prohibited\nCOMMIT\n<\/pre>\n<p>6. To start, stop, and restart iptables, you can run below commands :<\/p>\n<pre>\n[root@centos62 ~]# service iptables start\n[root@centos62 ~]# service iptables stop\n[root@centos62 ~]# service iptables restart\n<\/pre>\n<p>7. To set iptables start at boot :<\/p>\n<pre>\n[root@centos62 ~]# chkconfig iptables on\n<\/pre>\n<p>8. Display current opened port :<\/p>\n<pre>\n[root@centos62 ~]# netstat -plunt\nActive Internet connections (only servers)\nProto Recv-Q Send-Q Local Address               Foreign Address             State       PID\/Program name\ntcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1102\/sshd   \ntcp        0      0 :::22                       :::*                        LISTEN      1102\/sshd   \n<\/pre>\n<p>Note : Only ssh port has been opened on this server and listening port is 22.<\/p>\n<p>9. Add below line to enable certain port\/programs to pass through firewall such as:<\/p>\n<p>80 = Web service \/ httpd service<br \/>\n3306 = MySQL service \/ mysqld service<\/p>\n<p>10. View and modify original Iptables configuration file :<\/p>\n<pre>\n[root@centos62 ~]# vi \/etc\/sysconfig\/iptables\n<\/pre>\n<p>Original Iptables configuration file<\/p>\n<pre>\n# Firewall configuration written by system-config-firewall\n# Manual customization of this file is not recommended.\n*filter\n:INPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT\n-A INPUT -p icmp -j ACCEPT\n-A INPUT -i lo -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n-A INPUT -j REJECT --reject-with icmp-host-prohibited\n-A FORWARD -j REJECT --reject-with icmp-host-prohibited\nCOMMIT\n<\/pre>\n<p>11. Modify the Iptables configuration file as below. Add port &#8220;80&#8221; and port &#8221; 3306&#8243; :<\/p>\n<pre>\n# Firewall configuration written by system-config-firewall\n# Manual customization of this file is not recommended.\n*filter\n:INPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT\n-A INPUT -p icmp -j ACCEPT\n-A INPUT -i lo -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT\n-A INPUT -j REJECT --reject-with icmp-host-prohibited\n-A FORWARD -j REJECT --reject-with icmp-host-prohibited\nCOMMIT\n<\/pre>\n<p>12. Start httpd and mysqld daemon service :<\/p>\n<pre>\n[root@centos62 ~]# service httpd start\n[root@centos62 ~]# service mysqld start\n<\/pre>\n<p>13. Print updated opened port :<\/p>\n<pre>\n[root@centos62 ~]# netstat -plunt\nActive Internet connections (only servers)\nProto Recv-Q Send-Q Local Address               Foreign Address             State       PID\/Program name\ntcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1102\/sshd   \ntcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2482\/mysqld \ntcp        0      0 :::80                       :::*                        LISTEN      2345\/httpd  \ntcp        0      0 :::22                       :::*                        LISTEN      1102\/sshd   \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Iptables is the most popular packet filtering firewall package in linux. It can be used to set up, maintain, and inspect the tables of IP packet filter rules in the&#8230;<\/p>\n","protected":false},"author":6,"featured_media":1554,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1158],"tags":[1253,1519,1536,1744],"class_list":["post-2118","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iptables","tag-centos-6-2","tag-iptables","tag-linux","tag-security"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/2118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=2118"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/2118\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/1554"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=2118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=2118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=2118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}