GrepBoard/Manuals
605 manuals ·

Allow SSH in ufw before you enable the firewall

·Tested on Ubuntu 24.04 · based on the official ufw docs · 2026-10·Test scope ↓·8 steps

Running ufw enable first over SSH can lock you out. Add the SSH rule, check pending rules, enable, read status verbose, and recover from a lockout.

If you run ufw enable over SSH before adding an SSH rule, the firewall comes up without one and new connections are refused. Add the rule first, then enable. ufw accepts rules while it is still off.

[1] Check the current state. ufw ships disabled, and while it is off status prints a single line.

console
$ sudo ufw statusStatus: inactive

The default policies live in /etc/default/ufw. These are the values the package ships. Incoming traffic is dropped and outgoing is allowed, so every port without an allow rule is blocked the moment you enable.

console
$ grep POLICY /etc/default/ufwDEFAULT_INPUT_POLICY="DROP"DEFAULT_OUTPUT_POLICY="ACCEPT"DEFAULT_FORWARD_POLICY="DROP"DEFAULT_APPLICATION_POLICY="SKIP"

[2] Find the port sshd actually listens on. The default is 22, but many servers use another. That is the port you must allow.

console
$ sudo ss -tlnp | grep sshd

[3] Allow SSH. The openssh-server package installs an OpenSSH profile in /etc/ufw/applications.d/openssh-server, and it covers TCP 22.

/etc/ufw/applications.d/openssh-server
[OpenSSH]title=Secure shell server, an rshd replacementdescription=OpenSSH is a free implementation of the Secure Shell protocol.ports=22/tcp

Allow it by profile name or by port number. Do not add a protocol when you use a profile name (man ufw).

console
$ sudo ufw allow OpenSSHRules updatedRules updated (v6)

To use the port instead, run sudo ufw allow 22/tcp. With IPv6 enabled (IPV6=yes in /etc/default/ufw, the default), one rule covers both IP versions, hence the (v6) line.

[4] Review the rules you added before enabling. show added lists the rules as they were typed on the command line. The firewall is not running yet, so status is the wrong command here.

console
$ sudo ufw show addedAdded user rules (see 'ufw status' for running firewall):ufw allow OpenSSH

[5] Enable the firewall. Inside an SSH session, ufw asks first. Enabling flushes and rebuilds its chains, which may drop existing connections, but with the rule from step 3 in place new connections get through (man ufw, REMOTE MANAGEMENT).

console
$ sudo ufw enableCommand may disrupt existing ssh connections. Proceed with operation (y|n)? yFirewall is active and enabled on system startup

[6] Read the result with status verbose. The first four lines have this shape.

output
Status: activeLogging: on (low)Default: deny (incoming), allow (outgoing), disabled (routed)New profiles: skip

Below them, a To · Action · From table lists one rule per line.

FieldMeaning
Statusactive means on. When off, only inactive is printed
LoggingLog level. low by default
DefaultWhat happens to traffic no rule matches. With incoming deny, ports without an allow rule are blocked
ToThe destination: a port or profile on this server
ActionALLOW · DENY · REJECT · LIMIT. ALLOW OUT and ALLOW FWD are outgoing and routed rules
FromThe source. Anywhere is 0.0.0.0/0 (::/0 for IPv6)

Look for OpenSSH or 22/tcp with ALLOW in the To column. sudo ufw status numbered adds rule numbers.

[7] If you changed the SSH port, allow the new one. Keeping 22 allowed while sshd moves to 2222 leaves 2222 blocked. Allow the new port, confirm you can log in on it, then delete the old rule.

console
$ sudo ufw allow 2222/tcp$ sudo ufw delete allow OpenSSH

[8] If you are already locked out, SSH cannot help. Log in through the server's console (cloud provider console, IPMI, VM console), then run sudo ufw allow OpenSSH, or sudo ufw disable if you need it down right now. disable unloads the firewall and keeps it off at boot.

Analyze your auth.log (SSH) log in the browser ↗ — nothing is uploaded.

Check it with the ssh_config generator tool ↗ — calculated in your browser.

#ufw#firewall#ssh