In this guide, we want to teach you How To Install and Use Iptables on Ubuntu 22.04.
Implemented as Netfilter modules, iptables is a user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall. The filters are organized into tables containing chains of rules which govern how to treat network traffic packets.
Netfilter is the firewall framework on Linux, and iptables is the utility that is used to manage and control Netfilter. You can use iptables to filter both incoming and outgoing packets as well as route network packets.
Install and Use Iptables on Ubuntu 22.04
To install iptables on Ubuntu 22.04, you must 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 22.04.
Now follow the steps below to complete this guide.
Set up iptables firewall on Ubuntu 22.04
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
- The -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 22.04, let’s see some basic usage of it.
How To Use iptables
In this part, we intend to show you some basic usage of iptables on your server.
You can use iptables to define a rule. It means that you can append it to the chain.
To do this, you can use the -A parameter after the iptables command on Ubuntu 22.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 22.04 in the order shown below:
sudo iptables -A <chain> -i <interface> -p <protocol (tcp/udp) > -s <source> --dport <port no.>Â -j <target>
Iptables Allow Traffic
To allow traffic on localhost, you can use the iptables firewall command like the below command:
sudo iptables -A INPUT -i lo -j ACCEPT
Iptables Enable Connection
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 22.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 Filter Packets
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 22.04. For example:
sudo iptables -A INPUT -s 10.10.0.1 -j ACCEPT
Iptables Reject Packets
You can also reject the packets with the command below:
sudo iptables -A INPUT -s 10.10.0.1 -j DROP
Iptables Drop All Traffics
Also, you can drop all other traffic simply by using the following command:
sudo iptables -A INPUT -j DROP
Iptables Delete Rules
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 22.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
Disable iptables Ubuntu
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]
...
If you want to find more detailed information about iptables and its options it is highly recommended to read its manual:
man iptables
Conclusion
Iptables is a powerful firewall that you can easily benefit from. It is necessary for every system administrator to learn at least the basics of iptables.
Hope you enjoy it.
You may be interested in these articles:
How To Install PowerShell on Ubuntu 22.04
Install and Use CMake on Ubuntu 22.04