PortGate

Resources · Port reference · port 22

Port 22 (SSH): what it is and how to secure it

Port 22 is SSH. It is usually meant to be open, so secure it with keys, no root password logins, and rate limits.

Check command

sudo sshd -T | grep -E "^(port|passwordauthentication|permitrootlogin|kbdinteractiveauthentication)"

Copied.

Port 22 is SSH, and unlike the database ports it is normally supposed to be reachable: it is how you get into the server. What makes it safe is the login rules: keys only, no password logins for root, and limits on repeated attempts. Every server with port 22 open sees thousands of guessing attempts a day.

What port 22 is for

OpenSSH listens on 22 for terminal logins, file copies (scp, sftp) and tunnels. On Ubuntu 24.04 the ssh.socket unit listens and starts the SSH service when someone connects.

Why a weak SSH setup is dangerous

Bots try common usernames and leaked passwords around the clock. One weak password, especially on root, gives an attacker a shell. The usual outcome is a cryptominer, a botnet client, or your server attacking others, which gets your IP blocked.

Check yours

See the settings SSH is really using, after all included files:

sudo sshd -T | grep -E "^(port|passwordauthentication|permitrootlogin|kbdinteractiveauthentication)"

Safe values:

  • passwordauthentication no and kbdinteractiveauthentication no
  • permitrootlogin prohibit-password or no

See how many failed logins you are getting:

sudo grep -cE "Failed password|Invalid user" /var/log/auth.log

Fix it

First, make sure key login works, or you will lock yourself out. From your computer:

ssh-copy-id root@YOUR_SERVER_IP
ssh root@YOUR_SERVER_IP    # should log in without asking for a password

Then turn off passwords in a file that is read first, so cloud images cannot switch them back on:

echo -e "PasswordAuthentication no\nKbdInteractiveAuthentication no\nPermitRootLogin prohibit-password" | sudo tee /etc/ssh/sshd_config.d/00-hardening.conf
sudo sshd -t && sudo systemctl reload ssh

Keep your current session open and test a new login in a second window before you close anything.

Then limit repeated attempts, with UFW's rate limit, fail2ban, or both:

sudo ufw limit 22/tcp
sudo apt install -y fail2ban

If you want fewer people to even see port 22

  • Allow SSH only from your own IP or office range: sudo ufw allow from 203.0.113.10 to any port 22 proto tcp, then remove the general rule.
  • Put SSH behind a VPN such as WireGuard or Tailscale.
  • Moving SSH to another port cuts log noise, but it is not protection. On Ubuntu 24.04, a port change also needs sudo systemctl daemon-reload && sudo systemctl restart ssh.socket.

Common mistakes

  • Editing /etc/ssh/sshd_config while a cloud-init file in sshd_config.d still says PasswordAuthentication yes. The first value read wins, which is why the fix above uses a file named 00-....
  • Turning off passwords before a key works. Always test a key login in a second window first.
  • Sharing one key or the root password across many servers. One leak opens them all.

FAQ

Should I close port 22? Only if you have another way in, such as a VPN or your provider's console. Otherwise harden it as above.

What do the thousands of failed logins mean? They are automated scans. They are harmless while passwords are off. A sudden successful login from an unknown IP is not.

How do I know if someone turns passwords back on? PortGate's Server Guard checks the effective SSH settings on every heartbeat, counts failed logins, and alerts when password or root login is switched back on.