Learning to change default ssh port settings plus port knocking gives your VPS a quieter SSH door. Moving the port cuts automated scan noise, and port knocking adds a secret knock sequence that opens the port only for the knocker. Neither trick replaces key-only login or a firewall, but together they cut log clutter and add a layer that casual scanners never pass.
Order matters more than any single setting here. Update the firewall before you restart SSH on the new port, or the firewall blocks the very port you just moved to. Test in a second session while the first stays open, the same habit that saves every SSH change. If the test fails, the open session lets you revert without provider console gymnastics.
Change Default SSH Port: What It Helps and What It Does Not
Most bots knock only on port 22, the standard SSH port. Moving SSH to a high port, such as a number above 1024 that no other service uses, drops that noise sharply. Logs get readable again, Fail2ban bans fewer addresses, and real login attempts stand out. What it does not do is hide SSH from a targeted scan. A port scan of your server finds the new port in seconds, so treat the port change as noise reduction, not as security by itself. Key-only login remains the real protection, as our guide to disable password login and use SSH keys only explains.
Pick a port that avoids conflicts. Check which ports your services already use, skip well-known service ports, and write the choice down where your team can find it. Every person and script that logs in must learn the new port, so announce the change before you make it, not after.
Step 1: Open the New Port in the Firewall First
Before touching the SSH config, add a firewall rule for the new port and confirm it. An sshd port change also means updating any firewall rule that names the old port, not just the SSH setting itself. With UFW, allow the new port number, then check the firewall status to see the rule listed. Only then edit the SSH daemon config to listen on that port. Restart the SSH service, and immediately test a new login on the new port from a second terminal. If the test works, remove the old port 22 rule from the firewall so the old door closes for good.
That firewall-first order is the detail most guides skip, and skipping it is how people lock themselves out. Our guide to change the default SSH port safely walks through the same sequence with the checks at each step, and it is worth following line by line on a production server.
Step 2: Update Clients and Scripts
After the move, every SSH command needs the new port flag, and saved host entries need updating. Update deploy scripts, backup jobs, and monitoring checks in the same session, then test each one. A backup job that still points at port 22 fails silently at night, and you discover it when you need the backup. Keep a note of the new port in your server docs so a future admin does not waste an hour guessing.
What Port Knocking Adds
Port knocking closes the SSH port by default and opens it only after a client sends a secret sequence of connection attempts to chosen closed ports. Think of it as knocking a rhythm on a door before it opens. A scanner sees no open SSH port at all, so it moves on. The knock sequence itself carries no credentials, so it adds obscurity, not authentication. Key login still decides who gets in once the port opens.
A ssh port knocking setup usually runs through a small daemon that watches the firewall log for the knock sequence and then adds a temporary firewall rule for the knocking address. The rule expires after a set time, so the port closes again on its own. That expiry matters: a permanent open rule after one knock defeats the purpose, since the port stays visible to everyone afterward.
Step 3: Set Up Port Knocking Carefully
- Choose a knock sequence of three or four ports, using high numbers that no service uses, and keep the sequence secret like a password.
- Set the open window short, such as 30 seconds, so the knocker must log in promptly and the port closes fast.
- Test from a second address you control, confirming that SSH fails before the knock and succeeds after it.
- Keep a fallback path, such as provider console access, since a forgotten sequence locks you out as surely as a lost key.
- Log knock activity at first, so you can tell a wrong sequence from a daemon problem during testing.
Port knocking suits single-admin servers and owners who log in from a few known addresses. It suits teams poorly, since every member needs the sequence and the client tool, and every new member adds a leak path. For team servers, a VPN or a firewall allowlist of office addresses gives the same closed-port effect with less shared-secret juggling. Our guide to set up a firewall with UFW on Ubuntu Server 24.04 covers allowlist-style rules that work well for team access.
Downsides and Failure Modes
The main downside of a moved port is confusion: tools, teammates, and future you must remember it. The main downside of port knocking is fragility: networks that block or reorder packets can break the knock sequence, and some mobile networks make knocking unreliable. Both features also complicate incident response, since a responder who does not know the port or the sequence cannot reach the server quickly. Document both in your server runbook, and store the runbook outside the server itself.
Fail2ban still earns its place alongside both tricks, since it bans addresses that guess keys or hammer the open port after a knock. Our Fail2ban setup guide for SSH brute-force protection shows how to pair it with a moved port so bans apply to the new port too. And if you have not yet set up keys, start with our guide to set up SSH keys on a new VPS before adding either trick, since obscurity without key login is a thin shell.
The Quiet Door, Kept Honest
A moved port plus port knocking makes your SSH door quiet and invisible to casual scans. Keep the honest framing: the port change cuts noise, the knock adds obscurity, and key-only login plus a firewall do the actual protection. Set all four, document the port and the sequence outside the server, and test recovery before you need it.