Changing the SSH port on your server

How to move SSH off port 22 and reduce brute-force attack exposure.

By default, SSH listens on port 22 — and that's exactly where automated bots look first. The moment a scanner detects an open port 22, it starts throwing login attempts at it. Switching to a non-standard port won't make your server invisible, but it will eliminate the vast majority of that noise overnight.

Connecting to your server

Connect via SSH using whichever client you prefer:

  • Windows: PuTTY, MobaXterm, Windows Terminal
  • macOS / Linux: the built-in terminal
ssh root@YOUR_SERVER_IP

Checking which ports are already in use

Before picking a new port, make sure it's actually free:

netstat -tupln | grep LISTEN

This lists every port currently in use. Pick any unused number — somewhere in the 1024–65535 range works well.

Editing the SSH config

Open the SSH daemon config file in your preferred editor:

Nano:

nano /etc/ssh/sshd_config

Vi:

vi /etc/ssh/sshd_config

Find this line:

#Port 22

Uncomment it by removing the #, then replace 22 with your chosen port number:

Port 2222

Saving the file

Nano: Ctrl + O → Enter → Ctrl + X

Vi: type :wq and press Enter

Restarting the SSH service

Apply the change by restarting SSH:

systemctl restart sshd

If that doesn't work on your system, try one of these:

/etc/init.d/ssh restart
# or
/etc/init.d/sshd restart

Verifying the new port works

Don't close your current SSH session yet. Test the new port first — if something went wrong, you'll want your existing connection as a fallback.

Open a new terminal window and connect using the new port:

ssh -p 2222 root@YOUR_SERVER_IP

In PuTTY, simply update the Port field before connecting.

If the login succeeds, you're all set. Port 22 is now closed, and SSH is only reachable on the new port.

If you're running a firewall (iptables or ufw), don't forget to allow incoming connections on the new port and optionally block port 22 to keep things clean.

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us