In this tutorial
- Where does the firewall run on this platform?
- Why start with nftables?
- What does a minimal ruleset look like?
- How do I load new rules and keep SSH reachable?
- How do you open a port for one service or one address?
- What about Docker and published ports?
- How do you do the same with ufw?
- And with firewalld on the RHEL family?
- How do you verify the result from outside?
Where does the firewall run on this platform?
Inside each instance. The console has no network firewall panel, which means every port your software opens is reachable on the public IPv4 and IPv6 until you filter it in the OS. That is good news for control: the rules live with the server, travel with your config management, and you can version them in git like any other file.
First see what is actually listening:
sudo ss -tulpnAnything bound to 0.0.0.0 or [::] is public. A database on 5432 or a Redis on 6379 showing up here is the classic mistake to fix before anything else, either by binding it to 127.0.0.1 or by dropping it at the firewall.
Why start with nftables?
nftables is the packet filter of current Linux kernels, and one table of type inet covers IPv4 and IPv6 together. ufw and firewalld both generate nftables rules underneath on recent releases, so writing the ruleset directly removes a layer and shows exactly what is enforced.
sudo apt-get install -y nftables
sudo systemctl enable nftables
nft --versionOn AlmaLinux, Rocky and Fedora the package is installed already but firewalld usually owns it; pick one tool, not both. On Alpine install nftables with apk and enable it with rc-update add nftables.
What does a minimal ruleset look like?
Save this as /etc/nftables.conf. It allows established traffic, loopback, ICMP needed for IPv6 to work, SSH, HTTP and HTTPS, and drops the rest of the inbound traffic.
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
ip protocol icmp icmp type echo-request limit rate 5/second accept
ip6 nexthdr icmpv6 accept
tcp dport 22 ct state new limit rate 15/minute accept
tcp dport { 80, 443 } accept
udp dport 443 accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}The rate limit on port 22 slows brute force scripts without bothering a human. The UDP 443 line is for HTTP/3; drop it if your web server does not speak QUIC. Accepting all ICMPv6 is deliberate: IPv6 neighbour discovery and path MTU rely on it.
How do I load new rules and keep SSH reachable?
Load the file with an automatic rollback, because a missing SSH line with no browser console means a reinstall. Save the current state, apply the new rules, and schedule a flush that you cancel only after you have reconnected.
sudo nft -c -f /etc/nftables.conf # syntax check only
sudo nft list ruleset > ~/nft-before.conf
sudo systemd-run --on-active=180 --unit=nft-undo nft flush ruleset
sudo nft -f /etc/nftables.confNow open a brand new SSH session from your computer. If it connects, cancel the undo timer:
sudo systemctl stop nft-undo.timerIf it does not connect, wait three minutes; the timer flushes the ruleset and you are back in. Once you are happy, sudo systemctl restart nftables confirms the file loads at boot.
How do you open a port for one service or one address?
Add a line in the input chain, reload, and verify. For a WireGuard tunnel on UDP 51820:
udp dport 51820 acceptFor an admin panel only your office should see, match the source address:
ip saddr 203.0.113.0/24 tcp dport 8443 accept
ip6 saddr 2001:db8:77::/48 tcp dport 8443 acceptTo add a rule live from the command line, run it with nft add rule, then copy it into the file so it survives a reboot:
sudo nft add rule inet filter input tcp dport 9100 ip saddr 203.0.113.7 accept
sudo nft list chain inet filter inputWhat about Docker and published ports?
Docker writes its own rules and published container ports skip your input chain, because that traffic goes through the forward path to the container. Two safe patterns:
- Publish only on localhost, such as
-p 127.0.0.1:8080:80, and put a reverse proxy like Caddy in front. - Do not run
flush ruleseton a host where Docker is active, or restart Docker right after loading your rules so it recreates its chains.
The Docker Engine tutorial covers this in more detail.
How do you do the same with ufw?
ufw is a friendly front end on Ubuntu and Debian. Allow SSH first, then enable it.
sudo apt-get install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verboseufw limit adds a connection rate limit similar to the nftables line above. IPv6 is covered as long as IPV6=yes is set in /etc/default/ufw, which is the default.
And with firewalld on the RHEL family?
firewalld ships enabled on AlmaLinux, Rocky Linux, CentOS Stream and Fedora. Work with services and make changes permanent:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --reload
sudo firewall-cmd --list-allSSH is allowed in the default public zone. Remove services you never use, such as cockpit or dhcpv6-client, to keep the list honest.
How do you verify the result from outside?
Test from another machine, never from the server itself, because local traffic skips the rules.
nmap -Pn -p 1-1024 198.51.100.24
nmap -6 -Pn -p 22,80,443 2001:db8:4f2a::24
nc -zv 198.51.100.24 5432Open ports should be exactly the ones you meant. A database port should report as filtered or time out. Repeat this scan after every new service you deploy.
Questions about this tutorial
Does the BitcoinVPS console filter traffic for me?
No. Filtering is done inside the OS with nftables, ufw, firewalld or pf on FreeBSD, which keeps the rules next to the services they protect.
Do I need separate rules for IPv6?
Not with an nftables table of family inet, which covers both. With iptables you would need ip6tables as well, which is one reason to prefer nftables.
Can I block outgoing traffic too?
Yes, by setting the output chain policy to drop and allowing DNS, NTP, package mirrors and your own services. It is stricter but takes more care.
Is outgoing mail on port 25 allowed?
Do not plan around it. Open a support ticket and ask before you build anything that sends mail directly from the instance.
What is the FreeBSD equivalent?
pf. Enable it in /etc/rc.conf with pf_enable="YES", write /etc/pf.conf with block in all and pass rules for your ports, and load it with pfctl -f /etc/pf.conf.