Skip to content
Security

Change the SSH port and secure it with fail2ban

Change the SSH port safely without locking yourself out, update the firewall and fail2ban jail, and why SSH keys matter more than the port number.

By WP24×7 engineers5 min readPublished September 1, 2021Updated November 29, 2026

At a glance

Bots scan SSH port 22 all day, so moving SSH to another port removes most log noise, but it is a tidy-up and not a defence. The real protection is key-based login, no root login and fail2ban banning repeat offenders. This guide shows how to open the new port first, change it, test in a second session and point fail2ban at it without locking yourself out.

Updated November 29, 2026

  • For people running a Linux VPS or server they control
  • Safe order open firewall port, edit sshd_config, test, then close port 22
  • Keep an existing session open and know the provider's recovery console
  • fail2ban jail must name the new port
  • Matters more than the port SSH keys, no root login, patching
  • Panel-managed servers change the port in the panel
  • Help security service and support plans

Bots scan port 22 all day, filling your logs with failed logins. Moving SSH to another port removes most of that noise. It is a tidy-up, not a defence: the real protection is key-based login, no root login and fail2ban banning repeat offenders. Open the new port in your firewall before you change it, and keep your current session open until a new one works.

This guide is for a Linux VPS or server you control. If a hosting panel such as RunCloud manages the firewall and SSH settings, change the port there so the panel does not undo your edits. Take a snapshot or backup first.

What should I do before touching the port?

  1. Keep an existing SSH session open throughout. If you close it and the change is wrong, you may need your provider's web console to recover.
  2. Know your recovery path. Find the provider's rescue or VNC console now.
  3. Pick a port between 1024 and 65535 that nothing else uses, for example 2222 or a random high number. Check with ss -tlnp.
  4. Allow the new port in every firewall layer: the server firewall (ufw, firewalld or iptables) and any provider-level firewall or security group.

On Ubuntu or Debian:

sudo ufw allow 2222/tcp
sudo ufw status

How do I change the SSH port?

Edit the SSH server configuration:

sudo nano /etc/ssh/sshd_config

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

Port 2222

Check the file for mistakes before you restart:

sudo sshd -t

No output means the syntax is valid. On Debian and Ubuntu the service is called ssh, while on RHEL, Rocky and AlmaLinux it is sshd.

Distribution familyService nameFirewall tool
Debian, Ubuntusshufw
RHEL, Rocky, AlmaLinuxsshdfirewalld, SELinux label for the port

For Debian and Ubuntu:

sudo systemctl restart ssh

On recent Ubuntu releases, SSH can be started by a socket unit (ssh.socket), and in that case the Port line may be ignored. If ss -tlnp | grep ssh still shows port 22 after the restart, check systemctl status ssh.socket and either override the socket's ListenStream or disable socket activation, according to your release's documentation.

On RHEL-family systems with SELinux enforcing, a non-standard port must be labelled first:

sudo semanage port -a -t ssh_port_t -p tcp 2222

How do I test it?

Open a second terminal window and connect without closing the first:

ssh -p 2222 youruser@your.server.ip

Only after it works should you close port 22 in the firewall (sudo ufw delete allow 22/tcp) and leave the first session. Update saved connection settings in your SFTP client, deployment tools and backup jobs, or they will fail.

How do I make fail2ban watch the new port?

fail2ban reads log files and bans offending IPs through the firewall. Install it if needed (sudo apt install fail2ban), then create a local override. Never edit jail.conf, because package updates overwrite it:

sudo nano /etc/fail2ban/jail.local
[sshd]
enabled  = true
port     = 2222
maxretry = 5
findtime = 10m
bantime  = 1h

Restart and check:

sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

You should see the jail listed, with counters for failed attempts and banned IPs. Add your own address to ignoreip in the [DEFAULT] section if you do not want to ban yourself after typing a wrong password a few times. A slightly longer bantime and the recidive jail, which bans repeat offenders for longer, are common extras.

What matters more than the port number?

Do these as well, in this order of importance:

Keys, no root login and fail2ban do the real work
MeasureHow
SSH keys, no password loginPasswordAuthentication no after confirming a key works in a second session
No root loginPermitRootLogin no and a normal user with sudo
Only needed usersAllowUsers deploy
Restrict by IPIf you always connect from a fixed address
Keep the server updatedSecurity patches matter far more than obscurity

What if I locked myself out?

Use your provider's web console or recovery mode, log in there, and review sshd_config, the firewall status and the socket unit. Revert the Port line to 22 if needed and restart. Changes made through a hosting panel can be reverted in the panel.

How do I know fail2ban is actually banning attackers?

Look at the log and the jail status together. sudo fail2ban-client status sshd shows total failed attempts and the currently banned list. sudo tail -f /var/log/fail2ban.log shows bans and unbans as they happen. If the failed counter stays at zero on a busy server, the jail is reading the wrong log file or the wrong backend, which on systemd-only systems often means setting backend = systemd in the jail. Unban yourself with sudo fail2ban-client set sshd unbanip YOUR.IP if you trigger a ban while testing.

Should I do this myself?

If you manage servers regularly, yes, and keep notes. If you run a business site on a VPS you do not fully understand, an error here can cut off access to your own store. Our security team hardens servers and WordPress sites together, and a support plan covers the ongoing patching.

Server access too risky to touch alone?

A person will review the setup and say what to change.

FAQ

Questions people ask

13 answers. Can't find yours? Ask an engineer.

Does changing the SSH port make a server secure?

It reduces log noise from bots that scan port 22, but it is not real security. Key-based login, disabling root login and fail2ban or a firewall matter far more.

Link to this answer
Why did I lock myself out after changing the port?

The usual reasons are not opening the new port in the firewall first, not testing before closing the old session, or a service manager that still listens on port 22.

Link to this answer
Should fail2ban and the firewall use the same port?

Yes. fail2ban watches the logs and bans through the firewall, so its jail must name the new port, or it will watch the wrong one.

Link to this answer
Does changing the SSH port stop attacks?

It reduces log noise from bots that scan port 22 but is not real security. Key-based login, disabling root login, patching and fail2ban or a firewall matter far more.

Link to this answer
Why did I lock myself out after changing the port?

Usual reasons are not opening the new port in the firewall first, closing the old session before testing, or a socket unit still listening on port 22. A provider web console is the recovery path.

Link to this answer
In what order should I do the change?

Keep a session open, allow the new port in every firewall layer including any provider firewall, edit sshd_config, run sshd -t, restart, test from a second terminal and only then close port 22.

Link to this answer
Why might the Port line be ignored on Ubuntu?

Recent releases can start SSH through a socket unit, ssh.socket. If ss -tlnp still shows port 22 after restart, check systemctl status ssh.socket and override ListenStream or disable socket activation as your release documents.

Link to this answer
What extra step does SELinux need?

On RHEL-family systems with SELinux enforcing, label the non-standard port first with semanage port -a -t ssh_port_t -p tcp and your port number.

Link to this answer
Why must fail2ban use the same port?

fail2ban reads logs and bans through the firewall. If its jail names the wrong port, it watches or bans the wrong one. Put overrides in jail.local, never jail.conf, which updates overwrite.

Link to this answer
How do I know fail2ban is banning anyone?

Run fail2ban-client status sshd for totals and the banned list, and tail /var/log/fail2ban.log. If the failed counter stays at zero on a busy server, the jail may be reading the wrong log or backend, so try backend = systemd.

Link to this answer
How do I unban my own address?

Run fail2ban-client set sshd unbanip with your IP. You can also add your address to ignoreip in the DEFAULT section so testing does not ban you.

Link to this answer
What else should I harden besides the port?

Use SSH keys and disable password login after confirming a key works, set PermitRootLogin no, allow only needed users, restrict by IP if you connect from a fixed address and keep the server updated.

Link to this answer
Should I do this myself on a business server?

If you manage servers regularly, yes, with notes. If you run a business site on a VPS you do not fully understand, an error can cut off access to your own store, so ask for help.

Link to this answer

Keep reading

Want an engineer on it?

Tell us what is wrong in plain words. A person reads it, today.