Setup and Configure Basic iptables on CentOS 6 Linux system
In this article we will setup and configure Basic iptables on Linux systems (RPM Family “Redhat 6/CentOS 6/Scientific Linux 6”). This is the first step every experienced sysadmin must do Just after finishing installing the Linux OS “any member in any Family of Linux systems”. I’ll go and configure the iptables rules on RPM Linux Family releases 6 “CentOS 6”.
In RPM Linux Family release 6 iptables firewall is the default firewall, it included and enabled by default in any installation type “Minimal installation, Server installation, Server with GUI installation, etc..”.
We will learn how to configure iptables for basic usage. Set the default chain policy for INPUT, OUTPUT, and FORWARD chains. We will learn how to start, stop, flush and restart the service, how to secure CentOS 6 after the installation process, and how to save our rules for loading it at Boot time “make a persistent rule”.
So, Let’s start.
What is Iptables?
Iptables is an administration tool for IPv4 packet filtering and NAT. It’s a simple, a fast, and a powerful firewall installed on most Linux distributions. It’s a tool to secure, filter out, and block Internet traffic from accessing your Linux OS.
Iptables is used to set up, maintain, and inspect the tables of IPv4 packet filter rules in the Linux kernel. Several different tables may be defined. Each table contains a number of built-in chains and may also contain user-defined chains.
We will focus on the use of the default table “filter table”. All our rules in this article will be in this table. It contains the built-in chains INPUT, FORWARD, and OUTPUT. Here’s their definitions:
- INPUT: Packets is going to be locally delivered “accepted by Linux OS”.
- FORWARD: Packets being routed through the box “send to another interface”. Usually used when you setup Linux as router.
- OUTPUT: Packets sent from the machine itself will be visiting this chain.
A firewall rule specifies criteria for a packet and a target. If the packet does not match one rule, the next rule in the chain will be examined; if it does match,then the packet’s target will be checked to determine what action will be taken to that packet. I’ll use three targets ACCEPT, DROP, and REJECT. Here’s their definitions:
- ACCEPT means to let the packet through.
- DROP means to drop the packet on the floor, and do not send any response.
- REJECT means to drop the packet on the floor, and send a message as a response.
Step 1: Installing the required Linux packages
As we said before, iptables is the default firewall in RPM Linux family release 6, so it installed, and enabled by default, run the following command to check the status of iptables service:
# chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
So, the package is installed, and enabled by default on runlevels 2, 3, 4, and 5, but If it not installed for any reason, we will need to install it, Run the following commands to install and enable iptables” if not installed”:
# yum -y install iptables # chkconfig iptables on
At this point, we successfully installed and enabled iptables to run when the system boots on RPM Linux family release 6, let’s proceed with some administrative commands.
Step 2: Iptables Administrative Commands
In this section, we will learn how to manage iptables service. Iptables service is managed via system V init script. So, we can use either service command or /etc/init.d/iptables init script to manage iptables with the following options (start|stop|reload|restart|condrestart|status|panic|save). Later we will use the command iptables “found in this location /sbin/iptables ” to configure the iptables rules.
Option start is used to start the iptables service. This will apply all rules in /etc/sysconfig/iptables. To start the iptables service, run the following command:
# service iptables start
Option stop is used to stop iptables service. This will remove all applied firewall rules, and will set the default policy for INPUT, FORWARD, and OUTPUT chains to ACCEPT. Your machine will be accessible on all running ports. To stop the iptables service, run the following command:
# service iptables stop
Option reload will reload the currently persistent iptables rules found in /etc/sysconfig/iptables. To reload the iptables service, run the following: command:
# service iptables reload
Option restart will restart iptables if it already running or will start iptables if it’s not running. To restart the iptables service, run the following:
# service iptables restart
Option condrestart will restart iptables service if and only if the service is already running. To condrestart the iptables service, run the following:
# service iptables condrestart
Option status will return all applied iptables rules “persistent, and non-persistent”. Will return all applied rules from /etc/sysconfig/iptables and also all applied rules you applied from cli using the iptables command. This command is very useful when you want to remove a single applied rule “persistent, and non-persistent”. To show the applied iptables rules, run the following:
# service iptables status
Option panic will set all chains to DROP policy. This is useful in cases when your machine is under attack. This will DROP every incoming packets “established, and new”, every forwarded, and ever outgoing packets of course it will DROP your SSH connection. To panic the iptables service, run the following:
# service iptables panic
Option save will save currently applied (including manually applied rules through cli) iptables rules to /etc/sysconfig/iptables configuration file, making it persistent. To save your currently applied iptables rules, run the following:
# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
At this point you can deal with iptables service as you wish “start, stop, reload, etc..”. But there is some practical Hints you must be aware with when dealing with some of the above options.
Hints: 1. Reload, and Restart options will only apply the rules found in /etc/sysconfig/iptables "persistent rules" any non-persistent iptables rules "applied from cli using the iptables command" will not be applied. 2. Panic is very dangerous as it forces all chains policy to be DROP, it removes all applied rules, and closes all established connections to/from your server. You Must only run this command if and only if you are able to access your physical machine "connected through a native terminal device tty". If you are connected remotely to your machine "connected through pseudo terminal device pty i.e using any remote terminal program xterm, screen, or ssh", You will lose the connection to you server.
Step 3: A Look at the default applied iptables rules
Now, after we introduced how to manage the iptables service, it’s time to secure your CentOS 6 box. Let’s take a look at the default applied rules found in /etc/sysconfig/iptables. You can either cat this file or run “service iptables status”.
# cat /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
From the above output, we see that the existing rules applied for filter table, and the default policy is ACCEPT for the three chains INPUT, FORWARD, and OUTPUT. You may have this question in your mind Because the default policy is ACCEPT for all chains, so any new connection to my box is allowed?. The answer is ” NO “. Only SSH, and ICMP connections are allowed by default, take a look again to the above file, you will notice existence of the last INPUT rule ” -A INPUT -j REJECT –reject-with icmp-host-prohibited ” this rule will reject all connections not allowed in any rule above it with one message “connect to address <your server address>: No route to host”.
Let’s show an example, I’ll telnet from my laptop to port 25 “smtp” on my server. I didn’t open this port in any rule, and according to my explanation this connection will be rejected. run the following command:
$ telnet 192.168.12.45 25 Trying 192.168.12.45... telnet: connect to address 192.168.12.45: No route to host
The connection is rejected with “connect to address <your server address>: No route to host” message. If we changed the last INPUT rule to be “-A INPUT -j DROP “, and restarted the iptables service, then we run the telnet command again, we will not receive any message from the server, and the telnet will be always in trying state as follow:
$ telnet 192.168.12.45 25 Trying 192.168.12.45...
Step 4: Changing the default policy, and securing your Linux Box
Now. it’s time for securing your box, but because most of sysadmins are connecting remotely to their servers, I STRONGLY RECOMMEND TO ADD A CRON JOB TO STOP THE IPTABLES SERVICE EVERY 10 MINUTES WHILE YOU ARE WORKING. This to avoid loosing the connections to your server if you put a wrong iptables rule “only you will wait 10 minutes to gain access again”, run the following command to add the cronjob rule:
# crontab -e
And then add this cronjob and save the file:
*/10 * * * * /sbin/service iptables stop
Now, we safely start securing our CentOS 6 box, first of all we can either use iptables command to modify the iptables rules, all added rules will be gone when the system boots “because the added rules are non-persistent”, and this will require us to run “service iptables save” when we finish to make the added rules persistent OR Go directly and edit “/etc/sysconfig/iptables” file, this will require us to run “service iptables restart” to apply the added/modified iptables rules. Personally I prefer modifying the iptables file. So, let’s modify it to be like this one
# vim /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT COMMIT
Here’s what I did in the above file. First I changed the default INPUT, and FORWARD chain policy to DROP. This will drop any INPUT, and FROWARD packets that do not have any iptables rule allowing it. When any new packet is delivered to the INPUT or FORWARD chain, it will be checked against the existing iptables rules, if no match found the packet will be dropped “because of the default policy”. We only allow three iptables rules for incoming packets. We allow all established, and related packets exist before applying the new rules. Also, We allow packets generated from the local interface. Finally, We allow external packets for port 22 “packets for SSH service to be able to remotely manage the box”.
Any other packets not defined in the existing three rules, will be dropped. And this is the Basic iptables configuration for securing your Linux box.
Now, let’s apply those iptables rules, as we edited “/etc/sysconfig/iptables” file, we need to reload/restart the iptables service. Run the following command:
# service iptables restart iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] iptables: Applying firewall rules: [ OK ]
Hints: 1. The iptables command has option -F, --flush [chain], this will flush the selected chain (all the chains in the table if none is given). This is equivalent to deleting all the rules one by one. 2. Do not run this command "iptables -F " using the above configuration, as this will remove all the applied iptables rule, and any new packets will be matched against the default chain policy, which is DROP for INPUT, and FORWARD chain. You will lose the SSH connection to your box. But if you can access your machine's console "connected through a native terminal device tty", you can run it. Anyway you can go and try it, as we have a cronjob that stop iptables every 10 minutes. 3. If you are using the iptables command to edit the iptables rule, to make your rules persistent run this command "service iptables save"
Finally, If everything is well “you did not lose the SSH connection to your server”, we need to remove the cronjob, we created previously. Run the following command to remove the cronjob rule:
# crontab -e
And then remove/comment out this cronjob you created previously, save the file, and exit. Also make sure that iptables service is running after removing the cronjob, run the following command again:
# service iptables restart
In this article we have explained the meaning of iptables, also we have showed the default table “filter” and it’s three chains “INPUT, FORWARD, and OUTPUT” and their default installation policies “ACCEPT”. We showed you how to control the iptables service “i.e start, stop, restart, etc…”. We did our changes to secure our Linux box by direct modify “/etc/sysconfig/iptables” file. we mentioned one tip to use to avoid losing connections to your server if something wrong happened.
I hope this article is good enough for you.
See you in other articles.
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.