Write the rule plan first
UFW is a frontend for Linux firewall configuration. Before enabling it, identify the interface and actual port used by your current SSH session. The OpenSSH application profile normally describes port 22; it does not automatically track an SSH server moved to a different port.
Inspect current exposure
sudo ufw status verbose
sudo ss -lntp
sudo sshd -T | sed -n '/^port /p'Also consider socket activation and any host or cloud firewall. Keep a working SSH session open and confirm you can reach a console if the new rules are wrong.
Allow management before enabling
For a server really using TCP 22, this example allows SSH first:
sudo ufw allow 22/tcp comment 'SSH management'
sudo ufw default deny incoming
sudo ufw default allow outgoingIf your real port is different, substitute it before running the allow command. A known fixed administrator source can be restricted further with a from-address rule, but do not use a source range that excludes your actual tunnel, VPN or management connection.
Add only the other inbound services required by the server. A local-only web service behind a tunnel may not need public inbound port 80 or 443. Docker networking can have its own firewall interactions; do not assume a UFW policy alone describes container exposure.
Enable and test
sudo ufw enable
sudo ufw status numberedRead the enable warning. Open a new SSH connection from the normal management computer while leaving the first session open. Test required application paths from their intended clients and check an unapproved path remains blocked where expected.
Change or remove a rule carefully
The numbered list is useful for removal, but numbers can change after each deletion. Re-list it before another deletion. Do not remove the active SSH allowance until its replacement has been verified in a new session.
Common mistakes
A successful existing SSH session may survive a bad rule because established connections are treated differently. That is why a second new connection matters. Ping success is not a TCP service test. UFW enablement is not a full routing audit or a substitute for application authentication.
Recovery
If new access fails, use the retained session or recovery console to inspect and correct the rules. Avoid a permanent all-ports workaround. Keep the final rule set documented alongside the services it protects.
APPLIED FIELD GUIDE · NOVEMBER 2026
A reversible firewall change in four evidence checkpoints
An SSH allow rule alone may not be sufficient when a cloud security group, a site router, an overlay network or an application listener imposes a second restriction. Conversely, allowing a port in UFW does not mean the service should be publicly reachable. Identify the complete path before deciding which inbound traffic belongs in the rule set.
- Before: Confirm the remote client's real source IP, the server's SSH port, whether SSH listens on the correct address, and the console recovery route. Capture
sudo ufw status numberedand the relevant cloud firewall rules. - Prepare: Add the narrow management allow rule for the actual interface/source before switching on the deny-by-default policy. Do not rely on a dynamic client IP without considering how to recover when it changes.
- Change: Keep the original session open during activation. Use a separate connection from the intended remote network to test a complete new sign-in. Do not count an already-established session as a successful test.
- After: Confirm authorised services remain reachable and unauthorised paths are denied using a lab or approved test source. Record the final rule ordering and the name of the operator who verified access.
Worked example: management restricted to a VPN
If an administrator manages a server exclusively through a verified private VPN, an allow rule can target the relevant VPN interface and specific client address instead of exposing SSH to the public internet. However, interface names, route ownership and actual client addresses vary. Verify them with read-only inspection before writing a rule; blindly copying an example interface such as tailscale0 or wt0 may silently block the real management path.
Rollback is part of the design
Have out-of-band console access, a saved copy of the previous rules and a bounded change window. Do not treat disabling the entire firewall as the default recovery method on a production server. The objective is to restore a known authorised management path while retaining as much protection as possible.
Official references
Consult the current vendor documentation if your version or screen differs.
Documentation-based draft. Commands have not all been executed against the named products in a lab. Validate configuration examples against your installed version before changing a working system.