iptables and nftables rule generator

Build a Linux firewall by choosing policies, services, NAT and port forwards, and get iptables commands, an iptables-restore file or an nftables

loading iptables and nftables rule generator
About this tool

What does an iptables and nftables rule generator do?

A Linux server that faces the internet needs a firewall. Writing one means remembering the order of rules, the connection-tracking states, the separate ip6tables copy for IPv6, and the modules for rate limits and SSH protection. A single wrong line can block your own SSH session. This tool builds the firewall from choices you make on a form and writes it in three forms: iptables commands in a shell script, an iptables-restore file, or an nftables ruleset.

You choose the default action for incoming, forwarded and outgoing traffic, tick the basics (loopback, replies to your own connections, dropping invalid packets, limited ping, logging), add the services you run, and optionally set up NAT and port forwarding for a router or VPN server. The rules follow the iptables and nft manuals, and the tool's tests load the output into real iptables and nft in a container.

Everything is built in your browser. Your port list, internal addresses and interface names are not uploaded. A firewall is easy to get wrong, so read the output before you run it, and always test over a second SSH session. For the services you open, the port numbers list helps you find the right port, and the random port generator can pick an unused one.

How to generate firewall rules

  1. In Basics, set the default for Incoming (INPUT), Forwarded (FORWARD) and Outgoing (OUTPUT). The form starts with DROP, DROP and ACCEPT, which is a closed server that can still reach out.
  2. Keep the checks you want: Allow loopback, Allow replies to connections you started, Drop invalid packets, Allow ping (limited), Log what is dropped, Also write IPv6 rules.
  3. To slow down SSH brute-force attacks, tick that option and set the SSH port and the number of new connections per number of seconds. The default is 4 per 60.
  4. In Services and rules, add a rule for each service. Each has a name, a direction (Incoming or Outgoing), a protocol (TCP, UDP or both), ports, an optional address or block, an action (ACCEPT, DROP or REJECT) and an optional limit such as 10/minute. The form starts with SSH on 22 and Web on 80 and 443.
  5. To use the machine as a router, open NAT and port forwarding, tick the router option, write the internet interface (for example eth0) and optionally the local one. Add port forwards with the outside port, destination address and inside port.
  6. Choose the output format: iptables commands, iptables-restore or nftables. Read the warnings, then copy the result or download it.

The result loads into memory and is lost at reboot. To keep it, save with iptables-save and ip6tables-save (or netfilter-persistent on Debian and Ubuntu), or load the nftables file from /etc/nftables.conf.

Examples

The default firewall as iptables commands

With the starting settings, the IPv4 part reads like this (the IPv6 half repeats it with ip6tables, and uses -p ipv6-icmp instead of the ping rule):

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 5/second --limit-burst 10 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 --name SSH -j DROP
iptables -A INPUT -p tcp --dport 22 -m comment --comment SSH -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 80,443 -m comment --comment Web -j ACCEPT
iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix 'iptables-dropped: ' --log-level 4
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

The policies come last on purpose, so flushing the rules over SSH never leaves the machine closed halfway.

The same firewall as an iptables-restore file

IPv4 only, without SSH protection and logging:

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A FORWARD -m conntrack --ctstate INVALID -j DROP
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp --icmp-type echo-request -m limit --limit 5/second --limit-burst 10 -j ACCEPT
-A INPUT -p tcp --dport 22 -m comment --comment SSH -j ACCEPT
-A INPUT -p tcp -m multiport --dports 80,443 -m comment --comment Web -j ACCEPT
COMMIT

The nftables version of the rules

chain input {
    type filter hook input priority filter; policy drop;
    iif "lo" accept
    ct state invalid drop
    ct state established, related accept
    tcp dport 22 accept comment "SSH"
    tcp dport { 80, 443 } accept comment "Web"
}

This is an excerpt: the file also starts with flush ruleset and sits in table inet filter, which covers IPv4 and IPv6 together.

A router with a port forward

With masquerade on eth0, local interface br0, and outside port 8080 sent to 192.168.1.10:80, the NAT rules are:

iptables -A FORWARD -i br0 -o eth0 -j ACCEPT
iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 80 -m conntrack --ctstate NEW -j ACCEPT
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

The script then reminds you to run sysctl -w net.ipv4.ip_forward=1.

Common errors and how to fix them

  • Rule N: ports must be numbers from 1 to 65535, separated by commas, or a range like 8000:8100 (at most 15 ports). Fix the port list. A rule for both TCP and UDP needs ports.
  • Rule N: the address must be an IPv4 or IPv6 address or block. Write 10.0.0.0/8 or an IPv6 block. A rule with an IPv4 address goes only to the IPv4 half.
  • Rule N: the limit must look like 10/minute. Use second, minute, hour or day.
  • The interface names must be 1 to 15 letters, digits or the characters . _ : + -.
  • Port forward N: the destination must be an IPv4 address.
  • Incoming is set to DROP and no rule allows SSH. This is a warning, the rules are still written, but if you run this over SSH you will lose the connection. Add an SSH rule.
  • Outgoing is set to DROP. Replies are allowed, but DNS, updates and every other outgoing connection need their own outgoing rule.

Frequently asked questions

Is it safe to apply the rules over SSH?

Only if an SSH rule is in the list. Open a second SSH session first, apply the rules, and check that a new connection works before you close the first.

Do iptables and nftables rules do the same thing?

For the choices on the form, yes. The nftables output is one inet table that handles IPv4 and IPv6, while iptables needs separate IPv4 and IPv6 commands.

Why is ICMPv6 always allowed?

In the IPv6 rules it is always allowed, because IPv6 needs it for neighbour discovery and path MTU discovery. Blocking it breaks IPv6 connectivity.

Do the rules survive a reboot?

No. Save them with iptables-save and ip6tables-save, or put the nftables file in /etc/nftables.conf.

Is anything uploaded?

No. The rules are written in your browser.

How to run the result

Bash

sudo sh firewall.sh                 # the iptables commands
sudo iptables-restore < rules.v4    # the restore file
sudo nft -f firewall.nft            # the nftables file
sudo iptables -S                    # list what is loaded
Report a problem