![]() |
|
How to Install and Use Iptables Linux Firewall - Printable Version +- My Board (https://ellohost.com/forum) +-- Forum: Tutoriel EN (https://ellohost.com/forum/forumdisplay.php?fid=8) +--- Forum: Others (https://ellohost.com/forum/forumdisplay.php?fid=20) +--- Thread: How to Install and Use Iptables Linux Firewall (/showthread.php?tid=136) |
How to Install and Use Iptables Linux Firewall - aaron - 08-12-2023 How to Install and Use Iptables Linux FirewallWe will divide this iptables tutorial into three steps. First, you will learn how to install the tool on Ubuntu. Secondly, we are going to show you how to define the rules. Lastly, we will guide you to make persistent changes in iptables. 1. Install IptablesIptables comes pre-installed in most Linux distributions. However, if you don’t have it in Ubuntu/Debian system by default, follow the steps below:
Check the status of your current iptables configuration by running:
![]() Here, the
You will now have the Linux iptables firewall installed. At this point, you can notice that all chains are set to ACCEPT and have no rules. This is not secure since any packet can come through without filtering. Don’t worry. We’ll tell you how to define rules on the next step of our iptables tutorial. 2. Define Chain RulesDefining a rule means appending it to the chain. To do this, you need to insert the
It will alert iptables that you are adding new rules to a chain. Then, you can combine the command with other options, such as:
If you want to use all of them, you must write the command in this order:
Once you understand the basic syntax, you can start configuring the firewall to give more security to your server. For this iptables tutorial, we are going to use the INPUT chain as an example. Enabling Traffic on Localhost To allow traffic on localhost, type this command:
For this iptables tutorial, we use Enabling Connections on HTTP, SSH, and SSL Port Next, we want HTTP (port 80), HTTPS (port 443), and SSH (port 22) connections to work as usual. To do this, we need to specify the protocol (
It’s time to check if the rules have been appended in iptables:
It should return with the results below which means all TCP protocol connections from the specified ports will be accepted: Filtering Packets Based on Source Iptables allows you to filter packets based on an IP address or a range of IP addresses. You need to specify it after the
You can also reject packets from a specific IP address by replacing the
If you want to drop packets from a range of IP addresses, you have to use the
Dropping all Other Traffic It is crucial to use the
Now, the connection outside the specified port will be dropped. Deleting Rules If you want to remove all rules and start with a clean slate, you can use the
This command erases all current rules. However, to delete a specific rule, you must use the
You will get a list of rules with numbers:
To delete a rule, insert the corresponding chain and the number from the list. Let’s say for this iptables tutorial, we want to get rid of rule number three of the INPUT chain. The command should be:
3. Persist ChangesThe iptables rules that we have created are saved in memory. That means we have to save them to a file to be able to load them again after a reboot. To make these changes you can use these commands depending if you are saving IPv4 or IPv6 rules:
Now whenever you restart your VPS you will need to load the saved rules with the following commands:
If you want for the loading process to be completely automatic, you can set up the
After installation, you will be asked to save the current rules. Choose Yes for both IPv4 and IPv6 and finish the configuration. Now the loading process will be automatic. Keep in mind that you will still need to use the |