The SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → port is the network port your server’s SSH service listens on for logins. It is 22 unless someone has changed it. This guide is for anyone running a Linux server or VPSVPS A private slice of a powerful server, with its own guaranteed share of memory and processing and full control, at a fraction of the price of a whole machine. More about VPS → who wants to find out which port SSH uses, connect on a different one, or move SSH to a new port without locking themselves out.
SSH uses TCP portPort A numbered door on a server. Each service listens behind its own door: web pages on 443, email on others, remote login on 22. More about Port → 22 by default. To connect on another port, run **ssh -p 2222 user@example.com** or add a Port line to your SSH config file. To change the server’s port, open the new port in the firewallFirewall A gatekeeper that decides which connections are allowed into a server and blocks the rest. More about Firewall → first, add Port 2222 to the SSH server configuration, check it with **sudo sshd -t, restart SSH (on Ubuntu 24.04 that means sudo systemctl daemon-reload and sudo systemctl restart ssh.socket**), then test a new login before you close your current session.
What the SSH port is
A server runs many programs that accept network connections: a web serverWeb server The program that answers visitors’ browsers and sends them your website. Also used for the computer it runs on. More about Web server →, a mail server, a database. An IP address gets a connection to the right machine, and a port number gets it to the right program on that machine. Port 22 was set aside for SSH when the protocol was created, so every SSH client tries 22 unless you tell it otherwise.
On the server, the program that answers is sshd, the OpenSSH daemon (a background service). It reads its settings from /etc/ssh/sshd_config and any files in /etc/ssh/sshd_config.d/. The Port setting there decides where it listens. If no Port line is set, the default of 22 applies.
A few related port facts are worth knowing:
| Tool or feature | Port it uses | Notes |
|---|---|---|
ssh logins | 22 | The same port the server listens on |
| SFTP file transfers | 22 | SFTP runs inside SSH, so it needs no separate port |
scp and rsync over SSH | 22 | Also carried by the SSH connection |
| FTP | 21 | A different, older protocol, not related to SSH |
| SSH tunnels | Any you choose | The tunnel still travels over the SSH port |
Because SFTPSFTP A secure way to copy files between your computer and a server. Like dragging files into a folder, but the folder is on the server. More about SFTP →, scp and rsync ride on the SSH connection, changing the SSH port changes it for all of them too.
How to find which port SSH is using
From the server
If you can already log in, ask sshd which settings it is really using. The -T option prints the final configuration after every file has been read:
sudo sshd -T | grep -i '^port'
A standard server prints port 22. If you see more than one line, SSH is listening on more than one port.
To confirm what is actually listening right now, list the open TCP ports:
sudo ss -tlnp | grep -E ':22 |:2222 '
Replace 2222 with whatever port you expect. On Ubuntu 24.04 the process name may show as systemd rather than sshd, because systemd holds the port open and starts SSH when a connection arrives. That is normal.
From your own computer
If you cannot log in and do not know the port, the hosting control panelControl panel A website you log into to manage your hosting with buttons and forms, instead of typing commands. cPanel and Plesk are the common ones. More about Control panel → or the welcome email for the server usually states it. You can also test a guess from outside with nc -zv example.com 2222, as shown in the port checker guide. Scanning ports on a server you do not own or manage is not appropriate, so only do this on your own machines.
How to connect to SSH on a different port
When a server listens somewhere other than 22, each tool needs to be told. The option letters differ, which catches many people out:
ssh -p 2222 exampleco@203.0.113.10
sftp -P 2222 exampleco@203.0.113.10
scp -P 2222 backup.tar.gz exampleco@203.0.113.10:/home/exampleco/
rsync -av -e "ssh -p 2222" site/ exampleco@203.0.113.10:/var/www/site/
ssh uses a lower case -p. scp and sftp use a capital -P, because their lower case -p means “preserve file times”. rsync passes the option through to ssh with -e.
Save the port in your SSH config file
Typing the port every time gets old quickly. Put it in ~/.ssh/config on your computer (on Windows, C:\Users\yourname\.ssh\config):
Host exampleserver
HostName 203.0.113.10
User exampleco
Port 2222
Now ssh exampleserver, sftp exampleserver and scp file exampleserver: all use port 2222 automatically. Code editors and Git tools that read this file pick it up too.
Your computer stores the server’s host key separately for each port, so a server on 2222 appears in ~/.ssh/known_hosts as [203.0.113.10]:2222. The first time you connect on the new port you may see the host key prompt again. Check the fingerprint as described in the SSH host keys guide before you accept it.
Should you change the SSH port?
Moving SSH off port 22 is a common step, but be clear about what it does and does not do.
What it does: automated bots scan the whole internet for port 22 and try common user names and passwords all day. On a new port, most of that traffic never reaches sshd, so your logs are far quieter and easier to read.
What it does not do: it does not make SSH harder to break into for anyone who looks. A full port scan finds the new port in minutes. Real protection comes from SSH keys instead of passwords, turning off password logins, keeping the server updated and limiting who can reach the port with a firewall.
Changing the port is a reasonable extra, not a replacement for those. If your team connects from networks that block unusual outgoing ports, such as some office or hotel networks, staying on 22 may cause fewer support calls.
Choosing a new port
- Pick a number between 1024 and 65535 that nothing else on the server uses. 2222 is a common example; any unused number works.
- Avoid ports other software expects, such as 3306 (MySQL and MariaDB), 5432 (PostgreSQL) or 8080 (often used by web apps).
- Write the number down somewhere your team can find it, along with the server’s details.
How to change the SSH port safely
The golden rule: keep your current SSH session open the whole time. An existing session is not dropped when you restart SSH, so if something goes wrong you can still fix it. The steps below use 2222 as the new port.
- Open the new port in the firewall.
- Add the new port to the SSH configuration, keeping 22 for now.
- Check the configuration and restart SSH.
- Test a fresh login on the new port from a second terminal.
- Remove port 22 and close it in the firewall.
Step 1: open the new port in the firewall
Do this first, so the new port is reachable the moment SSH starts listening on it. On Ubuntu and Debian with UFW:
sudo ufw allow 2222/tcp
On AlmaLinux and Rocky Linux with firewalld:
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
If your host or cloud provider runs its own firewall in front of the server (often called a security group or network firewall in the provider’s dashboard), allow TCP 2222 there as well. The Linux firewall guide covers both tools in more depth.
Step 2: add the port to the SSH configuration
Rather than editing the main file, create a small file of your own in the drop-in folder. Package updates leave it alone, and it is easy to find later:
sudo nano /etc/ssh/sshd_config.d/10-port.conf
Add both ports for now:
Port 22
Port 2222
Port is one of the few settings that can appear more than once, and each line adds another listening port. Listening on both during the change means you can still get in on 22 if the new port turns out to be blocked somewhere.
As soon as any Port line exists, the built in default of 22 no longer applies. If you put only Port 2222 in the file, SSH stops listening on 22 at the next restart.
On AlmaLinux and Rocky Linux, SELinux (a security layer that controls which ports each service may use) also has to be told that sshd may use 2222, or SSH will fail to start on it:
sudo dnf install policycoreutils-python-utils
sudo semanage port -a -t ssh_port_t -p tcp 2222
You can check the result with sudo semanage port -l | grep ssh, which should list both 22 and 2222.
Step 3: check the configuration and restart SSH
Test the configuration before restarting. If this prints nothing, the files are valid:
sudo sshd -t
How you restart depends on the system.
On Ubuntu 22.10 and later, including 24.04, SSH is started by a systemd socket by default, and the port comes from sshd_config only after systemd reloads its settings. Restarting the service on its own leaves SSH on the old port. Run:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
You can confirm the socket is in use with systemctl is-active ssh.socket. If it says inactive, use the service command below instead.
On Ubuntu 22.04 and Debian 12:
sudo systemctl restart ssh
On AlmaLinux and Rocky Linux 9:
sudo systemctl restart sshd
Then confirm both ports are listening with sudo ss -tlnp | grep -E ':22 |:2222 '.
Step 4: test a new login
Leave your current session open. In a second terminal on your computer, connect on the new port:
ssh -p 2222 exampleco@203.0.113.10
If you get a prompt, the change works. If it hangs or is refused, use the open session and the table below to find out why.
Step 5: remove port 22
Once the new port works, edit /etc/ssh/sshd_config.d/10-port.conf again and delete the Port 22 line. Also check that the main /etc/ssh/sshd_config does not contain an uncommented Port 22 line, because that would keep 22 open. Run sudo sshd -t, restart SSH the same way as in step 3, and close the old port:
sudo ufw delete allow 22/tcp
On firewalld, SSH on 22 is normally allowed as a named service, so remove it with:
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --reload
If you use Fail2ban, its SSH jail watches port 22 by default. Set port = 2222 in the [sshd] section of /etc/fail2ban/jail.local and restart Fail2ban, or it will block attackers on a port nobody uses any more.
Common problems
| Symptom | Cause | Fix |
|---|---|---|
| SSH still only listens on 22 after restarting on Ubuntu 24.04 | Socket activation ignores the new port until systemd reloads | Run sudo systemctl daemon-reload then sudo systemctl restart ssh.socket |
| Connection times out on the new port | A firewall on the server or at the provider is dropping it | Allow TCP 2222 in UFW or firewalld and in the provider’s firewall |
| Connection refused on the new port | sshd is not listening there | Check sudo sshd -T shows the port and restart SSH |
| SSH fails to start on AlmaLinux or Rocky Linux | SELinux does not allow sshd on that port | Add it with semanage port -a -t ssh_port_t -p tcp 2222 |
scp -p 2222 copies nothing or errors | scp uses capital -P for the port | Use scp -P 2222 |
| Host key prompt appears again | Keys are stored per host and port | Check the fingerprint, then accept it once |
| Locked out after closing every session | Port 22 removed before the new port was tested | Use your host’s web console or rescue mode to fix the config |
Official documentation
- sshd_config manual (OpenSSH): the
PortandListenAddresssettings for the server. - ssh_config manual (OpenSSH): the
PortandHostsettings for your client config file.
Related
- How to connect to a server with SSH: the basics of logging in before you change anything.
- How to copy SSH keys to a server, clipboard or new PC: set up key logins so you can turn passwords off.
- What is Ubuntu Server?: the other first steps for securing a new server.
Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.



