Published 2026-11-22
Change the Default SSH Port on a VPS: Checklist

Short answer: pick a port above 1024, add it to the firewall first, update the SSH daemon configuration, test a new connection while the old one is still open, and only then remove the old port. Changing the default SSH port does not make a server secure by itself — it reduces the volume of automated login attempts that hit port 22. The real security still comes from key-based login and a firewall. Done in the wrong order, though, a port change is one of the easiest ways to lock yourself out of your own server.
In this guide
The order matters more than the port number. Every step below exists because someone skipped it: the firewall that still blocks the new port, the daemon that never reloaded, the saved connection that still dials port 22. Work through them in sequence, and keep one working SSH session open until the new port is proven.
1. Pick a port and check it is free
Choose a port above 1024 so it does not clash with well-known services, and avoid ports your other services already use. A port in the 20000–40000 range is a common choice. Before changing anything, check that nothing on the server is already listening on it:
ss -tlnp | grep 2222
No output means the port is free. If something is already listening there, pick a different number — reusing a port that belongs to another service will break that service, not just SSH.
2. Open the new port in the firewall first
This is the step that causes most lockouts. Add a rule for the new port before touching the SSH configuration, and confirm the rule is active:
sudo ufw allow 2222/tcp
sudo ufw status
If the server sits behind a cloud firewall or a provider-level security group as well as the on-server firewall, the new port has to be open in both places. Our firewall first-steps guide covers the two layers and how to check each one. Do not proceed until the new port is open everywhere the old one was.
3. Update the SSH daemon configuration
Edit /etc/ssh/sshd_config and set the Port directive to the new number. If the line is commented out with a #, remove the comment character and change the value. While you are in the file, confirm that password authentication is disabled if you already use keys — the port change is a good moment to check, and our SSH key rotation guide covers key-only login:
Port 2222
PasswordAuthentication no
Check the configuration for syntax errors before reloading, so a typo cannot stop the daemon from starting:
sudo sshd -t
sudo systemctl reload sshd
A reload keeps existing connections alive, so the session you are working in stays connected. A restart would drop it, which is why reload is the safer choice here.
4. Test the new port before closing the old one
Open a second terminal and connect to the new port while your first session is still open:
ssh -p 2222 user@203.0.113.10
Replace the address with your own server's. If the login succeeds, the change works end to end. If it fails, your first session is still alive and you can fix the firewall rule or the configuration without any recovery procedure. Only after a successful test should you remove the old port 22 rule from the firewall — and update every saved connection, script, and deployment tool that still dials port 22, or the next automated job will fail silently.
5. Update fail2ban and anything else that watches port 22
Tools that watch SSH traffic usually assume port 22. If fail2ban is running, confirm its SSH jail is watching the new port, or brute-force attempts on the new port will go unlogged. Our fail2ban checklist covers verifying the jail after a configuration change. The same applies to any monitoring or alerting that probes SSH: update the port there too, or the monitor will report the server as down the moment you change it.
Checklist before you move on
- New port chosen above 1024 and confirmed free on the server.
- New port open in the on-server firewall and any cloud firewall, before the daemon change.
sshd_configupdated,sshd -tpasses, daemon reloaded.- New connection tested from a second terminal while the old session stays open.
- Old port 22 firewall rule removed only after the new port is proven.
- Saved connections, scripts, deployment tools, fail2ban, and monitors updated to the new port.
If the test connection fails and the first session is gone, the recovery path is the provider console or a rescue mode — our SSH lockout recovery guide walks through it. But a port change done in this order should never need it.