
SSH (Secure Shell) is how you log in to and manage Linux servers remotely, transfer files with SFTP and SCP, and tunnel other traffic securely. Like every network service, it listens on a port number. This guide explains what the default SSH port is, why it is port 22, whether you should change it, how to change it safely on Ubuntu (including the socket-activation change in 22.10 and later), Debian, and RHEL-based systems, and how to connect to a server on a custom port.
Quick answerThe default SSH port is TCP 22. To use another port, set Port 2222 in /etc/ssh/sshd_config, allow the new port in your firewall, restart SSH, and connect with ssh -p 2222 user@server. On Ubuntu 24.04 and later, also run sudo systemctl daemon-reload and restart ssh.socket.
Why is the SSH port 22?
When Tatu Ylönen wrote the first version of SSH in 1995, he wanted it to replace the insecure Telnet (port 23) and FTP (port 21). Port 22 sat neatly between them and was unassigned, so he requested it from the Internet Assigned Numbers Authority (IANA), which registered it for SSH. Ports below 1024 are "well-known" ports that only root can bind to, which is one reason a system SSH daemon uses one.
Port 22 is used by SSH itself and everything that runs over it: SFTP, SCP, rsync over SSH, Git over SSH, and SSH tunnels.
Check which port SSH is using
sudo ss -tlnp | grep ssh
grep -i '^Port' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*.conf
sudo sshd -T | grep -i '^port'
ss shows what is actually listening. sshd -T prints the effective configuration after all include files are read.
Should you change the SSH port?
Changing the port is not a security measure on its own: a port scan finds SSH on any port within seconds. What it does do is cut the huge volume of automated login attempts that target port 22, which keeps logs readable and reduces noise. Treat it as a small extra on top of the measures that actually protect you:
- Use SSH keys and set
PasswordAuthentication no. - Disable direct root login with
PermitRootLogin no(orprohibit-password). - Install Fail2ban to block repeated failures.
- Limit SSH to known IP addresses in the firewall where possible.
- Keep OpenSSH updated.
Pick a port above 1024 that no other service uses, for example 2222 or something less common such as 49222.
How to change the SSH port safely
Keep your current SSH session open throughout. If something goes wrong, you can fix it from that session instead of losing access.
Step 1: Allow the new port in the firewall
Ubuntu and Debian with UFW:
sudo ufw allow 2222/tcp
RHEL, Rocky, AlmaLinux and CentOS with firewalld:
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
If your provider has a network firewall or security group, open the port there as well.
Step 2: Set the port in the SSH configuration
Create a drop-in file so package updates do not overwrite your change:
echo "Port 2222" | sudo tee /etc/ssh/sshd_config.d/10-port.conf
sudo sshd -t
If your system has no sshd_config.d directory, edit /etc/ssh/sshd_config and change #Port 22 to Port 2222. sshd -t checks the syntax; it prints nothing when the configuration is valid.
Step 3 (RHEL family): tell SELinux about the port
sudo semanage port -a -t ssh_port_t -p tcp 2222
Without this, SELinux blocks sshd from binding to the new port. Install policycoreutils-python-utils if semanage is missing.
Step 4: Restart SSH
Ubuntu 22.10 and later start SSH through systemd socket activation, so the listening port is managed by ssh.socket. On Ubuntu 24.04 and later, the port from sshd_config is read automatically when systemd reloads:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
On Debian, Ubuntu 22.04 and earlier:
sudo systemctl restart ssh
On RHEL-based systems the service is called sshd:
sudo systemctl restart sshd
Confirm with sudo ss -tlnp | grep 2222.
Step 5: Test, then close port 22
From a new terminal on your computer:
ssh -p 2222 user@203.0.113.10
Only after this works, remove the old rule (sudo ufw delete allow 22/tcp or sudo firewall-cmd --permanent --remove-service=ssh followed by a reload).
Connecting to a custom SSH port
- Command line:
ssh -p 2222 user@host; for SCP use a capital P:scp -P 2222 file user@host:/path; for SFTP:sftp -P 2222 user@host. - Config file: add an entry to
~/.ssh/configso you never have to type the port:
Then simply runHost myvps HostName 203.0.113.10 User deploy Port 2222ssh myvps. - PuTTY, WinSCP, FileZilla: enter the port in the Port field next to the host name.
Frequently asked questions
What is the default SSH port number?
TCP port 22. SFTP and SCP use the same port because they run over SSH.
Is SSH TCP or UDP?
SSH uses TCP. You only need to open the TCP port in your firewall.
Is changing the SSH port more secure?
Only slightly. It reduces automated attacks and log noise, but real protection comes from key-only authentication, disabling root login, Fail2ban and firewall restrictions.
I changed the port on Ubuntu but SSH still listens on 22. Why?
Ubuntu 22.10 and later use ssh.socket. Run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket. On 22.10 to 23.10 you may need to set the port in a ssh.socket override with sudo systemctl edit ssh.socket instead.
I locked myself out after changing the port. What now?
Log in through your provider's VNC or web console, then check that the firewall allows the new port, that SELinux has the port label on RHEL systems, and that sshd -t reports no errors.


