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 DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk9 min

How to Change the SSH Port on a VPS Safely

A practical, lockout-resistant guide to changing SSH from port 22 to a custom port on Ubuntu VPSs, including socket activation, UFW, drop-ins, verification, and recovery.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To change a VPS SSH port safely, set the new Port in /etc/ssh/sshd_config, validate it with sudo sshd -t, allow the port in every applicable firewall, then apply the listener change. On Ubuntu 22.10 and later, reload systemd and restart ssh.socket; restarting only ssh.service can leave SSH listening on port 22.

Keep your current SSH session open, connect through the new port from a second session, verify the listener with ss, and remove the port-22 firewall rule only after the new connection works.

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 changing the SSH port actually does

SSH normally listens on TCP port 22. Changing that listener to another TCP port, such as 2222 or 52022, means clients must specify the new port when connecting:

ssh -p 2222 user@server_ip

The change affects the server’s listening socket and the firewall rules that permit traffic to it. It does not rename the SSH service, change user accounts, or migrate existing keys. A port change can reduce noise from automated scans, but it is not a replacement for key-based authentication, updates, least-privilege accounts, or other SSH hardening.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

The important implementation detail is the operating system’s service manager. On Ubuntu 22.10 and later, published administration guides report that systemd socket activation can make ssh.socket the unit that owns the listening port. In that arrangement, changing sshd_config and restarting only ssh.service may leave port 22 open.

Before changing anything

Keep a recovery path

  • Leave your existing SSH session connected for the entire change.
  • Know how to open your VPS provider’s web, serial, or VNC console before you start. That console is the fallback if a firewall or syntax mistake blocks SSH.
  • Have the server IP, username, private key, and chosen port ready for a second connection.

Choose a port deliberately

Use an unused high TCP port. Common examples are 2222 and 52022. Avoid ports already assigned to another service, and do not assume that a port is free just because no firewall rule currently mentions it.

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

Check current listeners before choosing:

sudo ss -tlnp

Pick a port that does not appear in the output and that your provider’s network firewall can permit. If your VPS has a separate cloud security group or provider-level firewall, the VM’s firewall rule alone will not be sufficient; add the same TCP port there before testing.

Back up the SSH configuration

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

The backup gives you a known file to restore from the provider console if the configuration becomes invalid.

How Ubuntu reads the SSH configuration

Ubuntu’s default /etc/ssh/sshd_config commonly starts with an include directive similar to:

Include /etc/ssh/sshd_config.d/*.conf

OpenSSH generally uses the first value it finds for most directives. A file in /etc/ssh/sshd_config.d/ can therefore silently supply a different Port value from the one you edit in the main file.

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

Inspect the directory before changing the port:

sudo ls -la /etc/ssh/sshd_config.d/
sudo grep -RIn --include='*.conf' '^[[:space:]]*Port[[:space:]]' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/

If an existing drop-in declares Port 22, decide which file should be authoritative and remove or edit the conflicting declaration. Adding another file without checking the load order can leave the old value active.

Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Recommended procedure for Ubuntu 22.10 and later

1. Set the new port

Edit the main file:

sudo nano /etc/ssh/sshd_config

Find the existing port line. It may be commented as #Port 22. Add or change it to your selected value, for example:

Port 2222

Save the file and exit. You can instead use a dedicated drop-in such as /etc/ssh/sshd_config.d/99-port.conf, but only after checking for earlier drop-ins that already set Port. Do not leave contradictory active port directives and expect the last one to win.

2. Validate the syntax before applying it

sudo sshd -t

No output normally means the syntax check passed. If the command reports an error, correct that error and run the test again. Do not restart SSH while this check is failing.

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

3. Open the new port before switching listeners

For a server using UFW, allow the new TCP port first:

sudo ufw allow 2222/tcp

Keep the existing port-22 rule in place for now. If a provider firewall or security group sits outside the VPS, allow TCP 2222 there as well. The exact control-panel label varies by provider; the required rule is inbound TCP access to the new port.

Confirm the VM rule:

sudo ufw status

4. Reload systemd and restart the socket

Apply the socket-activation change with:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

daemon-reload makes systemd reread unit definitions and overrides. Restarting ssh.socket moves the listening socket to the port represented by the active SSH configuration on systems where the socket unit follows sshd_config.

Do not close your current session merely because these commands returned successfully. A successful restart proves only that systemd accepted the operation; it does not prove that the firewall and external path work.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

5. Inspect the listening ports

sudo ss -tlnp | grep -E ':(22|2222)b'

Replace 2222 with your selected port. The expected result is a listener on the new port. If port 22 is still listed, follow the troubleshooting section below before removing any firewall rule.

6. Test from a second terminal

Open a new terminal window or local console while the original session remains connected:

ssh -p 2222 user@server_ip

Use the same username, key, hostname, or IP that worked on port 22. Once authenticated, verify that you are on the intended server:

hostname
whoami

Only after this second connection works should you proceed.

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

7. Remove port 22 last

Delete the old UFW rule:

sudo ufw delete allow 22/tcp
sudo ufw status

If port 22 is also allowed in a provider firewall or security group, remove that external rule after confirming the new path works. Keep the original session open until you have confirmed that new connections continue to succeed after the old rule is removed.

When Ubuntu is still listening on port 22

The most common causes are an overriding configuration file, a socket unit that still owns port 22, or a firewall rule being mistaken for a listener. Work through these checks in order.

Check the effective configuration and drop-ins

Search every SSH configuration file for active port declarations:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
sudo grep -RIn --include='*.conf' '^[[:space:]]*Port[[:space:]]' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/

Remove or edit an earlier declaration that still specifies Port 22, then run:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo sshd -t
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Inspect the socket unit

Display the unit and any loaded drop-ins:

systemctl cat ssh.socket
systemctl show ssh.socket -p Listen

If the unit explicitly contains ListenStream=22, an override may be required. Create an administrator-owned override rather than editing a vendor file:

sudo systemctl edit ssh.socket

In the editor, enter:

[Socket]
ListenStream=
ListenStream=2222

The empty ListenStream= line clears the inherited listener before the new value is added. Save and exit, then apply and inspect the result:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
systemctl show ssh.socket -p Listen
sudo ss -tlnp | grep -E ':(22|2222)b'

Several third-party guides recommend this override for Ubuntu systems using socket activation. Reports about Ubuntu 24.04 are not consistent: some say the Port value is picked up automatically, while others report that an explicit socket override is needed. The listener shown by ss is the practical test for your installation.

Do not edit the vendor unit directly

Some older tutorials modify a file under /lib/systemd/system/. That file belongs to the installed package and can be replaced during an upgrade. A systemctl edit ssh.socket override is stored under the administrator configuration path and is the safer way to customize the unit.

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

Distinguish a listener from a firewall rule

A firewall rule does not create a listening socket. Use ss -tlnp to see which local ports a process or socket unit owns, and use ufw status to see which traffic the firewall permits. You need both the listener and the allow rule on the new port.

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

Ubuntu 22.04, older releases, and systems without socket activation

On Ubuntu 22.04 and older systems, and on installations that do not use socket activation, guides generally describe the simpler service-based sequence:

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
  1. Edit /etc/ssh/sshd_config and set Port 2222, while checking the drop-in directory for an earlier value.
  2. Run sudo sshd -t and correct any syntax error.
  3. Allow the new port with sudo ufw allow 2222/tcp, keeping port 22 open.
  4. Restart the SSH service:
sudo systemctl restart ssh
  1. Verify with sudo ss -tlnp | grep -E ':(22|2222)b'.
  2. Test ssh -p 2222 user@server_ip from a second session.
  3. Remove the old firewall rule only after the new login succeeds.

If ssh.socket exists and owns the listener, use the socket procedure instead of assuming a service restart is enough. You can check whether the unit is present with:

systemctl status ssh.socket

Firewall and provider controls

UFW

UFW is the Ubuntu firewall front-end used in the command examples above. The safe order is always “allow new, test new, remove old.” A rule can be checked with:

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

If you accidentally delete the wrong rule, the numbered status output helps identify what remains. UFW reload behavior after a rule change is not consistently documented in the guides used for this procedure; the allow and delete commands apply the rule directly on typical installations, while sudo ufw status confirms the current state.

Provider firewalls and security groups

A VPS can have two independent network filters: one inside the guest operating system and another at the hosting provider. If the provider-level control blocks TCP 2222, the SSH daemon can listen correctly while every external connection times out. Add the new inbound rule at the provider level before switching, and remove the old port-22 rule there only after verification.

Other firewall frameworks

If the server uses nftables, iptables, firewalld, or another firewall instead of UFW, add an equivalent inbound TCP rule for the new port using that system’s normal administration method. Do not run UFW commands merely because the operating system is Ubuntu; first determine which firewall is active.

Recovery if you lock yourself out

Do not terminate the original SSH session until the migration is complete. If all external sessions fail after the switch:

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.
  1. Open the VPS provider’s web, serial, or VNC console.
  2. Check the listener with sudo ss -tlnp.
  3. Check the configuration with sudo sshd -t.
  4. Check sudo ufw status and any provider-level firewall rule.
  5. Restore the backup if the configuration is invalid:
sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
sudo sshd -t

On a socket-activated system, reload systemd and restart the socket after restoring:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

If the issue is only that the new firewall rule was omitted, re-allow the chosen port and test again. Keep port 22 available until you have a confirmed working path.

Notes for RHEL-family and other distributions

The file is commonly still /etc/ssh/sshd_config, but service names, firewall tools, and systemd units differ. On RHEL-family systems with SELinux enforcing, the new port may also require an SELinux port label before sshd can bind to it. The exact labeling procedure depends on the distribution and installed policy; consult that distribution’s SELinux and OpenSSH documentation rather than copying Ubuntu UFW or socket commands blindly.

The same safety principles remain valid: retain a console path, validate syntax, allow the new port in every firewall layer, verify the listener, test a second session, and remove port 22 last.

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.

Final verification checklist

  • sshd_config or an intentional drop-in contains the desired Port.
  • sudo sshd -t completes without an error.
  • The new TCP port is allowed by UFW or the active guest firewall.
  • Any provider firewall or security group allows the same port.
  • Ubuntu 22.10+ systems have had systemctl daemon-reload and systemctl restart ssh.socket applied when required.
  • sudo ss -tlnp shows the new listener.
  • A second terminal can log in with ssh -p NEW_PORT user@server_ip.
  • Only after those checks does the port-22 firewall rule get removed.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair 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.