Port 22 (SSH)
Key facts
- Port
- 22
- Protocols
- TCP
- Range
- System port (Well-Known), 0-1023
- IANA service name
- ssh
IANA assigns ports in this range to well-known services, under RFC 6335.
About port 22
Port 22 is the default port of SSH (Secure Shell), the protocol for encrypted remote login to a server and running commands, over TCP. The protocol encrypts the whole connection, including login details, which is why it's the standard way to manage Linux servers.
The SSH transport layer is defined in RFC 4253, and IANA registers port 22 for the ssh service.
What port 22 is used for
Beyond logging in to the command line, the same SSH connection serves other uses:
- File transfer: SFTP and scp transfer files over the encrypted SSH connection, on the same port.
- Working with Git: many Git services let you pull and push code over SSH, with a key instead of a password.
- SSH tunnels: you can carry traffic inside an SSH connection to a service that isn't exposed to the internet, such as a database on port 3306.
- Automation: deployment and backup tools connect to the server over SSH and run commands without manual intervention.
Port 22 in the firewall
On a VPS you manage, port 22 (or the port set in its place) must be open, or you won't be able to connect to the server. If you have a static IP address, we recommend allowing access only from it:
- UFW:
ufw allow from 203.0.113.10 to any port 22 proto tcp - firewalld:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" service name="ssh" accept'
Without a static address, you can keep the port open and rate-limit connections, for example with ufw limit 22/tcp. Before changing SSH rules in the firewall, keep your current connection open and test a new login in a separate window, to make sure you keep access to the server.
Risks and security
Port 22 open to the internet gets automated login attempts, and that's normal for any server. A few simple settings make sure those attempts don't succeed:
- Log in with an SSH key instead of a password, and once the key works, disable password login (
PasswordAuthentication no). - Block root login with a password (
PermitRootLogin prohibit-password), or log in as a regular user and use sudo. - Run fail2ban, which temporarily blocks addresses that make repeated failed login attempts.
- Keep up with security updates for the operating system and the SSH server.
Moving SSH to a different port reduces the number of automated attempts in the logs, but the real protection comes from key-based login and limiting access.
Commands to check and configure
-
ss -tlnp | grep sshdShows which port and address the SSH server is actually listening on.
-
nc -zv server.example.com 22Checks from your computer whether port 22 on the server is open and reachable.
-
ssh -v user@server.example.comConnects to the server with verbose output, showing the stage at which the connection stops.
-
ssh -p 2200 user@server.example.comConnects to an SSH server listening on a different port (2200 in this example).
-
sshd -tChecks the SSH server configuration file for errors before you reload the service.
-
ufw limit 22/tcpAllows access to port 22 and temporarily blocks an address that keeps trying to connect within a short time (UFW).
-
ufw allow from 203.0.113.10 to any port 22 proto tcpAllows SSH access only from the given IP address (UFW).
-
firewall-cmd --permanent --remove-service=sshRemoves general SSH access in firewalld, after you've added a rule that allows access from your address. Then run firewall-cmd --reload.
Port 22 in the IANA registry
| Port | Protocol | Service | IANA registry description |
|---|---|---|---|
| 22 | SCTP | ssh | SSH |
| 22 | TCP | ssh | The Secure Shell (SSH) Protocol |
| 22 | UDP | ssh | The Secure Shell (SSH) Protocol |
Data source: IANA Service Name and Transport Protocol Port Number Registry (opens in a new tab)
Port 22 FAQ
Can I change the SSH port?
Yes. Change the Port setting in the SSH server configuration file, open the new port in the firewall, and only then reload the service. On servers with SELinux enabled, you also need to allow the new port for SSH. The change reduces automated login attempts, and we recommend also logging in with an SSH key.
What's the advantage of logging in with an SSH key instead of a password?
An SSH key is much longer and more complex than any password, so in practice it can't be guessed. The private part of the key stays on your computer and is never sent to the server. Logging in is also more convenient: with ssh-agent, you don't need to type a password every time you connect.
What do the Connection refused and Connection timed out errors mean?
Connection refused usually means the SSH server isn't listening on the port you connected to, for example because the service isn't running or a different port is set. Connection timed out usually means something along the way is blocking the connection, in the server's firewall or in the network you're connecting from. The command ssh -v shows the stage at which the connection stops.
Does SFTP use the same port as SSH?
Yes. SFTP runs inside an SSH connection, so it uses the same port, usually port 22. You don't need to open another port in the firewall to transfer files over SFTP.