HiBlog Linux and Windows Server HiCademy
How to Change the SSH Port on Linux (Ubuntu, Debian, AlmaLinux) Without Locking Yourself Out
- A. MounesiAuthor
- Last updated
- 6 minutesReading time
- 1,386Views
- A. Mounesi
- Last updated:
- 6 min read
- 1,386 views
You’ve just set up a Linux server, and one look at the login log shows bots from all over the world trying passwords on port 22 from the very first hour. Changing SSH’s default port shakes off most of these bots and keeps your logs clean. Just one catch: if you do things in the wrong order, you lock the door on yourself too!
In this guide we change the SSH port on Ubuntu, Debian, AlmaLinux and Rocky Linux, open it in the firewall and test the new connection before closing the old port.
Short answer: first open the new port in the firewall, then change the line
Port 22in/etc/ssh/sshd_configto the new port and restart SSH. On Ubuntu 22.10 and later, runsystemctl daemon-reloadand thensystemctl restart ssh.socket. Keep your current SSH window open until you’ve connected on the new port and seen that it works.
Why change the SSH port?
SSH listens on port 22 by default, and automated bots scan the whole internet for that port to try common passwords. With a different port, most of those attempts never reach your server. But changing the port alone isn’t real security; key-based login and disabling password login matter more. We cover all of these settings in how to enable and secure SSH on Ubuntu, and if SSH itself is new to you, read what SSH is.
Before you start: keep a way back
- Don’t close the window you’re connected with until you’re done; the current connection isn’t dropped when the port changes, and if something goes wrong you fix it from that window.
- Have a second way in. On HiData’s Iran VPS, the VNC console in the server panel works even when SSH doesn’t, with no ticket needed.
- Pick a new port between 1024 and 65535 and check that no other service uses it. In this guide we use port 7743. This command shows the ports in use:
sudo ss -tlnp1. Open the new port in the firewall
This step has to come before changing the port; otherwise the firewall blocks connections to the new port.
On Ubuntu and Debian with UFW:
sudo ufw allow 7743/tcpOn AlmaLinux, Rocky Linux and CentOS with firewalld:
sudo firewall-cmd --permanent --add-port=7743/tcp
sudo firewall-cmd --reloadThese distributions also run SELinux, which only lets SSH listen on ports it has been told about. Register the new port with SELinux (the semanage command is in the policycoreutils-python-utils package):
sudo semanage port -a -t ssh_port_t -p tcp 7743With iptables: if your server’s firewall is set up directly with iptables, add the new port’s rule above the other rules, and remember that iptables rules are lost on reboot without a tool such as iptables-persistent:
sudo iptables -I INPUT -p tcp --dport 7743 -j ACCEPT2. Change the port in the SSH configuration
Open the SSH configuration file with the nano editor:
sudo nano /etc/ssh/sshd_configFind the line #Port 22, remove the # at its start and change the number to the new port:
Port 7743Save the file with Ctrl+O and Enter and exit with Ctrl+X. Before restarting, test the configuration; if this command prints nothing, the file is fine:
sudo sshd -t3. Restart SSH
The right command depends on the operating system version. On Ubuntu 22.10 and later, including 24.04, SSH is started by ssh.socket, and restarting the ssh service alone doesn’t change the port:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socketIf you aren’t sure which method your server uses, run systemctl status ssh.socket; if it says active, use these two commands.
On Ubuntu 22.04 and older, and on Debian:
sudo systemctl restart sshOn AlmaLinux, Rocky Linux and CentOS:
sudo systemctl restart sshdCheck with this command that the new port is now open and waiting for connections:
sudo ss -tlnp | grep 77434. Test the connection on the new port
Keep the current window open and connect on the new port from a new terminal:
ssh -p 7743 root@IPIf you get in, you’re done. If not, check the firewall and sshd_config again from the old window. Give the port to your other tools as well: scp and sftp take the port with a capital P (scp -P 7743), and in apps such as PuTTY and FileZilla you enter the number in the Port field.
5. Close the old port
Now that the new connection works, close port 22 in the firewall. In UFW:
sudo ufw delete allow OpenSSH
sudo ufw delete allow 22/tcp(Whichever of these two rules you added earlier is removed, and the other reports that there is no such rule.) In firewalld:
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --reloadA tip: save the port on your own computer
To avoid typing -p 7743 every time, create the file ~/.ssh/config on your own computer and put these lines in it; from then on, ssh myserver is all you need:
Host myserver
HostName 203.0.113.10
User root
Port 7743Frequently asked questions
I can’t connect after changing the port. What now?
Check three things: the new port is open in the firewall, you restarted ssh.socket on newer Ubuntu, and on AlmaLinux and Rocky Linux you registered the port with SELinux. If you already closed the old window, log in through the VNC console in the server panel and fix the settings.
Can SSH listen on two ports?
Yes. Put two Port lines in sshd_config, for example Port 22 and Port 7743. This is safer during the switch; once the new port is tested, remove the port 22 line.
Is changing the SSH port enough for security?
No. It only keeps simple bots away. SSH key login, disabled password login and tools such as Fail2ban, which block the IPs of failed attempts, provide the real security.
A Linux VPS with full access
On HiData’s Iran VPS with full root access, you choose Ubuntu, Debian or AlmaLinux when you order and set up SSH however you like; and if SSH is ever locked, the VNC console in the server panel is available without a ticket. See all plans on the VPS page and the day-one server tasks in how to set up and use a VPS.


