Port 22 is the TCP port assigned to SSH (Secure Shell). When you run ssh user@host without a port, your client connects to port 22 on that host, and an SSH server (sshd) listening there answers.
ssh [email protected]
ssh -p 22 [email protected]
Both commands do the same thing, because 22 is the default. The second one just writes it out. This article covers where the number comes from, what runs on it, how to tell if it is reachable, and how to change it. For how the protocol itself works, see what is SSH.
What port 22 is for
RFC 4253, the SSH transport layer specification, says in section 4.1: "When used over TCP/IP, the server normally listens for connections on port 22." It adds: "This port number has been registered with the IANA, and has been officially assigned for SSH."
IANA's registry confirms it. Port 22 is listed with the service name ssh, described as "The Secure Shell (SSH) Protocol" and referencing RFC 4251. IANA lists the same number for TCP, UDP and SCTP, but SSH as people use it runs over TCP. Port 22 sits in the System Ports range, 0 to 1023, which is the range IANA reserves for assignments that go through IETF Review or IESG approval.
That registration is the whole reason it is "the SSH port". The number is a convention agreed in advance so a client and server that have never met know where to meet. Nothing in the protocol forces a server to use it.
What actually runs on port 22
One program: the SSH daemon, sshd. The sshd manual documents its -p option as "the port on which the server listens for connections (default 22)". Everything that rides on SSH reaches the server through that same port:
- Interactive logins.
ssh user@hostopens a remote shell. - File transfer.
scpandsftpuse the SSH connection, so they also go to port 22 unless told otherwise. - Git over SSH. A remote such as
[email protected]:owner/repo.gitconnects to port 22 by default. - Port forwarding. The tunnel travels through the SSH connection, so the firewall only sees port 22. See SSH port forwarding and tunnels.
Because all of these share one port, opening port 22 to a machine opens all of them. A firewall rule cannot tell an sftp upload from a shell login, because both are encrypted inside the same connection.
How to check that port 22 is reachable
The quickest test is SSH itself, in verbose mode. The ssh manual describes -v as printing "debugging messages about its progress", and repeating it raises the detail up to a maximum of three:
ssh -v [email protected]
In the output, look for the line that says it is connecting to the host and port. If the connection opens, you will see the server's version banner and the key exchange begin. If it does not, the error tells you which of two things happened:
| What you see | What it usually means |
|---|---|
Connection refused | The machine answered, but nothing listens on that port, or a firewall rejected the connection on purpose. Check that sshd is running and which port it uses. |
Connection timed out | No answer at all. A firewall is probably dropping the packets, the host is down, or the address is wrong. |
Permission denied | Port 22 is fine. You reached sshd and it rejected your credentials. |
Port 22 is reachable in the third case, so the problem has moved on to your key or password. For testing any port with tools other than SSH, see how to check if a port is open on Windows, Linux and macOS.
How to connect when the server uses a different port
Administrators sometimes move sshd to another port. The ssh manual describes -p as the "port to connect to on the remote host", and says it can also be set per host in the configuration file:
ssh -p 2222 [email protected]
To avoid typing it each time, add a block to ~/.ssh/config:
Host myserver
HostName server.example.com
User alex
Port 2222
Then ssh myserver connects to port 2222. The file format is covered in the SSH config file guide. For scp, the port flag is a capital -P, which trips people up; check scp's manual before relying on it.
How to change the SSH port
The setting lives in sshd_config. The sshd_config manual documents the Port option with the line "The default is 22", and notes that multiple options of this type are permitted, so a server can listen on more than one port at once. On Linux and macOS the file is normally /etc/ssh/sshd_config. On Windows, Microsoft's documentation says OpenSSH Server reads %programdata%\ssh\sshd_config by default.
- Keep your current session open. If the new setting is wrong, an existing connection lets you undo it.
- Edit the file. Add or change the line, for example
Port 2222. While testing, you can keep both with two lines:Port 22andPort 2222. - Test the configuration. The sshd manual describes
sshd -tas test mode, which will "only check the validity of the configuration file and sanity of the keys". It prints nothing if the file is valid. - Allow the new port in the firewall before you restart the service, whichever firewall the machine uses.
- Restart sshd. Microsoft's documentation states that sshd reads its configuration when the service starts, so changes need a restart. The restart command depends on the system (
systemctlon most Linux distributions,Restart-Service sshdin PowerShell on Windows). - Test from a second terminal with
ssh -p 2222 [email protected]before you close the first one.
sudo sshd -t
Two details in the sshd manual are worth knowing. A port given on the command line with -p makes sshd ignore Port lines in the file. And ports set through ListenAddress override command-line ports. If sshd seems to ignore your edit, check for a command-line flag in the service definition, and for ListenAddress lines that include a port.
Ports below 1024 are privileged on Unix-like systems, which is why sshd starts as root and why a regular user cannot run their own sshd on port 22. A test sshd on a high port such as 2222 does not need that.
Should you move SSH off port 22?
Moving the port changes where attackers and scanners have to look. It does not change what protects you. The protections are the ones SSH already has: key-based login, disabled passwords, and limits on who may log in.
The sshd_config manual lists two defaults worth checking on any server you expose. PasswordAuthentication defaults to yes, so password logins are on unless you turn them off. PermitRootLogin defaults to prohibit-password, which means root can log in with a key but not a password. Microsoft's documentation notes that PermitRootLogin does not apply on Windows; there, DenyGroups and AllowUsers style directives control who can sign in.
A sensible baseline on a public server is keys only (see generating an Ed25519 SSH key), PasswordAuthentication no, and an allow list of the accounts that need access. Whether you also move the port is a trade-off. A non-standard port means every client, script and firewall rule that uses SSH must know about it, and some restricted networks only allow traffic to well-known ports. If you move it, treat it as a convenience, not as a security control.
Port 22 in the wider list of ports
Port 22 is one of many well-known assignments, next to 80 for HTTP and 443 for HTTPS. For the full picture of how port numbers are split into ranges, see what is a port number, and for a list of the common ones, see common port numbers.
If port 22 still does not work
Work from the outside in. First confirm the host name or address resolves and the machine is up. Then check whether sshd is running on the server and which port it reports in its configuration. Next, look at the firewall on the server and any network firewall in between, since a timeout almost always means packets are being dropped before they reach sshd. Last, rerun ssh -v and read where it stops. A failure before the version banner is a network problem; a failure after it is an authentication or host key problem.
Frequently asked questions
- Is port 22 TCP or UDP?
- SSH uses TCP. RFC 4253 says that over TCP/IP the server normally listens on port 22. IANA also lists port 22 for UDP and SCTP under the name ssh, but ordinary SSH connections from OpenSSH and similar clients are TCP.
- Is it safe to leave port 22 open to the internet?
- It is a common setup, but it needs strong authentication. A server reachable on port 22 can be tried by anyone, so use key-based login and turn password login off. The sshd_config manual says PasswordAuthentication defaults to yes, so you have to disable it yourself.
- Can I run SSH on a port other than 22?
- Yes. Set Port in sshd_config, or start sshd with -p. Clients then connect with ssh -p PORT user@host, or a Port line in their SSH config file. Nothing in the protocol requires 22.
- Why does ssh say 'Connection refused' on port 22?
- The TCP connection reached the machine, but nothing was listening on that port, or a firewall actively rejected it. Check that sshd is running and that it listens on the port you used. A timeout instead of a refusal usually points to a firewall that silently drops packets.