Before you start
- Use only one firewall manager. Do not manage rules with nftables directly while UFW or firewalld is active. If you prefer a simpler tool, see UFW (Uncomplicated Firewall).
- Software such as Docker, WireGuard, or VPN panels may add their own rules. The example below starts with
flush ruleset, which removes those rules until the software adds them again.
Install and inspect nftables
Write a basic ruleset
Back up the current configuration file:/etc/nftables.conf:
inet table:
22 with your actual SSH port. Add or remove service ports as needed. Keep the ICMP rule because IPv6 requires ICMPv6 to work.
The
forward chain drops routed traffic. If the VPS routes traffic for a VPN or containers, add forward rules for that traffic or use the rules your VPN guide provides.Check and apply the ruleset
Check the syntax without applying it:Restrict a service to a trusted address
For example, replace the SSH rule with one that allows SSH only from a known administrator address:ip6 saddr for an IPv6 address. Remove the broader tcp dport 22 accept rule only after you have tested access from the trusted address.
Recover from a blocked connection
- Log in through the VNC console.
- Correct
/etc/nftables.conf, or restore your backup withsudo cp /etc/nftables.conf.bak /etc/nftables.conf. - Reload the rules with
sudo nft -f /etc/nftables.conf.