How to Install Iptables on Ubuntu Server 14.04

Ubuntu uses UFW (Ubuntu Firewall) as the Frontend tool to manage netfilter firewall rules by default. But if you are more familiar with iptables, no problem we can easily install and configure iptables on Ubuntu Server. So lets see how to install iptables on Ubuntu server 14.04.

Install Iptables Ubuntu Server
Step One

Disable UFW

Before install iptables, we should disable UFW First. Both UFW and ubuntu iptables use as a frontend tool to manage netfilter firewall rules, but we do not want any conflict between two. To Disable UFW Open Command line interface and enter the following command.

ufw disable

Step Two

Install Iptables on Ubuntu Server

To Install iptables on Ubuntu Server 14.04, Enter following Command on CLI

apt-get install iptables

Step Three

Configure Iptables on Ubuntu

Now we need to create the configuration file and add default firewall rules to the configuration file.  First create a folder call firewall inside /etc directory.

mkdir /etc/firewall

Now Create the Iptables Configuration File inside the /etc/firewall directory. We add all our permanent firewall rules to this file.

touch /etc/firewall/iptables

Add Following Default rules to the /etc/firewall/iptables configuration file. These rules set will add default firewall policies and also allow the ssh remote access to the server from the firewall.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

Step Four

Start and Apply Firewall Rules

After adding the rules to the configuration file, run following command to start and apply the firewall rules

iptables-restore < /etc/firewall/iptables

Step Five

Create Cronjob to Run at Boot Time

So We have installed and added default rules to the configuration file. Now we need to add a cron job to run when system reboot, So firewall rules will be automatically applied at boot time.

First open the /etc/crontab using a text editor

vim /etc/crontab

Now add following line to the /etc/crontab

@reboot root iptables-restore < /etc/firewall/iptables

@reboot use to run cron jobs when the system reboots. So Firewall rules should automatically will be applied every time Ubuntu server reboots.

So That is How to Install iptables on Ubuntu Server 14.04. Also This works for the previous version of the Ubuntu server, Including Ubuntu 12 and 10.