Share your love
How To Install and Use Iptables on Rocky Linux 8: Easy Guide
In this guide, we want to teach you How To Install and Use Iptables on Rocky Linux 8. iptables is a command line utility for configuring a Linux kernel firewall implemented within the Netfilter project. The term iptables is also commonly used to refer to this kernel-level firewall. It can be configured directly with iptables, or by using one of the many console and graphical front-ends. iptables is used for IPv4 and ip6tables is used for IPv6. Both iptables and ip6tables have the same syntax, but some options are specific to either IPv4 or IPv6.
Follow the guide steps provided by the Orcacore team to Install and Use Iptables on Rocky Linux 8.
Table of Contents
Steps To Install and Use Iptables on Rocky Linux 8
To Install and Use Iptables on Rocky Linux 8, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Rocky Linux 8.
Once you are done, follow the guide steps to Install and Use Iptables on Rocky Linux 8.
1. Install Iptables Firewall on Rocky Linux 8
First, update your local package index with the following command:
sudo dnf 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 dnf install iptables-services
Verify your Iptables installation by using the command below:
sudo rpm -qa | sudo grep -i iptables-services
Output
iptables-services-1.8.4-22.el8.x86_64
2. Manage Iptables Service
At this point of Install and Use Iptables on Rocky Linux 8, you can use the command below to start Iptables on Rocky Linux 8:
sudo systemctl start iptables
To enable it to start on boot, use the command below:
sudo systemctl enable iptables
Verify that the Iptables service is active and running on Rocky Linux 8:
sudo systemctl status iptables
In your output you will see:
Output
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor pr>
Active: active (exited) since Sat 2022-10-08 06:30:47 EDT; 10s ago
Main PID: 39471 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 23668)
Memory: 0B
CGroup: /system.slice/iptables.service
...
3. Check iptables Configuration
At this point of Install and Use Iptables on Rocky Linux 8, 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:
Now that you have installed iptables on Rocky Linux 8, let’s do some basic usage of it.
4. How To Use iptables on Rocky Linux 8
In this part of Install and Use Iptables on Rocky Linux 8, 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 Rocky Linux 8 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 Rocky Linux 8 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 Rocky Linux 8 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 Rocky Linux 8. 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
Delete a specific rule
In this part of Install and Use Iptables on Rocky Linux 8, you can learn the delete rule. To delete a specific rule, you need to first check the available rules of iptables on Rocky Linux 8 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 all -- anywhere anywhere state RELATED,ESTABLISHED
2 ACCEPT icmp -- anywhere anywhere
3 ACCEPT all -- anywhere anywhere
4 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
5 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
6 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 4
To apply the changes that have been saved on boot you can use the following command:
sudo /sbin/iptables-save
Disable iptables
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 their options it is highly recommended to read its manual:
man iptables
Conclusion
At this point, you have learned to Install and Use Iptables on Rocky Linux 8. You can easily manage your firewall rules on your Rocky Linux 8 by using the iptables which is one of the most popular command line utilities for Linux.
Hope you enjoy it. Also, you may like to read the following articles:
Open and Close Ports with FirewallD on Rocky Linux 8