iptables and nftables commands cheat sheet

This cheat sheet brings together the iptables and nftables commands for managing the firewall on Linux servers, side by side: listing rules, tables and chains, existing connections, opening ports, rules by IP address, blocking, saving rules, NAT, and moving from iptables to nftables. You can search for a command, filter by topic, and copy it with one click.

Which iptables version the server runs

  • sudo iptables -V

    Shows the iptables version. When the output includes (nf_tables), the iptables commands run on top of nftables in the kernel, in the version called iptables-nft. On recent Debian releases, this is the default.

  • sudo update-alternatives --set iptables /usr/sbin/iptables-nft

    On Debian: sets the iptables command to run as iptables-nft. You select the older version the same way, with the path /usr/sbin/iptables-legacy, and for IPv6 rules you also set ip6tables.

Listing rules

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

    Lists the rules of the filter table by chain, with a line number for each rule, packet and byte counters, and network interfaces. The -n option shows addresses and ports as numbers and avoids slow DNS lookups.

  • sudo iptables -S

    Lists the rules in the syntax of the commands themselves, as in the output of iptables-save. Handy for copying, comparing, and deleting a rule by its exact wording.

  • sudo iptables -t nat -L -n -v

    Lists the rules of the nat table. Without -t, only the filter table is shown.

  • sudo ip6tables -L -n -v

    Lists the IPv6 rules. iptables rules apply to IPv4 only, and IPv6 rules are managed separately with ip6tables, using the same syntax.

  • sudo nft list ruleset

    Lists all the nftables rules in all tables, including rules created through iptables-nft or through other firewall management tools.

  • sudo nft list tables

    Lists the tables and the family of each one, for example table inet filter.

  • sudo nft list table inet filter

    Shows one table with its chains and rules.

  • sudo nft -a list chain inet filter input

    Shows one chain with an identifier (handle) for each rule. You need the handle to delete a rule or to add a rule before or after an existing one.

Tables and chains: the basics

  • filter

    The default iptables table, for filtering traffic. Its built-in chains are INPUT for traffic coming into the server, OUTPUT for traffic leaving it, and FORWARD for traffic passing through it.

  • nat

    The table for address translation, with the PREROUTING, INPUT, OUTPUT, and POSTROUTING chains. It's used for masquerading and port forwarding.

  • mangle, raw, security

    Tables for changing packet fields, exempting packets from connection tracking, and rules for access control systems such as SELinux. A typical server hardly ever needs them.

  • ACCEPT, DROP, REJECT

    The common actions: ACCEPT allows the packet, DROP discards it without a reply, and REJECT refuses it and sends an error message back to the sender. Rules are checked in order, and the first rule that accepts, drops, or rejects the packet decides what happens to it.

  • policy

    The policy of a built-in chain sets what happens to a packet that matches no rule: in iptables, the values are ACCEPT or DROP, and in nftables, they're accept or drop, with accept as the default.

  • ip, ip6, inet

    The nftables table families: ip for IPv4, ip6 for IPv6, and inet for both together. A table in the inet family saves you duplicate rules. When you don't give a family, nft uses the ip family.

  • type filter hook input priority filter

    nftables has no built-in chains. A base chain is created with a type, a hook into the kernel, and a priority. A chain with hook input is the equivalent of the iptables INPUT chain.

Creating a table and chain in nftables

  • sudo nft add table inet filter

    Creates a table named filter in the inet family, for IPv4 and IPv6 rules together. The name filter is customary but not reserved.

  • sudo nft add chain inet filter input '{ type filter hook input priority filter; policy accept; }'

    Creates a base chain for incoming traffic with an accept policy. The single quotes are needed so the shell doesn't interpret the braces and semicolons. Set the drop policy only after the SSH and existing-connection rules are in place.

Existing connections and loopback

  • sudo iptables -A INPUT -i lo -j ACCEPT

    Allows traffic on the lo (loopback) interface, which programs on the server use to communicate with each other.

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

    Allows packets of existing connections (ESTABLISHED) and of connections related to them (RELATED). Without this rule, a DROP policy also blocks the replies to connections the server itself started.

  • sudo iptables -A INPUT -m conntrack --ctstate INVALID -j DROP

    Drops packets that don't belong to any known connection.

  • sudo nft add rule inet filter input iif lo accept

    The nftables equivalent of the loopback rule.

  • sudo nft add rule inet filter input ct state established,related accept

    The nftables equivalent of the rule for existing and related connections.

  • sudo nft add rule inet filter input ct state invalid drop

    The nftables equivalent of dropping packets that don't belong to any known connection.

SSH and a DROP policy

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

    Allows SSH connections on port 22. If the SSH service is set to a different port, change the number. Add this rule before you change the policy to DROP.

  • sudo iptables -P INPUT DROP

    Drops all incoming traffic that matches no rule. Caution: on a remote server, run it only after the loopback, existing-connection, and SSH rules, or you'll be locked out of the server.

  • sudo iptables -P INPUT ACCEPT

    Sets the INPUT policy back to ACCEPT, for example before deleting all the rules.

  • sudo nft add rule inet filter input tcp dport 22 accept

    Allows SSH connections on port 22 in nftables.

  • policy drop;

    In nftables, it's convenient to set the drop policy in the configuration file, inside the input chain definition, together with the SSH and existing-connection rules. Then check the file and load it, as explained under saving and loading rules.

Opening ports

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

    Opens a TCP port, here 443 for HTTPS. The --dport option is available after you give a protocol, for example -p tcp or -p udp.

  • sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

    Opens several ports in one rule, up to 15 ports.

  • sudo iptables -A INPUT -p tcp --dport 6000:6007 -j ACCEPT

    Opens a port range, including the first and last ports. In iptables, the range is written with a colon.

  • sudo iptables -A INPUT -p udp --dport 1194 -j ACCEPT

    Opens a UDP port, here 1194.

  • sudo nft add rule inet filter input tcp dport 443 accept

    Opens a TCP port in nftables.

  • sudo nft add rule inet filter input tcp dport { 80, 443 } accept

    Opens several ports in one rule, with a set in curly braces.

  • sudo nft add rule inet filter input tcp dport 6000-6007 accept

    Opens a port range. In nftables, the range is written with a hyphen.

  • sudo nft add rule inet filter input udp dport 1194 accept

    Opens a UDP port in nftables.

Rules by IP address

  • sudo iptables -A INPUT -s 203.0.113.10 -p tcp --dport 22 -j ACCEPT

    Allows SSH from one IP address. For the restriction to work, don't leave a rule that opens port 22 to everyone. Give an IP address rather than a hostname, because a hostname is resolved to an address only once, when the rule is added.

  • sudo iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT

    Allows access to MySQL, on port 3306, only from an internal network in CIDR notation.

  • sudo iptables -A INPUT -m iprange --src-range 198.51.100.10-198.51.100.20 -j DROP

    Drops traffic from an address range that isn't a whole CIDR network.

  • sudo nft add rule inet filter input ip saddr 203.0.113.10 tcp dport 22 accept

    Allows SSH from one IPv4 address in nftables.

  • sudo nft add rule inet filter input ip saddr 192.168.1.0/24 tcp dport 3306 accept

    Allows access to port 3306 only from an internal network.

  • sudo nft add rule inet filter input ip6 saddr 2001:db8::/32 tcp dport 22 accept

    In the same inet family table, a rule for IPv6 addresses is written with ip6 saddr.

Blocking: DROP and REJECT

  • sudo iptables -I INPUT -s 198.51.100.7 -j DROP

    Blocks an IP address. The -I option inserts the rule at the top of the chain, so the block applies before rules that allow traffic. With -A, the rule is added at the end, and it won't take effect if an earlier rule already accepted the packet.

  • sudo iptables -A INPUT -p tcp --dport 23 -j REJECT --reject-with tcp-reset

    Rejects TCP connections to the port and sends the sender a TCP RST, so the connection fails immediately instead of waiting.

  • sudo iptables -A INPUT -j REJECT

    At the end of the chain, rejects everything that wasn't accepted earlier. Without --reject-with, an ICMP port-unreachable message is sent. Caution: a rule added after it with -A is never checked, so add new rules with -I.

  • sudo nft insert rule inet filter input ip saddr 198.51.100.7 drop

    Blocks an IP address in nftables. The insert command puts the rule at the top of the chain, and the add command adds it at the end.

  • sudo nft add rule inet filter input tcp dport 23 reject with tcp reset

    Rejects TCP connections to the port and sends the sender a TCP RST.

Placing and deleting rules

  • sudo iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT

    Inserts a rule at a specific position in the chain, here 1, and the rules after it move down one place. Without a number, the rule goes to the top of the chain.

  • sudo iptables -D INPUT 3

    Deletes the rule at position 3 in the INPUT chain. The sudo iptables -L INPUT -n --line-numbers command shows the numbers. After a deletion, the numbers of the following rules change, so check again before deleting another rule.

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

    Deletes a rule by its wording, which must match the rule that was added. It's easiest to copy the wording from the output of sudo iptables -S.

  • sudo nft delete rule inet filter input handle 5

    Deletes a rule by its handle. The sudo nft -a list chain inet filter input command shows the handles.

  • sudo nft insert rule inet filter input handle 5 ip saddr 198.51.100.7 drop

    Inserts a rule before the rule with handle 5.

  • sudo nft add rule inet filter input handle 5 tcp dport 8443 accept

    Adds a rule after the rule with handle 5.

Deleting all rules

  • sudo iptables -F

    Deletes all the rules in all the chains of the filter table. Caution: the command doesn't change the policy, and if the INPUT policy is DROP, you're locked out of the remote server at once. So first run sudo iptables -P INPUT ACCEPT.

  • sudo iptables -t nat -F

    Deletes all the rules in the nat table, including masquerading and port forwards.

  • sudo nft flush chain inet filter input

    Deletes all the rules in the chain, and the chain itself stays, with its policy. Caution: when the policy is drop, you're locked out of the server.

  • sudo nft delete table inet filter

    Deletes the table with all its chains and rules.

  • sudo nft flush ruleset

    Deletes all the tables and all the rules, including tables other tools created, such as firewalld. Caution: use it only when you're rebuilding the whole firewall.

Saving and loading rules

  • sudo iptables-save

    Prints all the active rules in a format you can load again. Rules added with commands are kept in memory only and are lost when the server reboots, until you save them to a file that's loaded at boot.

  • sudo iptables-save -f /root/iptables.rules

    Saves the IPv4 rules to a file. For IPv6 rules, use ip6tables-save the same way.

  • sudo iptables-restore -t /root/iptables.rules

    Checks the file and builds the rules from it without actually loading them. Run it before loading.

  • sudo iptables-restore /root/iptables.rules

    Loads the rules from the file, replacing the existing rules in the tables the file contains. With -n, the existing rules are kept.

  • sudo apt install iptables-persistent

    On Debian and Ubuntu: installs the mechanism that loads the saved rules at boot, from the /etc/iptables/rules.v4 and /etc/iptables/rules.v6 files.

  • sudo netfilter-persistent save

    Saves the active IPv4 and IPv6 rules to the files that are loaded at boot. Run it after every change you want to keep.

  • sudo sh -c 'nft list ruleset > /root/nftables-backup.nft'

    Saves all the nftables rules to a backup file. The redirection runs inside sh -c because in a plain sudo command, the redirection runs with your user's permissions, not with root's.

  • sudo nft -c -f /etc/nftables.conf

    Checks the configuration file without loading it. On Debian, this is the file the rules are loaded from at boot.

  • sudo nft -f /etc/nftables.conf

    Loads the rules from the file. Caution: when the file starts with flush ruleset, all the existing rules are deleted before loading, including rules from other tools.

  • sudo systemctl enable nftables.service

    On Debian: sets the nftables service to start at boot and load the rules from the /etc/nftables.conf file.

NAT and port forwarding

  • sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

    Traffic that leaves through the eth0 interface goes out with the interface's address, so computers on an internal network reach the internet through the server. According to the documentation, MASQUERADE is meant for a dynamic address, and for a static address, SNAT with --to-source is the right fit.

  • sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.20:80

    Forwards connections that arrive on port 8080 to an internal server on port 80. For forwarding to work, you also need packet forwarding (IP forwarding) enabled in the kernel and a rule that allows the traffic in the FORWARD chain.

  • sudo nft add table inet nat

    Creates a table named nat for address translation rules.

  • sudo nft add chain inet nat prerouting '{ type nat hook prerouting priority dstnat; }'

    Creates a chain for changing the destination address before the packet is routed. Port forwarding is set up in this chain.

  • sudo nft add chain inet nat postrouting '{ type nat hook postrouting priority srcnat; }'

    Creates a chain for changing the source address after the packet is routed. Masquerading is set up in this chain.

  • sudo nft add rule inet nat postrouting oif eth0 masquerade

    The nftables equivalent of MASQUERADE on the eth0 interface.

  • sudo nft add rule inet nat prerouting iif eth0 tcp dport 8080 dnat ip to 192.168.1.20:80

    The nftables equivalent of forwarding port 8080 to an internal server. In an inet family table, give ip or ip6 after dnat.

Moving from iptables to nftables

  • sudo iptables-translate -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT

    Converts an iptables command to the equivalent nft command and prints it, without changing the firewall.

    Example: nft add rule ip filter INPUT tcp dport 22 ct state new counter accept

  • sudo sh -c 'iptables-restore-translate -f /root/iptables.rules > /root/ruleset.nft'

    Converts a file saved with iptables-save to an nftables rules file. Some iptables extensions aren't supported in the conversion, so check the result with sudo nft -c -f /root/ruleset.nft before loading it.

Before changing the firewall on a remote server

  • SSH first, then DROP: before you set a DROP policy, add rules for the loopback interface, existing connections, and SSH, and check them in the output of sudo iptables -S or sudo nft list ruleset.
  • Keep a session open: after changing rules, keep your current SSH session open and test logging in from a new window. This way, you can fix a mistake without losing access to the server.
  • Rules stay in memory until you save them: rules added with commands are lost when the server reboots. If an unsaved change blocked your access, rebooting from the server's console restores the saved rules. Save the rules only after testing.
  • IPv4 and IPv6: iptables rules apply to IPv4 only, and IPv6 rules use ip6tables. In nftables, a table in the inet family handles both protocols.

iptables, nftables, UFW, and firewalld

nftables is the newer framework for filtering traffic in the Linux kernel, and it replaces iptables. On current distributions, the iptables command is usually the iptables-nft version, which creates the rules in nftables. UFW and firewalld manage the firewall themselves, so on a server where one of them is active, make your changes through it rather than adding direct rules alongside it. A quick check: sudo ufw status and sudo firewall-cmd --state. Their commands are on the UFW commands and firewalld commands pages.

The IP addresses on this page, such as 203.0.113.10 and 198.51.100.7, belong to ranges reserved for documentation; replace them with your real addresses, and replace the interface name eth0 with the name of the interface on your server. Network notation such as 192.168.1.0/24 is explained in the CIDR table, and the private and reserved ranges are listed on the reserved IP ranges page.

Did you know?

On a Linux VPS, you connect over SSH with full root access and set up the server's firewall the way that suits you: direct iptables or nftables rules, or management with UFW or firewalld. Every command on this page is available to you, from rules by IP address to port forwarding.

Linux VPS plans

iptables and nftables FAQ

How do I open a port in iptables and nftables?

In iptables, add a rule to the INPUT chain, for example sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT. If the chain ends with a rule that rejects everything else, insert the new rule before it with -I. In nftables, the equivalent rule is sudo nft add rule inet filter input tcp dport 443 accept, once the table and chain exist. In both cases, the rule takes effect right away, and to keep it after a reboot, save the rules.

How do I save iptables rules so they persist after a reboot?

On Debian and Ubuntu, install the iptables-persistent package, and after every change, run sudo netfilter-persistent save. The rules are saved to the /etc/iptables/rules.v4 and /etc/iptables/rules.v6 files and loaded when the server boots. In nftables, save the rules in the /etc/nftables.conf file and enable the service with sudo systemctl enable nftables.service.

What's the difference between iptables and nftables?

nftables is the newer framework for filtering traffic in Linux, and it replaces iptables. In nftables, one command, nft, manages IPv4 and IPv6 rules together in an inet family table, and the syntax is shorter and more flexible, for example a set of ports in one rule. Moving over is simple: the iptables-translate command converts an existing rule to nft syntax, and the iptables-restore-translate command converts a whole rules file.

How do I allow SSH only from a specific IP address?

In iptables, add the rule sudo iptables -A INPUT -s 203.0.113.10 -p tcp --dport 22 -j ACCEPT, and in nftables, the rule sudo nft add rule inet filter input ip saddr 203.0.113.10 tcp dport 22 accept, with your own address. After you test logging in from that address in a new window, delete the general rule that opens port 22 to everyone. First make sure the address you connect from is static, so access to the server stays available.

What's the difference between DROP and REJECT?

DROP discards the packet without a reply, and the other side waits until the connection times out. REJECT refuses the packet and sends an error message back to the sender, so the connection fails immediately. DROP is common for blocking unwanted traffic from the internet, and REJECT is handy on an internal network, where a fast reply saves needless waiting.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement