Language
TR EN
Currency
20% off Virtual Servers – LIGHT20
Home / Blog / DDoS & Security / How to Change the SSH Port (Step-by-Step Security)
Changing the SSH port and server security

How to Change the SSH Port (Step-by-Step Security)

Port 22 on every internet-facing server is scanned all day by password-guessing bots. Changing the port removes most of that noise. It is not a firewall on its own, but it cleans up your logs and reduces brute-force attempts.

1. Pick a new port

Choose a port between 1024 and 65535 that no other service uses. Avoid clashing with web (80/443), game or database ports.

2. Edit the configuration

nano /etc/ssh/sshd_config

Find the #Port 22 line, remove the # and set your new port:

Port 2222

3. Open the port in the firewall

Skip this and you will not be able to connect. Ubuntu/Debian (ufw):

ufw allow 2222/tcp

AlmaLinux/CentOS (firewalld):

firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

On systems with SELinux you also need to allow the port:

semanage port -a -t ssh_port_t -p tcp 2222

4. Restart the service — but keep your session open

systemctl restart sshd

Important: do not close your current SSH session. Open a second terminal and try the new port:

ssh -p 2222 root@SERVER_IP

If it works, close the old session. If it does not, you can revert from the session you kept open. This order is what stops you from locking yourself out.

Changing the port is not enough

  • Disable password login: after setting up SSH keys, PasswordAuthentication no makes brute force useless.
  • Restrict root login: PermitRootLogin prohibit-password allows key login and blocks passwords.
  • Install fail2ban: it temporarily blocks IPs after repeated failures.
  • Restrict by IP when possible: with a static IP, open the SSH port only to that address.

Moving to key-based login (recommended order)

  1. Generate a key on your own computer: ssh-keygen -t ed25519
  2. Copy the public key to the server: ssh-copy-id root@SERVER_IP
  3. Open a new terminal and confirm the key login works.
  4. Only then set PasswordAuthentication no in sshd_config and restart the service.

The order matters: never disable password login before you have confirmed key login works.

Automatic blocking with fail2ban

apt install fail2ban
systemctl enable --now fail2ban
fail2ban-client status sshd

Even with default settings, fail2ban temporarily blocks an IP after repeated failures. If you moved SSH to a new port, update the port in the jail configuration.

Checklist

  • Is the new port open in the firewall?
  • Have you confirmed a connection on the new port?
  • Does the SSH key work?
  • Is password login disabled?
  • Is direct root password login restricted?
  • Is fail2ban running?

Moving to key-based login (recommended order)

  1. Generate a key on your own computer: ssh-keygen -t ed25519
  2. Copy the public key to the server: ssh-copy-id root@SERVER_IP
  3. Open a new terminal and confirm the key login works.
  4. Only then set PasswordAuthentication no in sshd_config and restart the service.

The order matters: never disable password login before you have confirmed key login works.

Automatic blocking with fail2ban

apt install fail2ban
systemctl enable --now fail2ban
fail2ban-client status sshd

Even with default settings, fail2ban temporarily blocks an IP after repeated failures. If you moved SSH to a new port, update the port in the jail configuration.

Checklist

  • Is the new port open in the firewall?
  • Have you confirmed a connection on the new port?
  • Does the SSH key work?
  • Is password login disabled?
  • Is direct root password login restricted?
  • Is fail2ban running?

A note for game and web servers

These steps protect management access. Volumetric attacks against your website or game server are handled at a different layer; on our services Layer 3, 4 and 7 attacks are filtered by the RouteFence® infrastructure with 8 global POP locations and 6+ Tbps of filtering capacity.

Frequently asked questions

What if I cannot log in after changing the SSH port?

Keep your current session open so you can revert. Do not close it until you have confirmed a working connection on the new port.

Is changing the port enough for security?

No. It reduces automated scanning, but real protection comes from SSH keys, disabling password login and limiting attempts with tools like fail2ban.

Need a server for this setup?

See our DDoS protection

Related guides

Server Guides SSH with PuTTY and Your First Server Setup DDoS & Security What Is a DDoS Attack? Layers and How to Defend