DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
VGSources
Blog

How to Allow SSH Connections Through UFW on Ubuntu Safely

Add the SSH rule before enabling UFW on Ubuntu. This guide covers service-name and explicit TCP commands, custom ports, trusted-IP restrictions, rate limiting, dry runs, rule order, verification, deletion, and remote lockout recovery.
Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

On a remote Ubuntu server, add the SSH allow rule before enabling UFW. For the usual SSH service on TCP port 22, run sudo ufw allow ssh, verify the pending configuration, enable the firewall, and verify again. If SSH listens on another port, allow that port instead; when possible, restrict the rule to your trusted IP address or network.

What allowing SSH through UFW actually does

UFW is Ubuntu’s standard firewall configuration tool. An SSH rule tells UFW which inbound TCP connection should be accepted so that an SSH client can reach the server. The rule does not start the SSH daemon, change its configuration, create a user, or authenticate anyone; it only controls whether matching traffic is permitted through the firewall.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The familiar SSH service normally listens on TCP port 22. That is a convention, not a guarantee. An administrator may configure the daemon to listen on a different port, and the firewall rule must match the actual listener. UFW can identify the service by the service name ssh when that name is defined in /etc/services, or you can state the port and protocol explicitly.

The safe remote procedure

If you are connected over SSH, the order of operations matters. Add and inspect the rule first, then enable UFW. Enabling the firewall before permitting your management connection can terminate your access or leave future connections blocked.

  1. Permit the SSH service:

    sudo ufw allow ssh
  2. Inspect the resulting configuration before changing the firewall state:

    sudo ufw status verbose
  3. Enable UFW:

    sudo ufw enable
  4. Confirm the active configuration again:

    sudo ufw status verbose

Keep your current SSH session open until the final status check succeeds. Opening a second session from the same administration network is a practical way to verify that a new connection can still be established before you close the original one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the rule syntax that fits your situation

These forms express the same basic intent but differ in clarity and portability. The service-name form is concise; the explicit form makes the protocol and port visible to anyone reviewing the rule.

Use case Command What it permits
SSH service name sudo ufw allow ssh Traffic identified as the SSH service through the system service definition.
Default SSH port, explicit TCP sudo ufw allow 22/tcp TCP connections to port 22 from any source.
Default port number sudo ufw allow 22 Port 22 using UFW’s port rule syntax; specify the protocol explicitly when you want the rule to be unambiguous.
Custom SSH port sudo ufw allow 2222/tcp TCP connections to port 2222; substitute the port configured for your SSH daemon.

The service-name rule depends on the local /etc/services entry. An explicit port rule is easier to audit when the daemon uses a nonstandard port or when you are documenting infrastructure for another administrator.

Allow SSH on a custom port

Do not assume that port 22 is correct merely because it is the default. Use the port configured for the SSH daemon and include the protocol. For a daemon listening on TCP port 2222, the rule is:

sudo ufw allow 2222/tcp

Replace 2222 with the real listener port. If you permit port 22 while the daemon listens elsewhere, UFW will not make SSH reachable; if another service uses port 22, you may expose that service instead of SSH.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

UFW’s general port syntax is PORT[/PROTOCOL]. The protocol suffix is useful because SSH uses TCP, and stating it prevents the rule from being interpreted more broadly than intended. After adding the rule, check the output of sudo ufw status verbose and confirm that the displayed port corresponds to the daemon’s configuration.

Restrict SSH to a trusted IP address or subnet

An unrestricted allow rule accepts matching SSH traffic from every source that can reach the server. If administration comes from a stable office, VPN, bastion host, or other known network, a source restriction reduces the number of systems that can even attempt an SSH connection.

Permit one trusted host

sudo ufw allow proto tcp from 192.0.2.10 to any port 22

This example permits TCP port 22 only when the source address is 192.0.2.10. Replace that documentation address with the public address from which your administration traffic actually originates. If your SSH daemon uses a custom port, replace 22 with that port.

Permit a trusted subnet

sudo ufw allow proto tcp from 192.0.2.0/24 to any port 22

The CIDR suffix /24 represents the stated subnet. Use the network and prefix length assigned to your trusted administration network, not a guessed range. A subnet rule is appropriate when legitimate administrators use several addresses in the same controlled network; it is broader than a single-host rule.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the practical consequence before restricting

  • Identify the source address that the server will see. A private workstation address may be translated by a router, VPN, or cloud gateway before it reaches the server.

  • Ensure every legitimate administration path is covered. A rule limited to an office address can block you when you later connect through a VPN or another approved location.

  • Add the restricted rule before removing a broader rule, and test a new SSH session while the current session remains open.

Use rate limiting when a broad allow rule is unavoidable

When you cannot restrict SSH to a known source network, UFW also supports a rate-limited form. The ufw manual documents:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo ufw limit 22/tcp

A limit rule is different from a normal allow rule: it is intended to reduce repeated connection attempts to the specified TCP port. It is not a replacement for source restriction when you have a reliable list of trusted addresses, and it does not replace strong SSH authentication or sensible daemon configuration. Apply it to the actual SSH port if the daemon is not using 22.

Preview a rule without applying it

For a change you want to inspect first, use UFW’s dry-run option:

sudo ufw --dry-run allow 22/tcp

--dry-run prints the rules that would result without applying the change. This is useful when you are working on a production host, reviewing a custom-port expression, or checking how a source restriction will be represented. A dry run is only a preview; you still need to run the command without --dry-run to create the rule.

Verify that the rule is active and understandable

UFW provides several status views. Use the least detailed view for a quick check, the verbose view for policy and logging information, and the numbered view before editing rules.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Command When to use it
sudo ufw status Quickly see whether UFW is active and which rules are listed.
sudo ufw status verbose Confirm the active state and inspect the fuller rule and policy display after a change.
sudo ufw status numbered Show rule numbers before deleting or otherwise modifying a particular entry.

Look for the intended SSH port, the TCP protocol, and the source scope you selected. A rule that says port 22 is open to all sources is materially different from one that names a single host or subnet. If you added both a broad and a restricted rule, the broad rule can make the restriction ineffective for traffic it already matches.

Understand rule order before adding exceptions

UFW evaluates matching rules in order. Overlapping entries therefore deserve attention: a generic allow rule can match traffic before a later, more specific rule is considered. For example, adding sudo ufw allow 22/tcp and then adding a rule limited to one source does not turn the first rule into a restricted rule; the generic entry still permits other sources.

Run:

sudo ufw status numbered

Review the numbered list, identify duplicate or overlapping SSH entries, and decide which policy you actually want. Keep one intentional rule where possible. If you need to remove an entry by its position, use the numbered deletion form supported by UFW, and recheck the list afterward so that you do not accidentally remove the wrong rule after numbering changes.

Delete or replace an SSH rule

To remove a rule, repeat the original expression after the word delete. For an explicit default-port rule:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo ufw delete allow 22/tcp

For a service-name rule, use the matching expression:

sudo ufw delete allow ssh

For a source-restricted rule, repeat the complete source, protocol, destination, and port expression. The more precisely your deletion command matches the rule you created, the less likely you are to remove a different entry. When several similar entries exist, inspect status numbered first and use the rule number if that is clearer.

Do not delete the only working SSH rule from a remote host until a replacement rule is already present and a second connection has been tested. If you need to change from an unrestricted rule to a restricted one, add and test the restricted rule first, then remove the broad rule.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common failure modes and recovery paths

UFW was enabled before SSH was allowed

If a remote session stops accepting new connections after enabling UFW, use an out-of-band route such as the server console or your hosting provider’s recovery console. From that local administrative channel, add the appropriate SSH rule, inspect the status, and test a fresh connection. Do not rely on an existing session as proof that new traffic is permitted.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The rule uses port 22 but SSH is configured elsewhere

Replace the service-name or port-22 rule with the daemon’s configured TCP port. A firewall rule for the wrong port cannot make the service reachable. Confirm the displayed UFW entry after making the change.

The source restriction uses the wrong address

Traffic may arrive from a VPN gateway, office router, bastion host, or other translated address rather than the address shown on the workstation. Correct the rule to the source address visible to the server, and keep an alternate approved access path available while testing.

A broad rule defeats a specific rule

Use sudo ufw status numbered to find overlapping entries. Remove the generic allow rule if the intended policy is source-restricted, then verify that the remaining rule names the expected source and port.

The command appears to work but the connection still fails

A practical decision guide

Situation Prefer Reason
You are using the conventional SSH service and want a short command sudo ufw allow ssh Uses the service name defined by the system service database.
You want the port and protocol obvious in configuration reviews sudo ufw allow 22/tcp States TCP port 22 explicitly.
The daemon listens on a nonstandard port sudo ufw allow <port>/tcp Matches the actual listener instead of assuming port 22.
Only one fixed administration host should connect sudo ufw allow proto tcp from <ip> to any port <port> Reduces the permitted source set to one address.
Several approved addresses share a controlled network sudo ufw allow proto tcp from <cidr> to any port <port> Allows the required subnet while excluding other sources.
You need to reduce repeated attempts but cannot maintain a source list sudo ufw limit <port>/tcp Applies UFW’s documented connection-rate control form.
You want to inspect a change first sudo ufw --dry-run allow <port>/tcp Shows the resulting rules without applying them.

The angle-bracketed values in the table are descriptive placeholders for the real port, IP address, or CIDR in your environment; when entering a command, replace them with actual values and omit the brackets.

Final checklist for a remote Ubuntu server

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.