Before You Start
- Pick a port: choose a free port between 1024 and 65535. The examples use 2222.
- Keep your session open: do not close the current SSH session until you have logged in on the new port from a second terminal.
- Know your way back in: check that you can reach the server console in your provider's control panel in case something goes wrong.
Step 1: Open the New Port in the Firewall
Do this first. On Ubuntu and Debian with ufw:
sudo ufw allow 2222/tcpOn AlmaLinux and Rocky Linux with firewalld:
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reloadIf your provider has a firewall in front of the server, open the port there as well.
Step 2: Tell SELinux About the Port
AlmaLinux and Rocky Linux run SELinux, which lets the SSH service bind only to ports labeled for it. Add the new one:
sudo semanage port -a -t ssh_port_t -p tcp 2222If the command is missing, install it with sudo dnf install policycoreutils-python-utils. Ubuntu and Debian do not need this step.
Step 3: Edit the SSH Configuration
Open the file:
sudo nano /etc/ssh/sshd_configFind the line #Port 22, remove the #, and set the new number:
Port 2222Check the file for mistakes before restarting anything. No output means it is valid:
sudo sshd -tStep 4: Restart SSH
| System | Command |
|---|---|
| Ubuntu 24.04 and later | sudo systemctl daemon-reload && sudo systemctl restart ssh.socket |
| Ubuntu 22.04 and Debian | sudo systemctl restart ssh |
| AlmaLinux and Rocky Linux | sudo systemctl restart sshd |
Recent Ubuntu releases start SSH through a systemd socket, which reads the port from the same file but only after a reload. That is why the first row has two commands.
Step 5: Test from a Second Terminal
Leave the old session open and connect on the new port:
ssh -p 2222 user@your_server_ipOn the server, confirm what SSH is listening on:
sudo ss -tlnp | grep sshStep 6: Close Port 22
Only after the test works:
sudo ufw delete allow 22/tcpOn firewalld, remove the ssh service: sudo firewall-cmd --permanent --remove-service=ssh, then reload.
Save the Port on Your Computer
To avoid typing -p every time, add the server to ~/.ssh/config:
Host myserver
HostName your_server_ip
User user
Port 2222After that, ssh myserver is enough. scp and sftp read the same file.
Does a New Port Make SSH Secure?
It hides the service from simple scans; it does not protect a weak password. Treat it as tidying up, and rely on the real measures: log in with SSH keys, turn off password login, do not allow direct root login, and block repeated failures with a tool such as fail2ban. Key login is covered in how to use PuTTY to SSH into a server.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations