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.
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
- Ubuntu / Debian
- AlmaLinux / Rocky
2
Write the ruleset
Open the file that loads at boot: Change
/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.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
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):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:
sudo iptables-restore --test rules.v4.
How to save iptables rules so they survive a reboot
- Ubuntu / Debian
- AlmaLinux / Rocky
/etc/iptables/rules.v4 and /etc/iptables/rules.v6 and loaded at boot.How to check it worked
Frequently asked questions
Why did my rules disappear after a reboot?
Rules added withnft 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 withflush 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 runsudo 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 ignoresiptables rules. With nftables, an inet table covers both.
Related guides
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:- Live Chat: Quick assistance via our website.
- Support Ticket: Open a Ticket
- Discord: Join our Community
- Email: support@freakhosting.com
Last Updated: November 2026