Install and Use Iptables Firewall on Ubuntu 20.04

In this article, we want to teach you How To Install and Use the Iptables Firewall on Ubuntu 20.04.

Iptables is a command-line firewall utility that uses policy chains to allow or block traffic.

When a connection tries to establish itself on your system, iptables looks for a rule in its list to match it to. If it doesn’t find one, it resorts to the default action.

Steps To Install and Use Iptables Firewall on Ubuntu 20.04

To install iptables on Ubuntu 20.04, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 20.04.

Now follow the steps below to install iptables on Ubuntu 20.04.

Install iptables on Ubuntu

First, update your local package index with the following command:

sudo apt update

By default, iptables come pre-installed in most Linux distributions.

If you don’t have it, then, you can use the following command to install an iptables firewall on your server:

sudo apt install iptables

You can check your iptables configuration with the following command:

sudo iptables -L -v

-L parameter is for the list of all rules, and the -v parameter is used to show information in more detail.

In your output you will see:

Output
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Now that you have installed iptables on Ubuntu 20.04, let’s see some basic usage of it.

How To Use Ubuntu iptables

You can use iptables to define a rule. It means that you can append it to the chain.

To do this, you can use -A parameter after the iptables command on Ubuntu 20.04 as shown below:

sudo iptables -A [argument]

Also, you can combine the command with other parameters like:

-i: the network interface whose traffic you want to filter, such as eth0, lo, ppp0, etc.

-p:  the network protocol where your filtering process takes place. It can be either TCP, UDP, udplite, ICMP, SCTP, icmpv6, and so on. Also, you can type all to choose every protocol.

-s: the address from which traffic comes. You can add a hostname or IP address.

-dport: the destination port number of a protocol, such as 22 (SSH), 443 (https), etc.

-j:  the target name (ACCEPT, DROP, RETURN). You need to insert this every time you make a new rule.

Note: If you want to use all of the parameters, you need to type the iptables command on Ubuntu 20.04 in the order shown below:

sudo iptables -A <chain> -i <interface> -p <protocol (tcp/udp) > -s <source> --dport <port no.>  -j <target>

To allow traffic on localhost, you can use the iptables firewall command like the below command:

sudo iptables -A INPUT -i lo -j ACCEPT

You can enable a connection like SSH on iptables command like this:

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Then, you can check the rule that has been appended in iptables on Ubuntu 20.04 with the command below:

sudo iptables -L -v

In your output you will see:

Output
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh

Iptables allow you to filter packets on an IP address or a range of IP addresses. To do this, you need to use the -s parameter in the iptables command on Ubuntu 20.04. For example:

sudo iptables -A INPUT -s 10.10.0.1 -j ACCEPT

You can also reject the packets with the command below:

sudo iptables -A INPUT -s 10.10.0.1 -j DROP

Also, you can drop all other traffic simply by using the following command:

sudo iptables -A INPUT -j DROP

To delete all current rules on your iptables firewall you can use the following command:

sudo iptables -F

To delete a specific rule, you need to first check the available rules of iptables on Ubuntu 20.04 by typing the following command:

sudo iptables -L --line-numbers

In your output you will see:

Output
num target prot opt source destination
1 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
2 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh

Here to delete a specific rule, you also need to specify the rule number in the command below:

sudo iptables -D INPUT 2

To apply the changes that have been saved on boot you can use the following command:

sudo /sbin/iptables-save

If you want to disable iptables, you can use the following commands:

$ sudo iptables -F
$ sudo /sbin/iptables-save

In your output you will see:

Output
:INPUT ACCEPT [19:2597]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [6:528]
...

Conclusion

At this point, you learn to install iptables on Ubuntu 20.04. Also, you learn some basic usage of it.

Hope you enjoy using it.

You may be like these articles:

Install and Configure CSF Firewall on AlmaLinux 9

How To Enable FirewallD GUI on Centos 7

Open and Close Ports with FirewallD on Rocky Linux 8

Install and Configure CSF Firewall on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!