Skip to main content
To allow a port with iptables, run sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT. With nftables, write the rule into /etc/nftables.conf, for example tcp dport 443 accept, then load it with sudo nft -f /etc/nftables.conf. Rules typed on the command line are lost at reboot, so save them to a file that loads at boot. nftables is the current Linux packet filter. iptables is the older command; on current distributions the iptables command is a compatibility layer (iptables-nft) that writes nftables rules. Red Hat lists iptables-nft as deprecated in RHEL 9 (Red Hat documentation), so prefer nftables for new rulesets.

Before you start

  • Most people should use a front end instead: UFW on Ubuntu/Debian or firewalld on AlmaLinux/Rocky. Use raw rules only if you do not run either of them, because they overwrite each other.
  • You need root or a sudo user. Root users can leave out sudo.
  • Open the VNC console in a browser tab before you start. On a dedicated server, open a ticket if you need console access.
A ruleset with policy drop blocks everything you have not allowed, including SSH. Always allow established connections, loopback and your SSH port in the same file, and load it from a session you can recover from.

Should I use nftables or iptables?

Convert an old iptables rule to nftables syntax with iptables-translate:

How to set up a basic nftables firewall

1

Install nftables

2

Write the ruleset

Open the file that loads at boot: /etc/nftables.conf on Ubuntu/Debian. On AlmaLinux/Rocky, create /etc/nftables/main.nft and add include "/etc/nftables/main.nft" to /etc/sysconfig/nftables.conf.
Change 22 if SSH listens on another port. Add one tcp dport or udp dport line for each service you run.
3

Check the syntax without applying it

No output means the file is valid.
4

Load the rules and enable them at boot

5

Test SSH from a second session

Log in from a new terminal before you close the current one.
flush ruleset deletes every existing rule first, including rules added by Docker, Fail2Ban or another tool. Restart those services after you load the file.

How to allow a port with nftables on the command line

Add a rule to the running ruleset (lost at reboot unless you also add it to the file):
Show the current rules with handles, then delete one by its handle:

How to allow a port with iptables

-A appends the rule to the end of the chain. If an earlier rule drops the traffic, insert it at the top instead with -I INPUT 1. A complete basic ruleset in iptables-restore format looks like this:
Test a file in this format without applying it: sudo iptables-restore --test rules.v4.

How to save iptables rules so they survive a reboot

Rules are saved to /etc/iptables/rules.v4 and /etc/iptables/rules.v6 and loaded at boot.

How to check it worked

Then test the port from another computer; see check if a port is open from outside.

Frequently asked questions

Why did my rules disappear after a reboot?

Rules added with nft add or iptables -A live only in memory. Put them in /etc/nftables.conf (and enable the nftables service) or save them with netfilter-persistent save.

Can I use nftables and UFW together?

Not with flush ruleset in your file, which removes UFW’s rules. Pick one tool to manage the firewall.

How do I undo a rule that locked me out?

Log in through the VNC console and run sudo nft flush ruleset, which removes all rules until the next reload. See fix being locked out after a firewall change.

Do I still need ip6tables?

With iptables, yes: IPv6 traffic ignores iptables rules. With nftables, an inet table covers both.

VPS and dedicated server guides

Every guide in this section.

Set up UFW

The simple option for Ubuntu.

Set up firewalld

The default on AlmaLinux and Rocky.

Port forwarding and NAT

Forward ports to another host.

Locked out after a firewall change

Recover access.

Checked against the official nftables wiki and Red Hat nftables documentation on 4 November 2026; the nftables ruleset (nft -c), the iptables ruleset (iptables-restore --test) and iptables-translate were tested on Ubuntu 24.04 LTS (nftables 1.0.9, iptables 1.8.10). Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026