Preserve Firewall Rules Across Server Reboots (nftables edition)
Premium Challenge
Upgrade your membership to unlock this and all other premium materials.
Firewall rules added with the nft command exist only in the kernel's memory.
When the server reboots, the rules disappear, and the ports they protect become exposed again.
This server runs an HTTP service on port 8080 that should be accessible only from the server itself.
A colleague has blocked access to the port from the network with the following commands,
as in the Protect an Unintentionally Exposed Server Port with Firewall Rules challenge:
sudo nft add table inet firewall
sudo nft add chain inet firewall input '{ type filter hook input priority filter; policy accept; }'
sudo nft add rule inet firewall input iif lo tcp dport 8080 accept
sudo nft add rule inet firewall input tcp dport 8080 drop
The rules work now, but the next reboot of the server will remove them.
Your task is to make the nftables rules persistent, so that port 8080 stays protected after the server reboots.
When the rules are persistent, reboot the server with sudo reboot.
Don't stop or reconfigure the http-server service. Only the firewall configuration should change.
The port must stay protected at all times, including while you change the firewall configuration and while the server reboots.
Hint 1
The nft list ruleset command prints the current rules in the format that the nft -f <file> command loads:
sudo nft list ruleset
Saving the rules to a file is only half of the job. Something also has to load the file every time the server boots, before the services start accepting connections.
Hint 2
The nftables package, which provides the nft command, also ships a systemd service that loads the /etc/nftables.conf file at boot.
Check whether the service is enabled and what the file contains:
systemctl status nftables
cat /etc/nftables.conf
What would loading the current file do to your colleague's rules?
Hint 3
If the port is still exposed after the reboot, check which rules the kernel has now:
sudo nft list ruleset
Then look at the log of the service that should have loaded them during the boot with journalctl -b -u <service-name>.