Celebrate Our 22nd Anniversary with Huge Savings! Up to 70% Off

How to Setup Iptables Rules in Ubuntu VPS Server [24.04]?

For any website, it is necessary to have a firewall (a checkpoint between networks). After passing through the firewall, a website or an IP can access the server & its applications. However, a firewall is the first layer of security. To ensure multiple levels of security, it is recommended to configure iptables which provide a facility to set rules for incoming or outgoing traffic.

Iptables are a critical firewall utility with Linux systems, bringing massive security by filtering incoming and outgoing network traffic based on specific pre-set rules. It allows the system administrator to control the server's entry and exit traffic. Therefore, it is a vital tool for keeping your Ubuntu VPS secure against unauthorized users - DDoS- and other malicious attacks. 

This guide will take you through the step-by-step process of implementing iptables rules on your Ubuntu VPS version 24.04, ensuring your server is secured against possible threats.

 

Why Set up iptables in Ubuntu?

Iptables are essential to protect the Linux server from many cyber threats. Through the help of iptables, one can ensure no unauthorized access is granted by allowing only specific traffic to enter/leave the server. 

 

How Does Setting iptables Rules Help?

  • Protect against brute force attacks, DDoS attacks, and port scanning.
  • Lower the chances of malware infection due to some bad traffic.
  • Enhance network performance by filtering out unwanted traffic.
  • Allow administrators to create their own traffic rules for both inbound and outbound traffic on the levels of various protocols, ports, and IP addresses.

Proper configuration ensures your server remains secure and, at the same time, functions effectively in a controlled network environment.

 

How Do iptables Work?

Iptables work by inspecting incoming traffic through pre-defined firewall rules. When the incoming traffic reaches the server, it will be verified by iptables if it fits any predefined criteria. If a match is found, iptables will take appropriate action. Without a matching rule, iptables uses a default policy to assign traffic. Generally, iptables would allow anything that fits the rules and block anything that doesn't by default. 

 

How do I configure IP tables in Ubuntu OS?

You need a VPS with Ubuntu Linux OS and sudo privileges to set iptables rules.

Recommended:

  • Type the following command to list the available packages:

    sudo apt update
  • To upgrade the latest versions of all the packages, type the command:

    sudo apt upgrade
 

How to install iptables in Ubuntu VPS?

There are two commands to install iptables:

  • The below command simply installs iptables and saves rules (temporary) until you reboot the system.

    sudo apt install iptables
  • The above command installs and saves rules permanently, even after a system reboot.

    sudo apt install iptables-persistent


Command

Function

Persistence

sudo apt install iptables

Firewall management tool

❌ No persistence (rules lost after reboot)

sudo apt install iptables-persistent

Automatically saves and restores firewall rules

✅ Yes (Rules restored after reboot)

After you type the above command, it will ask for the removal of ufw (Uncomplicated Firewall):

Click ‘Y’ to proceed with the installation of iptables-persistent.

Output of the iptables-persistent command:

 

 

How to Verify iptables Installation?

After installing iptables through the above commands, verify installation by typing the command:

 
sudo iptables --version
 

Output: shows the latest version of iptables installed

 

 

How to Disable UFW (Uncomplicated Firewall)?

 
Note: ufw is the front-end for iptables and can exist with iptables, but ufw and iptables - persistent are not compatible, so it will ask you to disable ufw at the time of installing iptables - persistent.
 
sudo ufw disable

# Only needed with iptables (Optional)

This command is used to uninstall ufw completely:

 
sudo apt remove --purge ufw
 

Output:-

 

 

To remove any packages or dependencies of the uninstalled packages like ufw:

 
sudo apt autoremove
 

How To Define the iptables Rules in Ubuntu VPS?

How to See Any Predefined Rules for iptables

First of all, check for any pre-defines iptables rules in your VPS by typing the command:

 
sudo iptables -L -v
 

 -v – shows detailed outputs like packet count, byte count, etc.

We are now ready to define the iptables rules:-

 
Note:- We are defining the rules, and there will be no direct output of any rule, but the rules will affect how the firewall behaves with website traffic.
 

# Allow all incoming traffic temporarily (default policy)

 
iptables -P INPUT ACCEPT
 

The above command allows everything for incoming traffic temporarily before setting rules.

# Flush (remove) all existing rules

 
iptables -F
 

It deletes all the current or previously defined, which is helpful while creating or defining new rules from scratch.

# Delete any custom chains

 
iptables -X
 

It removes all the custom UDF (User Defined Rules) rules.

# Allow loopback traffic

 
iptables -A INPUT -i lo -j ACCEPT
 

It allows local traffic (traffic generated from the internal system processes like MySQL on 127.0.0.1)

# Allow incoming traffic for established and related connections

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

The above command allows incoming packets that are part of existing or related connections (like replies from a web server or SSH session).

# Allow SSH connections

 
iptables -A INPUT -p tcp -m tcp -m state -m comment -s 0.0.0.0/0 --dport 22 --state NEW -j ACCEPT --comment “SSH”
 

It allows new incoming SSH connections on port 22 from any IP address.

# Allow ping requests (ICMP echo)

 
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
 

It allows ping requests so the server can be tested for connectivity.

 

How Do You Block All Other Ports?

Always remember to use the DROP chain policy after accepting the required ports.

 
Note:- Insert all the needed ports as shown in the example below before issuing the DROP command:
 

# Allow all traffic on the loopback interface (important for internal processes)

 
iptables -A INPUT -i lo -j ACCEPT
 

# Allow established and related connections (prevents breaking existing connections)

 
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
 

# Allow SSH (Port 22) - Prevent getting locked out

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

# Allow HTTP (Port 80) - If running a web server

 
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 

# Allow HTTPS (Port 443) - Secure web traffic

 
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
 

Once you add the required ports above, you can block all other ports that are not mentioned by you.

INPUT DROP: Drops any incoming traffic that is not explicitly allowed.

 
iptables -P INPUT DROP
 

How to Forward Traffic in Ubuntu?

FORWARD DROP: Blocks any forwarded packets.

 
iptables -P FORWARD DROP
 

How do you allow all outgoing traffic in Ubuntu?

OUTPUT ACCEPT: Allows all outgoing traffic.

 
iptables -P OUTPUT ACCEPT
 

How Do I Block an IP?

If you want to allow IPs only from trusted sources, you can do that with the help of this rule:

 
iptables -A INPUT -s 192.162.0.215 -j DROP
 

How Do I Allow an IP?

You can even allow a range of IP’s with the help of this rule:

 
iptables -A INPUT -s 192.162.0.215 -j ACCEPT
 

How to Check the Rules You Defined Through iptables -persistent?

This file stores IPv4 firewall rules that are automatically loaded at system startup if iptables-persistent is installed.

Run this command to check your rules are defined in the file:

 
cat /etc/iptables/rules.v4
 

Output:-

 

 

How to Edit/Replace the Existing iptables Rules?

To Edit or Replace specific rules in iptables, use the command:

First of all, list the iptables rules with their line number :

 
iptables -L --line-numbers
 

This command helps edit a specific rule as you don't need to reset all the existing rules to add a single.

Syntax:

 
iptables -R <chain> <rulenum> <new rule def>
 

Example:

 
iptables -R INPUT 2 -p tcp -s 192.168.0.215/24 --dport 80 -j ACCEPT
 

How to Reset the iptables Rules Defined?

 
Note: Keep in mind that if no rules are defined, the iptable will allow all the incoming traffic by default, which will keep your server unprotected.
 

Resetting rules will permanently clear all the rules you defined, but be sure to reset only when you want to add new rules from scratch.

Command for resetting iptables:

 
iptables -F
 

While defining rules, utmost care should be taken, and every command should be used cautiously while you create rules for your website traffic. Installing iptables is not much, but setting iptables rules helps with security hardening for your domains and clients. Contact our support team for any issues you may face while configuring iptables.


Was this answer helpful?

« Back

chat