Guide
What port does SSH use? Port 22, and how to change it
SSH uses TCP port 22 by default, and so does SFTP, because it runs inside SSH. To connect to a server that listens on another port, add -p: ssh -p 2222 user@server. To change the port a server listens on, set Port in its sshd_config, allow the new port in the firewall, then restart SSH.
Here's each of those in detail for Linux and Windows servers, how to check the port is open, and whether changing it is worth it.
- Port 22 by default
- ssh -p, scp -P, sftp -P
- Linux and Windows servers
Updated · OpenSSH on Linux, macOS and Windows 10 and 11
Which port does SSH use?
Port 22 is the port registered for SSH, and an SSH server listens there unless someone changes it. SSH only uses TCP, so there's nothing to open for UDP. Everything that runs over SSH uses the same port:
| Protocol | Default port | Notes |
|---|---|---|
| SSH | 22 (TCP) | Terminal sign-in, commands and tunnels. |
| SFTP | 22 (TCP) | Runs inside SSH: same port, same accounts and keys. |
| scp and rsync over SSH | 22 (TCP) | Also ride on an SSH connection. |
| FTP | 21 (TCP) | A different, unencrypted protocol, plus extra ports for data. |
| FTPS | 21 or 990 (TCP) | FTP with TLS: explicit on 21, implicit on 990. |
| Telnet | 23 (TCP) | An old, unencrypted remote terminal that SSH replaced. Avoid it. |
So if you can SSH to a server, SFTP to it works on the same port with the same sign-in. The SFTP guide covers the commands.
Connect to SSH on another port
Add the port to the command. Note the capital -P for scp and sftp:
ssh -p 2222 admin@server.example.com ssh ssh://admin@server.example.com:2222 scp -P 2222 report.pdf admin@server.example.com:/tmp/ sftp -P 2222 admin@server.example.com rsync -av -e "ssh -p 2222" photos/ admin@server.example.com:backup/photos/
ssh admin@server.example.com:2222 doesn't work: ssh reads the whole thing as a host name. These commands are the same in a Linux or Mac terminal and in PowerShell on Windows 10 and 11. To stop typing the port, save it once in a Host entry of your SSH config file; after that, plain ssh web uses the right port.
Step-by-step: change the SSH port on Linux
Step 1: Pick a free port
Choose a number above 1024 that nothing else uses, such as 2222.
sudo ss -tlnlists the ports already taken.Step 2: Open it in the firewall first
With ufw (Ubuntu):
sudo ufw allow 2222/tcp. With firewalld (Fedora, RHEL, Rocky Linux, AlmaLinux):sudo firewall-cmd --permanent --add-port=2222/tcp sudo firewall-cmd --reload
On a cloud server, also allow the port in the provider's firewall or security group.
Step 3: Allow it in SELinux (Fedora and RHEL family only)
SELinux only lets the SSH server use ports labeled for it. Add the label with
sudo semanage port -a -t ssh_port_t -p tcp 2222(semanage is in the policycoreutils-python-utils package). Ubuntu and Debian skip this step.Step 4: Set the new port
Open the settings with
sudo nano /etc/ssh/sshd_config, find#Port 22and change it toPort 2222. While you test, you can keep a second line,Port 22: the server then listens on both. Save, and check the file withsudo sshd -t(no output means it's fine).Step 5: Restart SSH
Which command depends on the system:
# Ubuntu 22.10 and later, including 24.04 (SSH listens through ssh.socket) sudo systemctl daemon-reload sudo systemctl restart ssh.socket # Ubuntu 22.04 and Debian sudo systemctl restart ssh # Fedora, RHEL, Rocky Linux, AlmaLinux and Arch sudo systemctl restart sshd
sudo ss -tlnshould now show the new port. If Ubuntu 24.04 still shows only 22, run thedaemon-reloadand therestart ssh.socketagain.Step 6: Test, then close the old port
Keep your current session open and connect from a second terminal with
ssh -p 2222 user@server. When that works, remove thePort 22line if you kept it, restart again, and delete the old firewall rule (sudo ufw delete allow OpenSSH, orsudo ufw delete allow 22/tcp, whichever you added).
Don't lock yourself out
Change the SSH port on Windows (OpenSSH Server)
In PowerShell as administrator, open the server's settings, set the port, allow it in Windows Firewall and restart the service:
notepad C:\ProgramData\ssh\sshd_config # change #Port 22 to Port 2222, then save New-NetFirewallRule -Name sshd-2222 -DisplayName "OpenSSH Server (port 2222)" -Direction Inbound -Protocol TCP -LocalPort 2222 -Action Allow Restart-Service sshd Get-NetTCPConnection -LocalPort 2222 -State Listen
The last line shows the server listening on the new port. If OpenSSH Server isn't installed yet, see how to turn on OpenSSH Server in Windows 11.
Check that the SSH port is open
- On the server:
sudo ss -tlnon Linux, orGet-NetTCPConnection -State Listenon Windows, lists the ports in use. Look for 22 or your own port. - From a Linux or Mac computer:
nc -zv server.example.com 22says whether the port answers. - From Windows:
Test-NetConnection server.example.com -Port 22, then look for TcpTestSucceeded : True. - From the internet, for a machine at home: after forwarding port 22 on your router, the open port checker tests whether port 22 is reachable on the connection you're browsing from.
If it fails, connection refused means nothing listens on that port (or a firewall rejects it), and connection timed out usually means a firewall drops it or the address is wrong. ssh -v shows where the connection stops. If the port answers but ssh gives up with Unable to negotiate, the port is fine: see how to fix no matching host key type found.
Should you change the default SSH port?
Moving SSH off port 22 cuts down the automated login attempts that fill a server's logs, because many scanners only try 22. It isn't protection on its own, though: anyone who scans all ports still finds it. What actually keeps a server safe:
- Key sign-in, with password sign-in turned off: see passwordless SSH with keys.
- No direct root sign-in, and only the accounts that need SSH.
- Updates, so the SSH server gets its security fixes.
- Not exposing SSH to the internet at all when you don't need to. RemoteAccessApp can reach an SSH server on your home or office network through one of your own PCs there, with no port forwarded on the router.
Set the port in RemoteAccessApp
In the RemoteAccessApp SSH client, every saved host has its own Port box, filled with 22 for SSH and SFTP. Change it when you add or edit the host. Pasting admin@server.example.com:2222, or a whole ssh -p 2222 admin@server.example.com command, into Address fills in the user name and port for you. To check a host answers, right-click it and choose More > Test connection.
Hosts reached through one of your PCs can use any port too. The Console is free for Windows and Linux with up to 5 saved hosts: download RemoteAccessApp.
Questions, answered
Related
- Enable SSH on UbuntuInstall and start the SSH server on Ubuntu or Debian, let it through the firewall, check it's listening and connect. Plus OpenSSH Server on Windows 11.Read the guide
- Passwordless SSH with keysMake an SSH key, copy it to the server with ssh-copy-id (or one PowerShell line on Windows), get the permissions right and sign in without a password.Read the guide
- SSH config file examplesWhere ~/.ssh/config lives on each system, how ssh reads it, and copy-ready examples for aliases, ports, keys, jump hosts and keep-alives.Read the guide
- What is an SSH tunnel?Local, remote and dynamic SSH port forwarding with ready-to-use commands, when to use each, agent forwarding, and how to keep tunnels safe.Read the guide
- SSH & SFTP clientTerminal tabs, an SFTP and FTP file manager, key sign-in and one command to every server.Learn more
- No port forwardingHow to reach a PC behind a router without opening ports, and why it's safer.Read the guide
