Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk8 min

How to Allow SSH Connections Through UFW on Ubuntu

Add the SSH UFW rule before enabling the firewall. This guide shows service-name and explicit TCP commands, custom-port and source-restricted rules, dry-run previews, verification, rate limiting, logging and safe troubleshooting.

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.

On a remote Ubuntu server, add the SSH rule before you enable UFW. For the normal SSH service, run:

sudo ufw allow ssh
sudo ufw status verbose
sudo ufw enable
sudo ufw status verbose

The first command permits the service name recorded for SSH (normally TCP port 22). If your SSH daemon listens on another port, substitute that port and protocol instead. Confirm the rule while your existing administrative session is still available, then enable 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.

What the SSH rule does

Ubuntu identifies ufw as its default firewall configuration tool. UFW adds simple allow, deny, limit and logging rules to the host firewall. An SSH rule tells UFW which incoming connection should be accepted so that an SSH client can reach the daemon.

The familiar SSH endpoint is TCP port 22, but port 22 is not guaranteed. The daemon can be configured to listen on a different port, and the UFW rule must match that actual listener. A rule for the wrong port can be syntactically valid yet useless.

#1 Best Overall
Sale
TOZO A1 Wireless Earbuds Bluetooth 5.3 Light Weight in Ear Headphones | IPX5 Waterproof,2 Mics for AI Calls,Immersive Premium Sound Bass Headset with Charging Case,32 EQ via App Black
  • [Ultra-Lightweight Ear Buds Designed for Small Ears] Each earbud weighs only 3.7g and features a compact, ergonomic in-ear design made especially for small ears. Secure, low-profile, and comfortable for workouts, all-day wear or overnight sleeping.The ultra-slim profile sits flush in your ear with no hard edges poking or rubbing, so you can even sleep on your side without discomfort.
  • [Immersive Stereo Sound with TOZO OrigX Technology] TOZO OrigX tuning delivers clear vocals, balanced mids, and natural stereo sound for music, podcasts, and videos.
  • [Long Battery Life for Daily Use] Get up to 7 hours of playtime on a single charge, with up to 32 hours total using the charging case—ideal for workdays, commuting, and extended listening sessions.
  • [Bluetooth 5.3 & Stable Connection] Bluetooth 5.3 provides fast pairing, stable wireless performance, and reduced dropouts as you move around home or office.
  • [Deep Bass with Clear Vocals] High-performance drivers produce punchy bass while keeping vocals clean and detailed for everyday listening.

Service name, port and protocol

sudo ufw allow ssh uses the service name defined in /etc/services. The explicit equivalent for the usual endpoint is sudo ufw allow 22/tcp. Writing the port and protocol makes the intended match obvious and is useful when the service name is unavailable or when documenting a non-standard configuration.

Choose the rule that matches your situation

Situation Command Effect and trade-off
Standard SSH service sudo ufw allow ssh Uses the SSH service definition in /etc/services; convenient, but dependent on that service mapping.
Standard port, explicit TCP sudo ufw allow 22/tcp Opens TCP port 22 directly and records the protocol explicitly.
Custom SSH port sudo ufw allow 2222/tcp Replace 2222 with the port configured for your daemon; the protocol must match.
One trusted host sudo ufw allow proto tcp from 192.0.2.10 to any port 22 Accepts SSH only from the specified address, reducing exposure.
Trusted network sudo ufw allow proto tcp from 192.0.2.0/24 to any port 22 Accepts connections from the stated CIDR subnet rather than every source.
Connection-rate control sudo ufw limit 22/tcp Uses UFW’s rate-limiting rule form instead of a plain allow rule.
Preview before applying sudo ufw --dry-run allow 22/tcp Prints the rules that would result without changing the firewall.

Use the least broad rule that remains operationally practical. An unrestricted allow rule is portable and quick, while a host or subnet rule limits who can reach the SSH port. A limit rule adds connection-rate control, and a dry run lets you inspect the generated change before applying it.

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

Safely open SSH before enabling UFW remotely

Enabling UFW on a machine you administer over SSH is the point at which an omitted rule can interrupt access. Add and inspect the SSH rule first, then enable UFW. The following sequence is the safe baseline for the standard SSH service:

  1. Add the service rule.

    sudo ufw allow ssh

    This creates an allow rule for the service name. If you prefer an explicit port and protocol, use sudo ufw allow 22/tcp instead.

  2. Inspect the planned active configuration.

    sudo ufw status verbose

    Read the displayed rule and confirm that it represents SSH before changing the firewall state.

  3. Enable UFW.

    sudo ufw enable

    Do this only after the SSH rule is present. The allow rule is what preserves the intended SSH path when the firewall becomes active.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    Rank #2
    Sale
    Smart Watch for Men Women Compatible with iPhone Samsung Android Phone 1.69 inch Full Touch Screen IP68 Waterproof Bluetooh Fitness Tracker Heart Rate/Sleep Monitor (Black)
    • 【IP68 Waterproof Multifunctinal Smart Watch】Torjalph fitness activity tracker smart watch is IP68 waterproof technology ,you can wear it when swimming,handwashing,it's rain-proof which means that you perform multiple exercise modes outside without worrying about sweat or rain corroding your fitness smart watch,and this watch equipped with a 230 mAh large capacity battery magnetic charging ,and this sports activity tracker watch can be used 5-7 days If fully charged.
    • 【Larger 1.83'' Full Screen Watch & Multi-dial Selection】1.83 inch full HD color touch screen smart watch for men / women provides excellent 240*284 resolution image quality ,This Bluetooth smart watch offers a better touch experience and remarkable immersion and ,more than 200 watch faces for you to choose from App"Da Fit ", you can change the dial style according to your mood to wear keeping the world on your wrist
    • 【Accurate Activity Tracker Smart Watch 】sports watch activity tracker uses sweat and dust resistant breathable silicone strap, our Fitness Tracker Smart Watch for Android iOS Phones supports 12 sports modes,walking,running,bicycle,swimming,badminton,basketball,football,rope skipping. This bluetooth calls smart watch for men women is lightweight and stylish, bringing more convenient and relaxing sports experience while also providing more protection.
    • 【Your 24 Hour Personal Daily Health Monitor】Fitness watch for women men full touch screen IP67 waterproof bluetooth smartwatch for Android iOS Phones with heart rate blood pressure monitor multifunction Andriod smart watch with steps counter sleep monitor sports fitness watch, monitoring blood oxygen saturation allows you to understand your physical condition in real time to track fitness progress ,to know your health status in time.
    • 【More Functions Bluetooth Call Smart Watch】This fitness activity smart watch full screen for iPhone and Android phones includes a bunch of new features including:The bluetooth smart watch can hang up the phone, receive and display WhatsAPP /Text /Facebook /Twitter /Messenger reminder.Support sedentary reminder, alarm clock, stopwatch, timer,weather information,music control, remote camera control compatible with Android 5.0 and above or iOS 9.0 and above smartphones.
  4. Verify after activation.

    sudo ufw status verbose

    Check that UFW is active and that the SSH rule remains listed. Keep the current administrative session available while you test a new connection, rather than closing your only working path immediately.

Use a custom daemon port

If the SSH daemon is configured for a port other than 22, replace the service name or port in every relevant command. For example:

sudo ufw allow 2222/tcp
sudo ufw status verbose
sudo ufw enable
sudo ufw status verbose

The number 2222 is only an example. Use the port in your SSH daemon configuration. UFW accepts the PORT[/PROTOCOL] form, so include /tcp for an SSH TCP listener. Opening port 22 when the daemon listens on 2222 does not open the actual SSH endpoint.

Restrict SSH to a known address or network

A broad SSH rule permits attempts from any source that can reach the server. Where your administration path has a stable public address or a defined management network, add a source restriction instead.

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

Allow one host

sudo ufw allow proto tcp from 192.0.2.10 to any port 22

This rule specifies TCP, the single source address 192.0.2.10, and destination port 22. Replace the example address with the trusted host that should administer the server. If SSH uses a custom port, replace 22 with that port.

Allow a subnet

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

The CIDR suffix describes a subnet rather than one machine. Use this when administrators connect from addresses within a known network range. A subnet rule is broader than a single-host rule, so choose the narrowest range that still covers legitimate access.

Rank #3
New Bee Guitar Cable 10ft Electric Instrument Bass AMP Cord
  • [Low Noise High Fidelity] New bee guitar cord with 22AWG oxygen-free-copper center conductors provides enhanced signal transfer, the PVC insulation material and the high-density stranded copper double shield provide noise rejection for quiet operation
  • [Ideal for Choice] New bee 1/4 straight and right angle metal connectors, ensures a secure connection in any instrument, pedal, or amp jack. L shape great fro on stage guitar stands
  • [Wide Compatible] Suitable for mixer, power amplifier, microphone, effector, decoder, equalizer, electoronic organ, electric guitar, electric bass and other electronic equipments. Widely used in the installation of smart home, karaoke speaker, professional stage audio, etc
  • [Easy Disassemble] Making incidental repairs quick and easy! Very nice, flexible cable
  • [Dont Hesite to Order] 24 hours customer services and professional technology team are standing by

Check the source-restricted rule

After adding either rule, inspect the result:

sudo ufw status verbose

Make sure the source address or CIDR, protocol and destination port are shown as intended. A source-restricted rule can leave you unable to connect if your current address is not the address you entered, so verify the management path before relying on it.

Preview a change with dry run

Use UFW’s dry-run option when you want to see the rules that a command would generate without applying them:

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

The output is a preview only. It does not open the port. Once the preview matches your plan, run the same command without --dry-run and then verify the active configuration with sudo ufw status verbose.

Dry-run is particularly useful when changing a custom port, adding a source qualifier or checking the exact protocol expression before modifying a production host. It separates syntax and rule-generation checks from the actual firewall change.

Verify active rules and their order

UFW provides three status views, each useful at a different stage:

  • sudo ufw status gives a concise list of active rules.
  • sudo ufw status verbose includes the firewall state and additional rule detail, making it the normal post-change check.
  • sudo ufw status numbered prefixes rules with numbers, which is useful when you need to delete or insert a particular entry.

Use the numbered view before editing a configuration that contains overlapping rules. UFW evaluates matching rules in order. A broad rule placed before a more specific rule can affect which result is reached, so inspect the sequence rather than looking only for the presence of an SSH line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
BSIMB 15.6" Digital Family Calendar & Chore Chart, No Subscription Fees
  • Digital Family Calendar: BSIMB digital calendar wall touch screen puts an end to your chaotic life. Designed for busy families, the digital calendar keeps you organised and in step with your family. Assign different colours and tasks to each family member to make family scheduling easier and more intuitive.
  • Easy Setup and Auto-Sync: Plug in, connect to WLAN and auto-syn your electric calendar in minutes. Digital wall calendar compatible with Google calendar, iCloud calendar, Outlook calendar, Cozi calendar and Yahoo calendar. Add events directly on the device or through the ecalendar app. Electronic calendar stays current whether you're at work or traveling—no manual updates needed.
  • All-In-One Home Assistant: Smart calendar builds habits and encourage independence with personalised chores. Display daily meal plan to reduce the daily ‘What's for dinner?’ stress. To-do lists keep track of everything you want. Turning chores into valuable achievements with stars and rewards, making life easier and more enjoyable.
  • Manage Anytime Anywhere: BSIMB electronic calendar for family stay connected with the powerful ecalendar app. Access and manage your digital wall calendar anytime, anywhere with free features like stars & rewards, photo screensaver, chore charts, meals plans, to-do lists etc. Electronic planner paid features like Magic Import tailor advanced tools for users with special needs.
  • Stay in Touch with Loved Ones: Share photos or videos from your phone to electronic wall calendar with app, instantly update your life moment, and keep in touch with your family, especially those who are far away; you can also surprise your smart calendar digital wall by sending him a wish card; it's also great for keeping tech-illiterate elders in touch with the youngsters.

What to verify in the output

  • The firewall is shown as active after sudo ufw enable.
  • The SSH service, explicit port, or custom port you intended appears in the rules.
  • The protocol is TCP when you used an explicit SSH rule.
  • A restricted rule shows the expected source host or CIDR.
  • Numbered output has the order you expect when both generic and specific entries exist.

Delete or replace an SSH rule

Remove a rule by prefixing the original rule expression with delete. For the explicit standard-port form:

sudo ufw delete allow 22/tcp

For a service-name rule, use the corresponding original expression:

sudo ufw delete allow ssh

If the rule includes a source restriction, repeat that complete expression when deleting it. Alternatively, use numbered status output and delete by rule number after confirming that the number identifies the intended entry. Inspect again with sudo ufw status numbered after the change.

Do not remove the only working SSH rule from a remote host until another valid rule is in place. When replacing an unrestricted rule with a source-restricted one, add and verify the restricted entry first, test it from the permitted source, and only then remove the broader entry.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use rate limiting for repeated connection attempts

The UFW manual documents a limit rule form for connection-rate control:

sudo ufw limit 22/tcp

Apply the same port substitution used for a custom SSH listener. A limit rule is different from a plain allow rule: it still defines the SSH endpoint, but asks UFW to control repeated connection attempts according to its limit behavior. Use it when you want an additional abuse-resistance measure and when that behavior is compatible with your administration pattern.

Inspect the resulting rule with sudo ufw status verbose. Do not describe a limit rule as an IP allowlist; it does not replace a source restriction. If only a known host or subnet should connect, use the corresponding from expression.

Enable logging when diagnosing a rule

UFW supports log and log-all rule forms. These can be used when you need firewall records to understand whether traffic is matching a rule or being handled elsewhere. Add the logging form appropriate to the rule you are diagnosing, then inspect the active configuration with sudo ufw status verbose.

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

Logging is a diagnostic aid, not a substitute for opening the correct port. First ensure that the daemon’s configured port, protocol and source restriction are correct; then use logging to investigate unexpected matches or denials.

Troubleshoot common SSH access failures

The rule exists, but the client cannot connect

Compare the rule with the daemon’s actual listener. Confirm that you opened the configured port rather than assuming 22, and that the rule uses TCP. A service-name rule and an explicit rule can refer to different endpoints if your configuration or service mapping does not match your expectation.

The connection is refused

A firewall allow rule does not start the SSH daemon. If UFW shows the expected rule but the client receives a refusal, verify that the SSH service is running and listening on the same port you opened. Correct the UFW rule if the daemon was configured for a custom port.

The connection times out after enabling UFW

Check from the existing administrative session:

sudo ufw status verbose

Look for an active firewall and an SSH rule that matches the destination port and protocol. If you used a source restriction, compare the permitted address or CIDR with the address from which you are connecting. A source mismatch will not be fixed by adding more unrelated port rules.

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

A specific rule seems ineffective

Display numbered rules:

sudo ufw status numbered

Review the order for overlapping entries. UFW evaluates matching rules in order, so a generic entry can be encountered before a more specific one. Remove or reorder the unintended entry using its original expression or its confirmed rule number, then verify again.

The service-name command is unclear or unavailable

Use the explicit form and state the protocol:

sudo ufw allow 22/tcp

For a custom listener, substitute its configured port. This avoids depending on the SSH name in /etc/services and makes the intended endpoint visible in status output.

A practical decision sequence

  1. Find the daemon’s configured port. Use 22 only when that is the actual SSH listener; otherwise use the custom value.
  2. Choose the source scope. Prefer a trusted host or CIDR when your administration network is stable; use an unrestricted rule only when broader reachability is required.
  3. Choose the expression. Use allow ssh for the service-name form, allow PORT/tcp for an explicit endpoint, or the from ... to any port ... form for source control.
  4. Preview if the change is sensitive. Run the matching --dry-run command and review its output.
  5. Apply before enabling. Add the rule, inspect it, enable UFW, and inspect the active state again.
  6. Harden or troubleshoot. Consider limit PORT/tcp, use logging while diagnosing, and consult numbered status output before deleting overlapping rules.

Command reference

Purpose Command
Allow the SSH service name sudo ufw allow ssh
Allow standard SSH explicitly sudo ufw allow 22/tcp
Allow a custom SSH port sudo ufw allow 2222/tcp
Allow one source address sudo ufw allow proto tcp from 192.0.2.10 to any port 22
Allow a source subnet sudo ufw allow proto tcp from 192.0.2.0/24 to any port 22
Preview a rule sudo ufw --dry-run allow 22/tcp
Show concise status sudo ufw status
Show detailed status sudo ufw status verbose
Show numbered rules sudo ufw status numbered
Enable UFW sudo ufw enable
Rate-limit standard SSH sudo ufw limit 22/tcp
Delete an explicit rule sudo ufw delete allow 22/tcp

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 from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.