Before you begin
- An original working session, a separately tested key-based sudo account, and verified independent recovery access.
- A current recoverable backup and an inventory of required listeners and external access-control rules.
- Authority to change guest firewall rules; authorized root users omit sudo from the examples.
Prove administrative access before restricting anything
Confirm the installed release and your authority before applying Ubuntu-specific instructions. Keep the original connection open throughout the work. In a separate terminal, test a new key-based login to the intended non-root account, then run sudo -v and sudo id -u; the latter must report 0.
Do not disable password authentication or root login until that independent key-based account and sudo both work. This guide does not make those changes. Confirm an independent recovery route with the responsible administrator; a console is usable only if it actually exists and your access has been verified.
Identify actual listeners and every filtering layer
Inspect the guest before adding rules:
sudo ss -lntp
sudo sshd -t
sudo ufw status verboseReplace GUEST_SSH_PORT in later commands with the verified numeric SSH listener inside the guest. SSH_PORT means the client-facing external port; NAT can make these different. Never put the external value into a guest rule without verifying that it is also the actual listener. Socket activation means configuration text alone is not proof of the active port.
Check documented external ACLs and forwarding separately. If UFW is absent or another firewall manager owns the rules, stop rather than installing or replacing a firewall blindly.
Stage the narrow SSH rule and review existing policy
Only after verifying the listener and access inventory, stage the guest rule:
sudo ufw allow GUEST_SSH_PORT/tcp
sudo ufw show addedIf UFW is already active, rule changes apply immediately; preserve existing policy and test carefully. If it is inactive, this does not by itself enable filtering. Review configured defaults in /etc/default/ufw, saved rules, IPv6 behavior, routed traffic, and any container networking. Add only other explicitly required service rules after verifying their listeners. Do not assume an SSH rule preserves application availability or that UFW governs every container traffic path.
Enable an inactive firewall only after every gate passes
Do not enable remotely unless all gates pass: the original session remains open; a second key-based session and sudo work; the actual guest SSH rule and required application rules are reviewed; external ACLs and forwarding are understood; and independently verified recovery access is ready. If a usable recovery console or equivalent independent path is unavailable, leave the firewall state unchanged and arrange authorized recovery access first.
Only for a reviewed, currently inactive UFW configuration meeting those gates:
sudo ufw enable
sudo ufw status verboseRead the prompt rather than forcing confirmation. Immediately open another fresh client connection using SSH_PORT and verify sudo.
Recover narrowly if newly enabled filtering blocks access
Do not close preserved sessions until fresh SSH and required application checks pass. If access fails immediately after enabling a previously inactive UFW instance, use the verified independent recovery console to inspect its rules. As an authorized temporary rollback of that specific enablement, run:
sudo ufw disableDisabling UFW broadens exposure; investigate promptly and plan a corrected staged configuration. Do not use this as a blanket rollback when UFW was already active. Never reset UFW, flush the entire ruleset, or change SSH ports to recover from an unexplained problem. Preserve evidence of the original policy.
Verify security controls and keep a redacted record
Verify a fresh administrator login, sudo, intended application access, and the absence of unintended newly exposed listeners. Review operating-system updates and backup restoration separately; a firewall is not a substitute for either. Record the final UFW state, allowed guest services, external mapping, recovery route, and verification time.
For escalation, share a redacted before-and-after rule summary and the exact failing check. Remove public and private addresses, usernames, hostnames, account identifiers, and sensitive application details. Never include private keys, passwords, or raw configuration containing secrets. These commands are instructional examples, not evidence that security changes were tested on your VPS.
Final checklist
- A fresh key-based non-root login and sudo work while the original connection remains open.
- GUEST_SSH_PORT matches the actual guest listener; external SSH_PORT and ACLs were checked separately.
- Firewall ownership, defaults, required services, and independent recovery access are verified.
- UFW was enabled only after all gates passed, followed by a fresh connection and application checks.
- Rollback is specific and authorized; the security record and escalation evidence are redacted.
Sources & further reading
Discuss your requirements.
For a VPS proposal, share your workload, preferred region and resource needs. Availability, specifications and terms are confirmed separately; an inquiry does not place an order or provision a server.
Start an inquiry