To allow SSH through UFW on Ubuntu, add a rule for the port your SSH server actually uses before enabling the firewall. Keep your current session open, confirm the saved rule, and test a new login after enabling UFW.
The OpenSSH application profile may cover a default installation; a custom SSH port needs a matching rule. The steps below show how to choose between them, check the configuration, and preserve a recovery path if access fails.
Key takeaways
- Add the SSH rule before enabling UFW on a remote VPS.
- Use
sudo ufw allow OpenSSHwhen the OpenSSH application profile matches your server. - Use a port-specific rule such as
sudo ufw allow 2222/tcpif SSH listens on a custom port. - Keep the current SSH session open while testing a second session.
- Confirm
sudo ufw status verbosebefore and after enabling the firewall. - For related access tasks, read the Virtarix guides to generating SSH keys, securing a VPS, VPS security, and systemctl basics.
Before you enable UFW
Do these checks first:
- Confirm you are logged in over SSH and the session is stable.
- Confirm which port SSH actually listens on.
- Decide whether you will use the OpenSSH application profile or a port rule.
- Add the rule.
- Enable UFW only after the rule exists.
- Test a new SSH login before closing the original session.
If you are working through a hosting console, keep that console available too. It gives you a recovery path if you make a firewall mistake.
Step 1: check UFW status
Run:
sudo ufw status verbose
If UFW is inactive, that is normal on many fresh Ubuntu servers. You can still add rules before enabling it. If UFW is already active, be more careful: changing rules can affect live traffic immediately.
When UFW is active, verbose status shows its rules and default policies. When it is inactive, status does not list the saved rules; use sudo ufw show added to inspect those before enabling it. Save the relevant output in your maintenance notes.
Step 2: check available application profiles
Ubuntu packages can provide UFW application profiles. Check whether OpenSSH is listed:
sudo ufw app list
If OpenSSH appears, inspect it with sudo ufw app info OpenSSH and compare its port with the actual SSH listener. Allow the profile only when they match. A profile does not automatically follow a custom port configured in the SSH service.
Step 3: allow SSH with the OpenSSH profile
Run:
sudo ufw allow OpenSSH
This adds a rule for the OpenSSH application profile. It is clearer than a bare service name because it matches the profile name shown by ufw app list.
Before enabling UFW, inspect the saved rule:
sudo ufw show added
Look for the OpenSSH allow rule in the saved configuration. If UFW is already active, also check sudo ufw status verbose to inspect the running firewall.
Step 4: allow a custom SSH port instead
If you changed SSH to a custom port, allow that port explicitly. For example:
sudo ufw allow 2222/tcp
Replace 2222 with your real SSH port. Do not copy a custom-port example unless it matches the active SSH server configuration. If SSH listens on a different port, UFW will faithfully allow the wrong port and block the real one when the firewall becomes active.
After adding the rule, inspect the saved configuration:
sudo ufw show added
Confirm that the allow rule names your actual TCP port. When UFW is active, check sudo ufw status verbose as well.
Step 5: enable UFW safely
Before enabling UFW, confirm the SSH rule and any other inbound services the server must provide, such as HTTP and HTTPS. Enabling the firewall can affect those services too. Once the required rules and recovery access are ready, enable it:
sudo ufw enable
Read the warning carefully. On a remote VPS, enabling the firewall can disrupt existing connections if rules are wrong. Because you added the SSH rule first, the risk is lower, but you still need to test.
After enabling, check status again:
sudo ufw status verbose
Confirm that UFW is active and the SSH rule remains allowed.
Step 6: test a second SSH session
Do not close the original terminal yet. Open a new terminal and connect again using the expected port. If you use the default port, connect normally. If you use a custom port, specify that port in your SSH client.
A successful new login confirms access from that client over the tested address and port. Test other required paths, including IPv6 where used. If a login fails, keep the first session or recovery console open while you inspect the rules.