Updated: 12 October 2026 · Applies to: OpenSSH 9.6p1 on Ubuntu 24.04, 10.2p1 on Ubuntu 26.04, 10.0p2 on Debian 13 and 9.9p1 on AlmaLinux 10 (same steps on Rocky Linux and RHEL 9 and 10), tested

The default SSH port is TCP port 22. IANA assigned port 22 to SSH, and the SSH standard (RFC 4253) says the server "normally listens for connections on port 22". To connect to a server on another port, use ssh -p PORT user@server. To change the port on the server, add a Port line in /etc/ssh/sshd_config.d/, allow the port in the firewall (and in SELinux on AlmaLinux, Rocky Linux and RHEL), restart SSH and test the new port before you close port 22.

Quick answers

  • What port does SSH use? TCP 22, for OpenSSH and every other SSH server, unless the administrator changed it.
  • Is SSH TCP or UDP? TCP. The IANA registry lists port 22 for SSH under TCP, UDP and SCTP, but OpenSSH listens on TCP only, so a firewall needs only TCP 22 (or your custom port) open.
  • Is port 23 SSH? No. Port 23 is Telnet, an old protocol that sends everything, passwords included, unencrypted. Do not use Telnet for server logins.
  • Do SFTP and scp use a different port? No, they run over SSH on the same port. See What Port Does SFTP Use?
  • Which port does my server use? Run sudo sshd -T | grep -E '^port' on the server. It prints one port line per port.

Connect to SSH on a specific port

ssh -p 2222 user@203.0.113.10                      # -p sets the port
ssh ssh://user@203.0.113.10:2222                   # the same as a URI
scp -P 2222 file.zip user@203.0.113.10:/home/user/ # scp and sftp use a capital -P
sftp -P 2222 user@203.0.113.10

To avoid typing the port every time, add the server to ~/.ssh/config on your computer and connect with ssh myserver:

Host myserver
    HostName 203.0.113.10
    Port 2222
    User user

ssh -G myserver | grep '^port' shows the port ssh will use (port 2222 in our test). For PuTTY, WinSCP, FileZilla and rsync, see How to Connect to SSH on a Different Port (ssh, scp, sftp, rsync, PuTTY).

Should you change the SSH port?

A different port cuts down the automated login attempts that fill your logs, but it is not real protection: port scanners find SSH on any port. What secures SSH is key-based login, password login turned off, root login restricted and a ban tool such as Fail2Ban. If you change the port, pick one that no other service uses, such as 2222, and follow the steps below so you do not lock yourself out.

Change the SSH port safely

Run the commands as root (or with sudo).

  1. Keep your current SSH session open during the whole change and test in a second terminal. In our test, an open session stayed connected while the port was changed and SSH restarted. Have a console ready in case: on a Ucartz VPS that is the VNC console (How to Access Your VPS via VNC in Virtualizor); on a dedicated server, the IPMI console.
  2. Check that the new port is free. This prints only a header line when nothing uses it:
    ss -ltn 'sport = :2222'
  3. Add the new port and keep port 22 for now. Debian 13, Ubuntu 24.04 and 26.04 and AlmaLinux 10 all read /etc/ssh/sshd_config.d/*.conf, and a separate file survives package updates:
    printf 'Port 22\nPort 2222\n' > /etc/ssh/sshd_config.d/10-port.conf
    If /etc/ssh/sshd_config has an uncommented Port line, comment it out: Port lines add up, so in our test Port 22 in the main file plus Port 2222 in the new file made SSH listen on both.
  4. Check the configuration. sshd -t prints nothing when it is valid, and sshd -T shows the result:
    # sshd -t
    # sshd -T | grep -E '^port'
    port 22
    port 2222
    A typo stops it with a clear message, for example /etc/ssh/sshd_config.d/10-port.conf: line 2: Bad configuration option: Prt. On Ubuntu, Missing privilege separation directory: /run/sshd means SSH has not run since boot (for example when you are on the console); run systemctl restart ssh once and check again.
  5. Open the new port in the firewall:
    ufw allow 2222/tcp                                                     # UFW (Ubuntu, Debian)
    firewall-cmd --permanent --add-port=2222/tcp && firewall-cmd --reload  # firewalld (AlmaLinux, Rocky, RHEL)
    With CSF on a cPanel server, add the port to TCP_IN in /etc/csf/csf.conf and run csf -r. More on UFW: How to Open Ports on Ubuntu with UFW (Allow SSH and Enable).
  6. On AlmaLinux, Rocky Linux and RHEL, allow the port in SELinux, otherwise sshd cannot use it. semanage is in the policycoreutils-python-utils package:
    # semanage port -a -t ssh_port_t -p tcp 2222
    # semanage port -l | grep -w ssh_port_t
    ssh_port_t                     tcp      2222, 22
    The stock sshd_config on these systems carries the same reminder in a comment.
  7. Apply the change. The command depends on the system:
    systemctl daemon-reload && systemctl restart ssh.socket   # Ubuntu 24.04 and 26.04
    systemctl restart ssh                                      # Debian 13
    systemctl restart sshd                                     # AlmaLinux, Rocky Linux, RHEL
    Ubuntu starts SSH through a systemd socket (ssh.socket). In our Ubuntu 24.04 test, systemctl restart ssh alone kept SSH on port 22 only; the socket restart added 2222.
  8. Check that both ports listen:
    # ss -ltnp | grep -E ':(22|2222) '
    LISTEN 0      4096       127.0.0.1:22        0.0.0.0:*    users:(("systemd",pid=1,fd=29))
    LISTEN 0      4096       127.0.0.1:2222      0.0.0.0:*    users:(("systemd",pid=1,fd=14))
    (Lab example from Ubuntu 24.04; our test server listened on 127.0.0.1 only, a normal server shows 0.0.0.0 and [::].) On Ubuntu the process is systemd, which is normal with socket activation; on other systems it is sshd.
  9. From a second terminal, log in on the new port: ssh -p 2222 user@SERVER-IP
  10. When that works, change 10-port.conf to only Port 2222, repeat steps 4 and 7, and close port 22 in the firewall: ufw delete allow 22/tcp (or the rule you used, such as ufw delete allow OpenSSH), or firewall-cmd --permanent --remove-service=ssh && firewall-cmd --reload (firewalld's ssh service is port 22/tcp).

After the last step our test server refused port 22 and accepted 2222:

$ ssh -p 22 alice@127.0.0.1
ssh: connect to host 127.0.0.1 port 22: Connection refused

Common problems

  • "Connection refused" on the new port: nothing listens on it. On Ubuntu, restart ssh.socket as in step 7; on AlmaLinux, Rocky Linux and RHEL, check the SELinux step. See the log with journalctl -u ssh (Ubuntu, Debian) or journalctl -u sshd. More causes: SSH Connection Refused on Port 22: Causes and How to Fix It.
  • "Connection timed out": a firewall drops the traffic, on the server or in front of it. See Connection Timed Out: How to Fix SSH and Server Timeouts.
  • Locked out: log in through the console (VNC on a VPS, IPMI on a dedicated server), delete /etc/ssh/sshd_config.d/10-port.conf and restart SSH as in step 7. Ucartz customers can also open a support ticket.

If our engineers look after your server, tell support the new port so their access keeps working.

Official documentation: sshd_config manual (OpenSSH), RFC 4253 and firewall-cmd manual.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket