SSH Port Number Explained: Default, Uses, and Changes
SSH (Secure Shell) uses port 22 by default. This article explains what that port does, why it matters, and how to change it for better security.
What Is SSH and Why Does It Need a Port?
SSH is a network protocol that lets you securely log into another computer over an unsecured network. It encrypts all data between your client and the remote server, so passwords and commands cannot be read by eavesdroppers. Like any network service, SSH listens on a specific port number, which is a virtual endpoint that directs traffic to the right application. Port 22 is the well-known, registered port for SSH, assigned by the Internet Assigned Numbers Authority (IANA).
How Port 22 Became the Default
The SSH protocol was developed in 1995 by Tatu Ylönen, a researcher at the Helsinki University of Technology. He designed it as a secure replacement for Telnet, which used port 23. When the SSH protocol was formalized, port 22 was chosen and later registered with IANA. This made it the standard port that SSH clients automatically try to connect to unless you specify a different one.
How SSH Ports Work
When you run ssh user@host, your client assumes the server is listening on port 22. The server runs an SSH daemon (usually sshd) that binds to that port and waits for connections. The port number is part of the TCP/IP header, so the operating system knows which process to deliver the incoming data to. If you want to use a different port, you must tell the client with -p (lowercase) or Port in the config file, and configure the server to listen on that port.
Why Change the SSH Port?
Changing the default port is a common security hardening step. The main reason is to reduce automated attacks. Bots continuously scan the internet for port 22, trying to guess passwords or exploit vulnerabilities. Moving SSH to a non-standard port (e.g., 2222) makes these automated scans much less likely to find it. It is not a true security measure, because a determined attacker can still discover the port with a full scan, but it significantly cuts down on noise and log entries.
How to Change the SSH Port
To change the SSH port on a Linux server, follow these steps:
- Open the SSH configuration file, usually
/etc/ssh/sshd_config, with root privileges. - Find the line that says
#Port 22and change it to your desired port, e.g.,Port 2222. - Save the file and restart the SSH service:
sudo systemctl restart sshd(orservice ssh restarton older systems). - On the client side, connect with
ssh -p 2222 user@host.
Make sure your firewall allows the new port. If you are using a cloud provider, you may need to update the security group rules as well.
Common Problems and Misconceptions
One common mistake is confusing the SSH client port option (-p) with the SCP option (-P, uppercase). SCP uses -P for port, while SSH uses lowercase -p. Another issue is forgetting to update firewall rules, which results in a connection timeout. Some people believe that changing the port alone makes the server unhackable, but it is only one layer. Always use strong passwords or, better, SSH keys, and disable password authentication if possible.
SSH Port vs. Other Ports
Port 22 is specifically for SSH. Other related ports include:
- Port 23 – Telnet (unencrypted, obsolete for secure use)
- Port 21 – FTP (file transfer, also unencrypted)
- Port 443 – HTTPS (often used for SSH over TLS, e.g., to disguise traffic)
Using port 443 for SSH can help in networks that block all non-standard ports, but it may conflict with a web server on the same machine.
Security Best Practices
Beyond changing the port, consider these measures:
- Use SSH key authentication instead of passwords.
- Disable root login over SSH (
PermitRootLogin no). - Use a firewall to restrict which IPs can connect to the SSH port.
- Install fail2ban to block repeated failed login attempts.
Frequently Asked Questions
Is SSH always on port 22?
Not necessarily. While 22 is the default and standard, the server can be configured to listen on any port. The client must then specify that port.
Can I use a port above 65535?
No. TCP and UDP ports range from 0 to 65535. Any value outside that is invalid.
What is the difference between port 22 and port 2222?
Port 2222 is an unofficial alternative often used to avoid automated scans. There is no technical difference other than the number; both work the same if configured correctly.
Does changing the SSH port affect performance?
No. The port number does not affect speed or encryption. It only changes which door the traffic enters.
Why do I get “Connection refused” after changing the port?
This usually means the SSH service is not listening on the new port. Check that you edited the correct file, restarted the service, and that the firewall allows the new port.
Can I run multiple SSH servers on different ports?
Yes, you can run multiple instances of sshd on different ports, but it is rarely needed. You would need separate configuration files and start commands.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.