Before you start
This page can leave the machine unreachable over SSH. Confirm your fallback access works before you begin. If the machine has a BMC — IPMI, iDRAC, or iLO — the BMC console is your fallback. Log in to it now to confirm it works. If the machine has no BMC, which is common on consumer and workstation boards, the fallback is a monitor and keyboard attached to the machine. If you cannot reach the machine physically, schedule an automatic undo. This disables the firewall in ten minutes:sudo systemctl stop undo-firewall.timer. If the change locks you out instead,
the timer runs and you can reconnect.
See Security Hardening
for the full explanation.
An SSH tunnel to the BMC is not a fallback. A tunnel requires a working SSH
connection, which is what the steps below can break. Reach the BMC over its own
network connection.
A firewall on a host machine prevents services you did not intend to publish from being reachable from the internet.
What ufw covers here
Instances are Docker containers with published ports, and Docker’s own documentation states that this traffic does not pass through ufw:When you publish a container’s ports using Docker, traffic to and from that container gets diverted before it goes through the ufw firewall settings.ufw therefore governs services listening on the host itself, such as sshd and anything else you installed. It does not filter client instance ports in either direction. This has two consequences:
- Enabling ufw will not disconnect running instances.
ufw statusdoes not report what the internet can reach. Only a scan from outside the network reports that.
These steps use
ufw because it ships with Ubuntu Server. If you run nftables,
firewalld, or raw iptables, apply the same rules there. The checks below still
report whether the result is correct.Check what is running now
Before changing anything, check whether the machine already has a firewall:Work out the ports you forwarded
The requirement is 5 forwarded ports per GPU minimum, 100 per GPU recommended. Count the GPUs in the machine:
The range to allow is the one configured on your router to forward to this
machine. If you do not know it, get it from your router’s port-forwarding
configuration before continuing.
Add the rules
Allow your own SSH first. Check which port sshd is on rather than assuming 22:limit allows the connection but blocks a source address that opens six or
more connections in thirty seconds.
Then allow the forwarded range. The protocol is required whenever you specify a
range rather than a single port:
Set the default policy and enable
Verify
If the new connection succeeds, the firewall is configured. If it hangs, use
your fallback access to run
sudo ufw disable and start again. See
Recovery for the full reset.