Skip to content
Log In Sign Up info@muvcloud.com

Before You Change Any Rule

A DROP policy set before the SSH rule cuts your connection at once. Keep your current session open, know where the server console is in your provider's control panel, and schedule an automatic rollback first. The at command needs the at package and its atd service, with the same names on all four distributions:

sudo apt install at     # Ubuntu, Debian
sudo dnf install at     # AlmaLinux, Rocky Linux
sudo systemctl enable --now atd

Save the current rules, then queue the rollback:

sudo sh -c 'iptables-save > /root/rules.v4.bak; ip6tables-save > /root/rules.v6.bak'
echo 'iptables -P INPUT ACCEPT; iptables -F INPUT; iptables-restore < /root/rules.v4.bak' | sudo at now + 5 minutes
echo 'ip6tables -P INPUT ACCEPT; ip6tables -F INPUT; ip6tables-restore < /root/rules.v6.bak' | sudo at now + 5 minutes

If the new rules lock you out, the two jobs open the input chains and restore the old rules within five minutes. When a new SSH login works from a second terminal, cancel both: sudo atq shows the job numbers, and sudo atrm JOB_NUMBER removes a job.

iptables, nftables, and Front Ends

On current distributions the iptables command is normally iptables-nft: classic syntax, nftables rules underneath. Check:

sudo iptables -V

(nf_tables) after the version means the nftables back end; (legacy) is the old one. Do not mix rules written with iptables and with nft (see how to use nftables). ufw and firewalld own the rule set while active; on such a server use their commands, such as ufw allow. On a Docker host, leave the FORWARD chain and the Docker chains alone; see Docker port mapping.

List Rules

sudo iptables -L -n -v --line-numbers
sudo iptables -S

-n skips slow name lookups, -v adds packet counters and interfaces, and --line-numbers numbers each rule. -S prints the rules in the form you would type them. Each chain header shows its policy, which applies to packets no rule matched. Use ip6tables the same way for IPv6.

Add Rules

sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
sudo iptables -I INPUT 3 -s 203.0.113.10 -j DROP

-A appends a rule to a chain; -I inserts it at a position, or at the top without a number. The first matching rule decides, so a block for one address must sit above the rules that would accept it.

A Minimal Server Rule Set

These commands take effect one by one, so the order is what keeps your session alive. Start from an empty INPUT chain with an ACCEPT policy, add the rules that let established traffic and SSH in, and set the DROP policy last:

sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p icmp -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
sudo iptables -P INPUT DROP

Then the same for IPv6. ICMPv6 must be allowed, because IPv6 uses it to find neighbors and routers:

sudo ip6tables -A INPUT -i lo -j ACCEPT
sudo ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo ip6tables -A INPUT -p ipv6-icmp -j ACCEPT
sudo ip6tables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo ip6tables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
sudo ip6tables -P INPUT DROP

Replace 22 with your SSH port if you changed it. The ESTABLISHED,RELATED rule lets replies to the server's own connections back in, such as DNS answers. Test a new SSH login, then cancel the rollback job.

Delete Rules

sudo iptables -D INPUT 4
sudo iptables -D INPUT -p tcp --dport 8080 -j ACCEPT

Delete by number or by repeating the rule exactly. sudo iptables -F deletes every rule but keeps the policies, so with a DROP policy it cuts SSH at once: set the policy to ACCEPT before you flush.

Make the Rules Persistent

Rules added with the iptables command live in memory and are gone after a reboot. On Ubuntu and Debian:

sudo apt install iptables-persistent
sudo netfilter-persistent save

The rules go to /etc/iptables/rules.v4 and rules.v6, and the netfilter-persistent service loads them at boot. Do not use it where ufw is active; both would manage the same rules. On AlmaLinux and Rocky Linux, keep permanent rules in firewalld (see how to use firewalld) or in an nftables file loaded by the nftables service.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations