Should You Change the Default SSH Port? How to Do It Safely

If you run a VPS, changing the default SSH port is one of the first hardening steps people recommend, and one of the quickest. It takes about five minutes and immediately quiets the automated bots that hammer port 22 with login attempts around the clock. This guide shows you exactly how to do it on a typical Ubuntu or Debian server, including the safety checklist that keeps you from locking yourself out, which is the part most tutorials skip.

The short answer: yes, change the port, but do it carefully and treat it as one layer of defense, not the whole plan. Here is the full walkthrough.

What Changing the SSH Port Actually Protects Against

SSH listens on port 22 by default, and every botnet on the internet knows it. Automated scanners sweep entire IP ranges and try thousands of username and password combinations against port 22. They are not targeting you personally. They are rattling every doorknob on the street, hoping to find one that opens.

Moving SSH to a non-standard port does not make your server invisible. A determined attacker running a full port scan can still find it. What the change does is take you out of the firehose of lazy, automated login attempts that only bother checking port 22. That means cleaner logs, fewer wasted CPU cycles on failed logins, and a much quieter auth log to monitor.

Before You Touch Anything: The Safety Checklist

Almost every SSH lockout story starts the same way: someone edited sshd_config, restarted the service, closed their session, and discovered the new setup did not work. Run through this checklist first and you make that outcome nearly impossible.

  • Keep one working SSH session open at all times. Do every test in a second, separate session. Do not log out of the first session until the new one connects successfully on the new port.
  • Back up the config file. Run sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak so you can restore it in seconds if something goes wrong.
  • Confirm you have console access. Most VPS providers offer a web console or VNC rescue console. Open it in a browser tab before you start, so you have a last-resort recovery path that does not depend on SSH at all.
  • Know which firewall you are running. You might have ufw, firewalld, a cloud provider security group, or all of them. The new port must be open in every layer or your connection will be blocked.
  • Ideally, set up SSH key authentication first. If you still log in with a password, this is a good moment to switch. Key-based auth removes password guessing from the equation entirely. (We covered the full setup in our SSH keys tutorial.)

How to Change the SSH Port, Step by Step

Step 1: Pick a port number

Choose a port between 1024 and 65535. Avoid common service ports like 8080, 8443, or 3306 so you do not create confusion later. Something like 2222 is memorable, or pick a random high port like 48213 for a little extra obscurity. Before committing, check that nothing on your server is already using it:

ss -tulpn | grep 2222

If that command returns nothing, the port is free.

Step 2: Open the new port in your firewall

Do this before changing the SSH config, not after. If you use ufw:

sudo ufw allow 2222/tcp
sudo ufw reload

If you use firewalld (common on RHEL, Rocky, and AlmaLinux):

sudo firewall-cmd –permanent –add-port=2222/tcp
sudo firewall-cmd –reload

If your server sits behind a cloud security group (AWS, DigitalOcean, Tencent, and others), add an inbound rule for TCP port 2222 from your IP in the provider’s control panel. This is the layer people forget most often.

On RHEL-based systems with SELinux enforcing, you also need to tell SELinux that SSH is allowed on the new port:

sudo semanage port -a -t ssh_port_t -p tcp 2222

Step 3: Update sshd_config

Open the SSH daemon configuration file:

sudo nano /etc/ssh/sshd_config

Find the line that reads #Port 22, remove the hash, and change it to your new port:

Port 2222

Save and exit, then validate the config before restarting anything. This is the step that prevents most typos from turning into lockouts:

sudo sshd -t

If the command prints nothing, your config is valid. If it prints an error, fix the line it points to before going further.

Step 4: Restart SSH and test in a new session

Restart the SSH service so the change takes effect:

sudo systemctl restart sshd

On some distributions the service is named ssh instead of sshd. If the restart fails, try sudo systemctl restart ssh.

Now, and this is critical, open a new terminal window and connect on the new port while your original session stays open:

ssh -p 2222 your-user@your-server-ip

If the new session connects, the change worked. If it hangs or gets refused, do not panic: your original session is still open. Check the firewall rules, re-run sshd -t, and verify you edited the right line in the config.

Step 5: Close the loop

Once the new session is working, you have two cleanup jobs. First, decide what to do with port 22. Closing it in the firewall removes the old attack surface entirely; leaving it open defeats much of the point. Second, update anything else that connects over SSH on port 22: deployment scripts, backup jobs, rsync commands, Git remotes, and monitoring tools. They all need the new port or they will start failing silently.

Only after all of that is done should you log out of your original session.

Common Mistakes That Lock People Out

Nearly every lockout traces back to one of these. Check the list before you call the job done.

Mistake What happens How to avoid it
Opening the firewall after restarting SSH New port is blocked, old config is gone Always open the port in every firewall layer first
Skipping sshd -t A typo breaks the whole SSH config Validate the config before every restart
Closing the only session before testing No way back in if something is wrong Test in a new session, keep the old one open
Forgetting SELinux on RHEL systems SSH fails to bind the new port Run the semanage command in Step 2
Forgetting the cloud security group Traffic never reaches the server Add the inbound rule in the provider panel
Editing the wrong config line Change never takes effect or breaks parsing Edit the single Port line, then validate

What Changing the Port Does Not Fix

This is where honesty matters. A new port is one control, and the weak version of this advice pretends it is the whole security strategy. It is not.

It does not stop a targeted attacker. Anyone who decides to attack your server specifically will scan all ports and find SSH wherever you put it. The port change only filters out the automated background noise.

It does not replace key-based authentication. If you still allow password logins, bots that do find your port can still guess credentials. Disable password auth with PasswordAuthentication no in sshd_config once your SSH keys are working. That single line eliminates password guessing entirely.

It does not replace rate limiting. Fail2ban watches your auth logs and temporarily bans IPs that fail too many logins. It works on any port and stops the bots that do find your new one. Installing it takes minutes and pairs perfectly with a port change.

The strong setup is all three together: a non-standard port, key-only authentication, and fail2ban. Each one covers the gaps the others leave.

Do You Even Need to Change the Port?

Here is the pragmatic view. If you already use key-only authentication plus fail2ban, automated bots are already harmless to you: they cannot guess a key, and fail2ban will silence repeat offenders. Changing the port in that setup is mostly about log hygiene and reducing noise.

If you have not done those two yet, changing the port first is still worthwhile because it buys you quiet time while you set them up. But do not stop at the port. The port change is the appetizer. Key auth and fail2ban are the meal.

Recap

Changing the default SSH port is a five-minute job with one rule that matters more than the rest: never close your working session until a new session connects on the new port. Back up the config, open the firewall first, validate with sshd -t, test in a second window, then clean up port 22 and update your scripts. Pair the move with SSH keys and fail2ban, and your server goes from being rattled by every bot on the internet to quietly minding its own business.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.