To change the default SSH port and harden sshd, edit /etc/ssh/sshd_config (or a drop-in file in /etc/ssh/sshd_config.d/), set Port, disable root and password logins, restrict which users may connect, test the configuration with sudo sshd -t, and reload the service. Open the new port in your firewall before you restart, and keep your current session open until a second session confirms the new settings work.
Moving the port does not make SSH secure. It only reduces log noise from bots that scan port 22. The real protection comes from key-only authentication and a short list of allowed users.
Automated scanners try port 22 on every public address. A non-standard port such as 2222 or 49152 cuts the number of failed login lines in your logs, which makes real problems easier to spot. It is not a security boundary, because a targeted attacker can scan all ports in minutes.
Treat the port change as housekeeping and put your effort into the hardening options below.
The server reads /etc/ssh/sshd_config and, on current Ubuntu and Debian releases, every *.conf file in /etc/ssh/sshd_config.d/. Files in that directory are read first, and for most options the first value found wins. A drop-in file is the cleanest place for your changes because package upgrades will not overwrite it.
Create one:
Cloud images often ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. Because files are read in alphabetical order, a name starting with 10- is read before it and wins.
Before you disable passwords, confirm that key login works for your user:
Then open the new port in the firewall while the old one stays open:
If your provider has a cloud firewall, open the port there too.
Put this in the drop-in file:
What each line does:
| Setting | Effect |
|---|---|
Port 2222 | Listens on port 2222 instead of 22 |
PermitRootLogin no | Blocks direct root logins; use a normal user and sudo |
PasswordAuthentication no | Only SSH keys are accepted |
KbdInteractiveAuthentication no | Closes the keyboard-interactive route that can still accept passwords |
AllowUsers deploy | Only the listed users may log in |
MaxAuthTries 3 | Disconnects after three failed attempts per connection |
LoginGraceTime 30 | Gives 30 seconds to authenticate |
X11Forwarding no | Disables graphical forwarding you rarely need on a server |
Modern OpenSSH already defaults to strong algorithms, so you rarely need to hand-pick ciphers. Keep the package updated instead.
Check the syntax first. A typo here can stop sshd from starting:
No output means the file is valid. You can also print the effective values with sudo sshd -T | grep -E "port|permitrootlogin|passwordauthentication".
On Ubuntu 22.10 and later, sshd is started through socket activation by default, which means the Port setting can be ignored because ssh.socket decides the listening port. If the port does not change after a restart, either override the socket or disable it:
Recent Ubuntu releases may handle this differently, so check with ss after the restart. Then reload:
The unit is called ssh on Ubuntu and Debian, and sshd on RHEL-family systems.
Keep your existing terminal open. In a new terminal, connect with the new port:
If it works, close the old firewall rule with sudo ufw delete allow OpenSSH. If it fails, fix the file from the session you left open. Add a host entry to ~/.ssh/config so you do not type the port every time:
It reduces scanner noise, but it does not stop a determined attacker. Do it in addition to key-only login, not instead of it.
Use your provider's web or rescue console to log in and fix the configuration. This is why you test from a second session before closing the first.
Yes for most servers. Use a normal account with sudo so every administrative action is tied to a named user.
Ed25519 is a good default: short, fast and supported by all current OpenSSH versions. Use RSA with at least 3072 bits only when an old system requires it.
Use a drop-in file in /etc/ssh/sshd_config.d/, set a new Port, disable root and password logins, limit AllowUsers, and validate with sshd -t. Open the new port first, restart, and confirm from a second session before you close the first.