Docs

How to change the SSH port safely

Move SSH from port 22 to another port without locking yourself out: open the new port in CSF first, change sshd_config, test in a second session, then close port 22.

How-to over SSH AlmaLinux, Rocky Linux, Ubuntu, Debian 3 min read Updated

Moving SSH away from port 22 does not make it secure on its own, but it removes most of the automated password guessing from your logs. The real protection is signing in with keys only; do both.

The order of the steps matters: the firewall must allow the new port before SSH moves to it, and the old session must stay open until the new one works.

Before you start#

  • Pick a port between 1024 and 65535 that nothing else uses, for example 2222 or 49222. Check it is free:
ss -tlnp | grep -w 2222 || echo "port 2222 is free"
  • Keep your current SSH session open for the whole procedure.
  • If your provider has a network firewall or security group in front of the server, allow the new port there as well.

Step 1: Open the new port in CSF#

Either in the panel, under Security → Firewall & Settings → Ports, add the port to Incoming TCP, or over SSH:

grep -E '^TCP6?_IN' /etc/csf/csf.conf

Add the new port to both TCP_IN and TCP6_IN, keeping 22 for now:

sed -i -E 's/^(TCP6?_IN = ")/\12222,/' /etc/csf/csf.conf
grep -E '^TCP6?_IN' /etc/csf/csf.conf
csf -r

Step 2: Change the port in sshd_config#

See where a Port line is set today:

grep -RniE '^\s*Port\s' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/ 2>/dev/null

Edit /etc/ssh/sshd_config and set the port (replace a #Port 22 or Port 22 line rather than adding a second one):

sed -i -E 's/^#?\s*Port\s+[0-9]+/Port 2222/' /etc/ssh/sshd_config
grep -nE '^Port' /etc/ssh/sshd_config

If a file in /etc/ssh/sshd_config.d/ also sets Port, change it there too.

Step 3: Check the configuration and restart SSH#

On AlmaLinux and Rocky Linux:

sshd -t && systemctl restart sshd
ss -tlnp | grep -w 2222

On Ubuntu and Debian the service is named ssh:

sshd -t && systemctl restart ssh
ss -tlnp | grep -w 2222

On Ubuntu 24.04 and later, SSH can be started by a socket that takes its port from the same file. If systemctl is-active ssh.socket answers active, apply the change with:

sshd -t && systemctl daemon-reload && systemctl restart ssh.socket ssh
ss -tlnp | grep -w 2222

sshd -t prints nothing when the file is valid. Do not restart if it prints an error. Open sessions stay connected during a restart.

Step 4: Test in a new window#

From your own computer, in a new terminal, keeping the old session open:

ssh -p 2222 root@your-server

Only continue when this works.

Step 5: Close port 22#

Remove 22 from TCP_IN and TCP6_IN (in the panel's Ports tab, or by editing /etc/csf/csf.conf), then reload the firewall:

csf -r

Step 6: Tell your tools#

Update the port in your SSH client's configuration, backup scripts, monitoring and any Server Migration connection that reaches this server. In ~/.ssh/config on your computer:

Host myserver
    HostName 203.0.113.10
    Port 2222
    User root

If you locked yourself out#

Use your provider's console (VNC, KVM or serial console) to sign in, then either open the port in /etc/csf/csf.conf and run csf -r, or set Port 22 back and restart SSH as in step 3. If CSF blocked your address after failed attempts, unblock it with csf -dr YOUR.IP and csf -tr YOUR.IP.

Frequently asked questions

Does LFD still protect SSH on the new port?

Yes. LFD reads the SSH log, not the port, so failed sign-ins are counted and blocked as before.

Should I also switch off password sign-in?

Yes. Add your key under Security → Root SSH Keys, test it, then untick Accept passwords under SSH Password Sign-in. The panel will not let you switch passwords off while root has no key.

Will the Security Checkup still warn me?

The checkup warns about the standard port 22 and about password sign-in. Once both are changed, those warnings clear.

Was this article helpful?

Something missing? Ask in the community or open a ticket.