It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In RegisterIt looks like you're new here. If you want to get involved, click one of these buttons!
Iptables is a firewall program for Linux. It allows administrators to define rules that control inbound, outbound, and forwarded traffic based on various criteria such as IP addresses, ports, and protocols. It acts as a firewall, helping to secure your system against unauthorized access and malicious traffic.
Rules & Chains – Iptables operates using rules that define how to handle network packets, organized into chains (INPUT, OUTPUT, FORWARD).
Tables – Different tables serve specific purposes:
Packet Flow – When a packet reaches the system, iptables checks it against rules in the appropriate chain and decides whether to ACCEPT, DROP, or REJECT it.
Order of Rules – Rules are processed sequentially, meaning the first matching rule determines the packet’s fate.
Persistence – Rules set with iptables commands are temporary; to make them permanent, they must be saved and reloaded after a reboot.
Important! Iptables rules only apply to ipv4. If you want to set up a firewall for the ipv6 protocol, you will need to use ip6tables instead.
Iptables is installed by default on most Linux distributions. To confirm that iptables is installed, run:
iptables --version
1.1. The command shows the version number. If the package is not found, check the guide on your installed OS:
sudo apt install iptables
1.2. To keep iptables firewall rules after reboot, install the persistent package:
sudo apt install iptables-persistent
1.3. Enable the netfilter-persistent service on restart
sudo systemctl enable netfilter-persistent
sudo yum install iptables
1.2. To persist firewall rules after restart, install the following package:
sudo yum install iptables-services
1.3. Enable the service automatically when the system reboots:
sudo systemctl enable iptables
2.1. Check current Iptables rules before making changes:
sudo iptables -L -v -n
2.2. If you want to start fresh, you can clear the rules:
sudo iptables -F
2.3. To remove chains use the command below:
sudo iptables -X
2.4. Define default behavior for each chain:
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
2.5. Allow Loopback traffic. Allowing traffic from your system (localhost) is secure and allows applications to communicate with the localhost interface. Enter the following to append the INPUT chain:
sudo iptables -A INPUT -i lo -j ACCEPT
2.6. Allow SSH access. The port (22) can be changed if needed:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
2.7. Allow HTTP & HTTPS web traffic:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
2.8. Allow ICMP (ping requests):
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
2.8.1. Either remove the possibility to ping requests:
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
2.9. Control traffic by IP address:
sudo iptables -A INPUT -s [IP-address] -j ACCEPT
It is allowed to insert one IP address per rule, it means you need to create separate rules.
2.9.1. You can also reject traffic from an IP address or IP address range:
sudo iptables -A INPUT -m iprange --src-range [IP-address-range] -j REJECT
2.10. Save the rules:
Debian/Ubuntu
sudo netfilter-persistent save
AlmaLinux/Rocky Linux (RHEL)
sudo service iptables save
3.1. To save the current iptables rules to a file run this command (use naming and path for your own server):
sudo iptables-save > ~/iptables-backup.txt
3.2. To restore rules from a backup file run the command (with your naming and path to the file):
sudo iptables-restore < ~/iptables-backup.txt
Iptables is a powerful tool for managing network security on Linux. By understanding how to configure filtering rules, allow necessary traffic, and block unwanted connections, you can effectively protect your system. Always remember to save your rules to make them persistent and regularly review them to adapt to new security needs. With this guide, you now have the foundation to manage iptables confidently.