Skip to content

SSH and SecurityGuide

What Port Does SSH Use and How to Change It Safely

  • Updated
  • 16 min read
  • VPS Lane Engineering
Short answer

SSH uses TCP port 22 by default. To use another, change Port on the server and connect with ssh -p PORT user@host.

How we tested this

Run for real on Ubuntu 24.04.5 LTS with OpenSSH 9.6p1 (package 1:9.6p1-3ubuntu13.20), iproute2 6.1.0, OpenBSD netcat 1.226, nmap 7.94SVN and rsync, as root, against a second sshd started from a copy of sshd_config on ports 20022 and 22122 over IPv4 loopback. Not run, and written from the documentation under Sources, are the systemctl, ufw, firewalld, SELinux and Windows commands, because the test machine had no systemd as init and no IPv6. Ubuntu 22.04, 26.04, Debian and Red Hat systems were not tested.

SSH Port Numbers at a Glance

The SSH port numbers and settings below are the ones you need before you check or change anything.

SSH port quick reference for OpenSSH
QuestionAnswer
Default port22 on TCP
Where the server sets itThe Port line in /etc/ssh/sshd_config or a file in /etc/ssh/sshd_config.d/
What can hold the listener on Ubuntu 22.10 and laterThe systemd unit ssh.socket, which needs its own reload step
Show the configured portsudo sshd -T filtered for port
Show the live listenersudo ss -tlnp filtered by port
Connect to another portssh -p 22122 user@host, and -P for scp and sftp

The server port is the TCP port where sshd accepts incoming connections. The -p option on the client only tells the client which port to call, so it never changes the server. To move the port you change the server configuration and apply it.

Before you start you need root or sudo rights on the server and, for a remote machine, a second way in such as the provider console.

What this guide covers and how far it was tested
SystemStatus
Ubuntu 24.04 LTSsshd commands and port checks run for real, systemd steps from Ubuntu documentation
Ubuntu 22.10 to 23.10From the Ubuntu announcement, not tested
Ubuntu 22.04 and earlierClassic service method, not tested
Ubuntu 26.04Not tested, so check the socket state as shown below and the release documentation
Debian, including Debian 13Outside the procedures in this guide when ssh.socket is active, classic method otherwise, not tested
Red Hat familyShort notes from the Red Hat documentation, not tested

The addresses 203.0.113.10 for the server and 203.0.113.25 for your own connection are documentation examples. Replace them with your real addresses. The new port used throughout, 22122, is also only an example.

What the Default SSH Port 22 Is

The default SSH port is 22, the number the SSH transport standard names and IANA lists under the service name ssh.

RFC 4253 says an SSH server normally listens on port 22 when it runs over TCP. The IANA registry also carries UDP and SCTP rows for the same number, but the OpenSSH sshd_config manual only describes TCP listeners, and the checks below found TCP sockets alone. For an OpenSSH server you open TCP, not UDP.

Linux knows the name too. This is the exact match from /etc/services on Ubuntu 24.04, the local list that tools use to print ssh in place of 22.

Output
$ grep -E '^ssh[[:space:]]' /etc/services
ssh		22/tcp				# SSH Remote Login Protocol

The number 22 is a default, not a requirement. The Port option in sshd_config accepts another value, and the manual allows more than one Port line, which is what makes a safe change possible.

How to Check the SSH Port on a Server

Two checks show the SSH port on a server, one for what the config says and one for what is listening.

Read the Configured Port

The configured port is easiest to read from sshd -T, which prints the effective settings with included files applied.

bash
sudo sshd -T | grep -E '^(port|addressfamily|listenaddress) '

Run against the untouched system config of a fresh Ubuntu 24.04 install, and then against the test copy with -f, it printed this.

Output
$ sshd -T | grep -E '^(port|addressfamily|listenaddress) '
port 22
addressfamily any
listenaddress [::]:22
listenaddress 0.0.0.0:22

$ sshd -T -f /root/w/ssh-port/sshd_config | grep -E '^(port|addressfamily|listenaddress) '
port 20022
addressfamily any
listenaddress [::]:20022
listenaddress 0.0.0.0:20022

This beats reading sshd_config by eye, because the stock file has #Port 22 commented out and a file under /etc/ssh/sshd_config.d/ can set the value instead. It has a limit. sshd -T reports what the files say. It does not prove that sshd or systemd is listening on that port, that the socket unit was rebuilt or that a firewall lets connections in.

Read the Listening Port

The listening port comes from the kernel, so ss with a port filter shows what is open right now.

bash
sudo ss -tlnp 'sport = :22'

On the test instance the same filter was pointed at its own port.

Output
$ ss -tlnp 'sport = :20022'
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:20022      0.0.0.0:*    users:(("sshd",pid=19297,fd=4))

The filter matches that one port only, where a loose grep 22 would also match 2222, 5222 or a process ID. A header with no rows means nothing is listening there. The test machine had no IPv6, so only the IPv4 line appears. On a server with IPv6 expect a second line for [::].

On a server that uses socket activation the process column can name systemd in place of sshd, because systemd holds the port until a connection arrives. The same filter with -u in place of -t lists UDP sockets, and ss -H -uln 'sport = :20022' returned nothing in the test.

Find an SSH Port From Another Computer

To find an SSH port from another computer, without a login on the server, scan the candidate ports with nmap.

bash
nmap -Pn -p 22,22122 203.0.113.10

Only scan servers you own or have permission to test. The test run against loopback, after the move to 22122, looked like this, trimmed to the port table.

Output
$ nmap -Pn -p 20022,22122 127.0.0.1
PORT      STATE  SERVICE
20022/tcp closed unknown
22122/tcp open   unknown

open means some TCP service answered, not that it is SSH. In this mode nmap only looks the number up in its service list, hence unknown. A firewall can also hide a running service, which shows as filtered. Add -sV, shown in the security section, to identify the service.

Which Change Method Your Server Needs

Which change method your server needs depends on whether sshd or the systemd socket owns the listening port.

Ubuntu announced that from 22.10 the OpenSSH server uses systemd socket activation by default. There systemd opens the port and starts sshd on the first connection, so editing sshd_config and restarting the service is not enough. Check the server itself instead of trusting the release number, because the default can be switched off and other distributions differ.

bash
systemctl is-active ssh.socket
systemctl status ssh.socket ssh.service
systemctl cat ssh.socket

These were not run for this guide. is-active answering active means the socket unit is running, whether or not it is enabled at boot. It does not prove that it is the only SSH listener, because ssh.service can be active and listening too. Use the status output and the ss check above to see whether systemd or sshd holds each port, and if both do, sort that out before you pick a method. inactive or a message that the unit does not exist points to the classic method. systemctl is-enabled describes boot behaviour only, so it does not answer this question.

How the SSH port is changed on each kind of server
ServerWhere the port is setWhat applies it
ssh.socket not active, the norm on Ubuntu 22.04 and earlierPort in sshd_configsudo systemctl restart ssh
Ubuntu 24.04 with ssh.socket activePort in sshd_configsudo systemctl daemon-reload then sudo systemctl restart ssh.socket
Ubuntu 22.10 to 23.10 with ssh.socket activeListenStream in a drop-in for ssh.socketsudo systemctl daemon-reload then sudo systemctl restart ssh.socket
Red Hat family with SELinux enforcingPort in sshd_configAn SELinux port label, then sudo systemctl restart sshd
Anything else with ssh.socket active, such as Debian 13 or a newer Ubuntu releaseRead systemctl cat ssh.socket firstFollow that release documentation, not covered here

The firewall step, the validation step and the testing steps are the same for every row. Only the apply command differs. On the last row, also make sure ssh.service and ssh.socket are not both holding a port, by running the ss filter for the old and the new port after the change and checking that only the listener you intended is left.

Open the New SSH Port in the Firewall First

Open the new SSH port in the firewall first, so the port is already allowed when the listener moves.

Pick a Port That Is Free

Pick a port that is free on the server and, ideally, not assigned to another service in the IANA registry.

bash
sudo ss -H -tln 'sport = :22122'
grep -E '[[:space:]]22122/' /etc/services
cat /proc/sys/net/ipv4/ip_local_port_range
cat /proc/sys/net/ipv4/ip_local_reserved_ports
Output
$ ss -H -tln 'sport = :22122'
$ grep -E '[[:space:]]22122/' /etc/services
$ cat /proc/sys/net/ipv4/ip_local_port_range
32768	60999
$ cat /proc/sys/net/ipv4/ip_local_reserved_ports

On the test machine the first two commands printed nothing, meaning no TCP listener and no local service name for 22122. The third printed the range Linux uses for outgoing connections and the fourth an empty reserved list. Those values belong to that machine and yours can differ. A port inside the outgoing range can already be in use when sshd starts, so avoid that range unless you have a reason.

IANA splits ports into system ports from 0 to 1023, user ports from 1024 to 49151 and dynamic ports from 49152 to 65535. Prefer an unused user port. /etc/services is only a short local list and does not show whether a number is assigned, so look your choice up in the IANA registry and check what else runs on the server.

Allow the Port in ufw

Allow the port in ufw with a TCP rule for the new number, and leave the rule for 22 in place for now.

bash
sudo ufw status verbose
sudo ufw allow 22122/tcp

A status of inactive means ufw is not filtering anything. Do not enable it in the middle of this change unless both SSH ports are allowed first. According to the ufw manual, a rule without an address applies to IPv4 and IPv6 when IPv6 is enabled in /etc/default/ufw.

If you connect from a fixed address, a narrower rule lets only that address reach the port. Use it instead of the open rule, not as well as it.

bash
sudo ufw allow proto tcp from 203.0.113.25 to any port 22122

Run sudo ufw status verbose again and confirm the new rule is listed. That proves the rule exists in ufw. It does not prove that a firewall outside the server lets the port through, so add the same TCP port to any cloud firewall, security group or router forward in front of the machine.

Red Hat Family Systems

Red Hat family systems need two extra steps per the Red Hat documentation, an SELinux label for the port and a firewalld rule.

bash
sudo dnf install -y policycoreutils-python-utils
sudo semanage port -l | grep -w ssh_port_t
# Run only if 22122 is not already listed under ssh_port_t
sudo semanage port -a -t ssh_port_t -p tcp 22122
sudo firewall-cmd --add-port=22122/tcp
sudo firewall-cmd --list-all
sudo firewall-cmd --runtime-to-permanent

None of these were run for this guide. The first line installs the package that supplies semanage on RHEL 9, which a minimal install can lack. The second lists the ports SELinux already allows for SSH. The -a form adds a port label and fails if the port is already assigned to any SELinux port type. If the port is already listed under ssh_port_t, no change is needed. If it belongs to another type, find out why before changing it, and use semanage port -m only once you have confirmed that reassigning it is appropriate. Without the label, SELinux in enforcing mode stops sshd from binding the new port even when the config file is correct.

The last firewalld command, taken from the Red Hat guide, saves the whole running rule set and not just this port. Review the --list-all output before you run it, and add --zone= with the right zone name if the network interface is not in the default zone.

Change the SSH Port in sshd_config

The safe way to change the SSH port in sshd_config is to add the new port beside the old one and validate before applying.

Back Up the Config File

Back up the config file first, so a bad edit can be undone with one copy command.

bash
sudo cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

If your port is set somewhere else, back up that file instead. That can be a file in /etc/ssh/sshd_config.d/ or, on Ubuntu 22.10 to 23.10, the socket drop-in /etc/systemd/system/ssh.socket.d/addresses.conf.

Add the New Port Beside the Old One

Add the new port beside the old one by writing two Port lines, which makes sshd listen on both.

bash
sudo nano /etc/ssh/sshd_config
text
Port 22
Port 22122

The stock line is #Port 22. Remove the # and add the second line under it. Both lines are needed, because once any Port line is set the default of 22 no longer applies by itself. Then search for every explicit Port line, ignoring case since sshd keywords are not case sensitive.

bash
sudo grep -RniE '^[[:space:]]*Port[[:space:]]' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
Output
$ grep -RniE '^[[:space:]]*Port[[:space:]]' /root/w/ssh-port/d/sshd_config /root/w/ssh-port/d/sshd_config.d/
/root/w/ssh-port/d/sshd_config:22:Port 20022
/root/w/ssh-port/d/sshd_config:23:Port 22122

In the test copy 20022 plays the part of 22. The search lists explicit lines only, so use sshd -T as shown earlier for the effective result.

Validate Before You Apply

Validate before you apply, because sshd will not start from a file it cannot parse.

bash
sudo sshd -t

No output and exit status 0 mean the file parses. In the test the copy was checked with -f, first with a deliberate typo of 221222 and then with the typo fixed.

Output
$ sshd -t -f /root/w/ssh-port/sshd_config
/root/w/ssh-port/sshd_config line 23: Badly formatted port number.

That run ended with exit status 255. With the typo fixed the same command printed nothing and ended with status 0.

A pass has limits. In the same test sshd -t also passed a copy that pointed at port 3389, which another program already held, and that daemon then logged Bind to port 3389 on 0.0.0.0 failed: Address already in use. and did not start. The check reads the file and does not try the port. Two programs cannot normally listen on the same address and TCP port, which is why the free port check matters.

Apply It on a Server Without the Socket Unit

On a server without the socket unit active, a restart of the SSH service applies the new ports.

bash
sudo systemctl restart ssh

The unit is named ssh on Ubuntu and Debian. On Red Hat family systems run sudo systemctl restart sshd instead. Neither restart was run for this guide.

What was run is a reload, which is a different operation. The test daemon was sent a hangup signal, the same signal the packaged Ubuntu unit sends for systemctl reload ssh, and it opened the new port without being stopped. A session that was already open stayed connected. A full restart stops and starts the daemon, so do not assume the same result, and keep the console within reach.

Change the Port With the Ubuntu systemd Socket

To change the port with the Ubuntu systemd socket active, the socket unit has to be rebuilt after you edit the file.

Ubuntu 24.04

Ubuntu 24.04 reads the port from sshd_config through a systemd generator, as the comment in its own stock config file explains.

Output
$ grep -n -B8 '^#Port 22' /etc/ssh/sshd_config
15-# configuration must be re-generated after changing Port, AddressFamily, or
16-# ListenAddress.
17-#
18-# For changes to take effect, run:
19-#
20-#   systemctl daemon-reload
21-#   systemctl restart ssh.socket
22-#
23:#Port 22

So the edit is the same two Port lines as above, followed by sudo sshd -t and then these two commands.

bash
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Skipping daemon-reload is a common reason a server stays on 22 after an edit. An Ubuntu bug report describes exactly that, and the comment above was added to the file as the fix. To roll back, restore the sshd_config backup and run the same two commands, since the socket settings are generated from that file.

To see what the generator produces, the binary from the 24.04 package was run by hand. It only reads /etc/ssh/sshd_config, so a copy holding Port 22 and Port 22122 was bind mounted over that path inside a private mount namespace with unshare -m, which leaves the real file untouched. The three arguments are the output folders systemd normally passes, all pointed at one test folder.

Output
$ unshare -m sh -c "mount --bind /root/w/ssh-port/gencfg /etc/ssh/sshd_config && /usr/lib/systemd/system-generators/sshd-socket-generator /root/w/ssh-port/gen /root/w/ssh-port/gen /root/w/ssh-port/gen"
$ cat /root/w/ssh-port/gen/ssh.socket.d/addresses.conf
# Automatically generated by sshd-socket-generator

[Socket]
ListenStream=
ListenStream=0.0.0.0:22122
ListenStream=[::]:22122
ListenStream=0.0.0.0:22
ListenStream=[::]:22

The empty ListenStream= line clears the port 22 entry built in to the stock socket unit, and the lines after it add each configured port for IPv4 and IPv6. With an unchanged config the generator wrote nothing. This run shows the file format only and is not a test of the restart.

Ubuntu 22.10 to 23.10

Ubuntu 22.10 to 23.10 use socket activation by default but have no generator, so check where the port is set before editing.

bash
systemctl cat ssh.socket

According to the Ubuntu announcement, upgrades to those releases moved an existing Port or single ListenAddress setting into /etc/systemd/system/ssh.socket.d/addresses.conf in most cases. If the output shows a custom port in a drop-in, change its ListenStream lines there, in the same format as the generated file above. A fresh install has no such file, and sudo systemctl edit ssh.socket creates one. Then run the same two systemctl commands. To roll back, restore your copy of the drop-in and run them again.

Go Back to a Plain sshd Service

To go back to a plain sshd service that binds its own port, Ubuntu documents the commands below for 22.10 and later.

Two of them delete drop-in files, the override that ties the service to the socket and the address settings. Copy both folders first if they exist, so custom settings in them can be restored. The -d test skips a folder that is not there.

bash
[ -d /etc/systemd/system/ssh.service.d ] && sudo cp -a /etc/systemd/system/ssh.service.d /root/ssh.service.d.bak
[ -d /etc/systemd/system/ssh.socket.d ] && sudo cp -a /etc/systemd/system/ssh.socket.d /root/ssh.socket.d.bak
bash
sudo systemctl disable --now ssh.socket
sudo rm -f /etc/systemd/system/ssh.service.d/00-socket.conf
sudo rm -f /etc/systemd/system/ssh.socket.d/addresses.conf
sudo systemctl daemon-reload
sudo systemctl enable --now ssh.service

After that the classic method applies. Run this from the console, not over SSH, since it stops the unit that accepts new connections before the service takes over.

Test the New SSH Port Before Closing the Old Session

Test the new SSH port before closing the old session, using a second terminal and leaving the first one connected.

Check Both Listeners on the Server

Check both listeners on the server first, since a missing line here means the apply step did not work.

bash
sudo ss -tlnp '( sport = :22 or sport = :22122 )'
Output
$ ss -tlnp '( sport = :20022 or sport = :22122 )'
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:20022      0.0.0.0:*    users:(("sshd",pid=19297,fd=5))
LISTEN 0      128          0.0.0.0:22122      0.0.0.0:*    users:(("sshd",pid=19297,fd=4))

On the test instance both ports were open under the same daemon after the reload.

Test the Port From Your Own Computer

Test the port from your own computer next, because a check run on the server skips every firewall in between.

bash
nc -zv -w 3 203.0.113.10 22122

On Windows, Test-NetConnection 203.0.113.10 -Port 22122 in PowerShell does the same job, and TcpTestSucceeded : True means the TCP connection opened. Neither outside test could be run for this guide. The loopback version below shows the two answers netcat gives.

Before the port was added, the loopback test was refused.

Before the port was added
$ nc -zv -w 3 127.0.0.1 22122
nc: connect to 127.0.0.1 port 22122 (tcp) failed: Connection refused

After the port was added and the daemon reloaded, the same test succeeded.

After the port was added
$ nc -zv 127.0.0.1 22122
Connection to 127.0.0.1 22122 port [tcp/*] succeeded!

Connection refused means the packet arrived and nothing was listening. succeeded means a TCP connection opened. A check that hangs until it times out usually points at a firewall dropping packets or a wrong address. A loopback pass like this one says nothing about firewalls, which is why your own test has to come from outside.

Sign In on the New Port

Sign in on the new port as the final proof, because an open port only shows that something answered.

bash
ssh -p 22122 user@203.0.113.10
Output
$ ssh -p 22122 -i /root/w/ssh-port/client_ed25519 root@127.0.0.1 'echo connected on port ${SSH_CONNECTION##* }'
Warning: Permanently added '[127.0.0.1]:22122' (ED25519) to the list of known hosts.
connected on port 22122

That is the test sign in with a key, trimmed of four client options that only kept the test away from the real known hosts file. The warning matters on a real server too. OpenSSH stores a host key per address and port, so expect a host key question the first time you use the new port. Compare the fingerprint with the one you already trust before you accept it.

Remove the Old Port Last

Remove the old port last, once a full sign in on the new port has worked from the network you normally use.

Delete the Port 22 line, run sudo sshd -t and apply the change with the same command you used before. Then run the listener check and the outside test again for both ports.

Output
$ ss -tlnp '( sport = :20022 or sport = :22122 )'
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22122      0.0.0.0:*    users:(("sshd",pid=19297,fd=4))

$ nc -zv -w 3 127.0.0.1 22122
Connection to 127.0.0.1 22122 port [tcp/*] succeeded!

$ nc -zv -w 3 127.0.0.1 20022
nc: connect to 127.0.0.1 port 20022 (tcp) failed: Connection refused

The new port accepts and the old one refuses, which is what a finished change looks like. In the test the session opened earlier on the old port was still alive after that port closed, so closing a listener did not cut a connection already made. Only now remove the firewall rule for 22.

bash
sudo ufw delete allow 22/tcp
sudo ufw status verbose

The delete command has to match the rule as it was added. If the rule was created as OpenSSH or ssh, list the rules with sudo ufw status numbered and delete by number. The ufw manual notes that a number removes one rule only, so the IPv6 twin needs its own delete.

Connect to SSH on a Custom Port

Each client tool needs the number to connect to SSH on a custom port, and the flag is not the same in all of them.

How OpenSSH client tools take a custom port
ToolCommand
sshssh -p 22122 user@203.0.113.10
scpscp -P 22122 file.txt user@203.0.113.10:/tmp/
sftpsftp -P 22122 user@203.0.113.10
rsyncrsync -a -e "ssh -p 22122" file.txt user@203.0.113.10:/tmp/

All four forms were run against the test instance on 127.0.0.1 with the OpenSSH 9.6 client tools and rsync from Ubuntu 24.04, and each one connected and exited with status 0. Note the capital -P for scp and sftp. Other clients, such as PuTTY, have a port field in their session settings.

To stop typing the number, save it in ~/.ssh/config on your own computer.

text
Host lane-test
    HostName 203.0.113.10
    Port 22122
    User root

After that ssh lane-test uses the saved port. The test loaded the same block, with 127.0.0.1 as the host name, from a separate file with -F and asked the client what it would use.

Output
$ ssh -F /root/w/ssh-port/ssh_config -G lane-test | grep -E '^(hostname|port|user) '
user root
hostname 127.0.0.1
port 22122

Remember scripts, backup jobs and monitoring that still point at 22.

Does Changing the SSH Port Improve Security

Changing the SSH port can cut unsolicited connection attempts, but it does not improve security against a scan that includes it.

Automated scanners that only check port 22 will not reach a moved port. That does not stop a targeted scan and does not make the SSH service itself any stronger. Red Hat describes the effect as security through obscurity. Treat it as optional noise reduction, not as authentication or access control.

A scan with version detection shows why. This is the test instance after its move to 22122.

Output
$ nmap -Pn -sV -p 20022,22122 127.0.0.1
PORT      STATE  SERVICE VERSION
20022/tcp closed unknown
22122/tcp open   ssh     OpenSSH 9.6p1 Ubuntu 3ubuntu13.20 (Ubuntu Linux; protocol 2.0)

With -sV nmap probes the open port and matches the reply, so it named the service and its version on a number that is not the default. The banner lines nmap prints around the table are left out here.

What protects an SSH server more than a port change
MeasureWhat it doesWhat it does not do
Key based sign in with passwords turned offRemoves password guessing as a way inProtect a private key that is stolen
Firewall rule limited to your addressesBlocks connections from other source addressesHelp when your own address changes
sudo ufw limit 22122/tcpDenies an address that opens 6 or more connections in 30 seconds, per the ufw manualStop slow or spread out attempts
Security updates appliedCloses known OpenSSH flawsCover flaws that are not yet fixed

If you want a server where you control all of this, a Linux VPS with full root access lets you set the port, the keys and the firewall yourself.

Fix SSH Port Problems and Roll Back

Most SSH port problems fall into one of the cases below, and a roll back from the console undoes the change.

Common results after an SSH port change
What you seeLikely causeFix
Still listening on 22 only after the editssh.socket is active and was not rebuiltRun sudo systemctl daemon-reload then sudo systemctl restart ssh.socket
Badly formatted port numberA typo or a value above 65535Fix the line and run sudo sshd -t again
Address already in use in the logAnother program holds the portFind it with sudo ss -tlnp and the port filter, then pick another number
Connection refused on the new portNothing is listening thereCheck sudo sshd -T and ss, then apply again
Timeout on the new portA firewall is dropping the packetsAdd the port in ufw and in the provider firewall
sshd cannot bind the new port on a Red Hat systemThe SELinux label is missingAdd the port with semanage as shown above
TCP connection succeeds but the SSH sign in failsAuthentication or access policy, not the portCheck the user name, the authorized keys, the sign in settings and the server log

The three messages in backticks were all produced in this test. On a systemd server read the daemon log with sudo journalctl -u ssh -n 30, using sshd as the unit name on Red Hat family systems.

To roll back, sign in through the console or the session you kept open, restore the backup, validate it and apply it with the command for your server.

bash
sudo cp -p /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
sudo sshd -t
sudo systemctl restart ssh

On Red Hat family systems the last line is sudo systemctl restart sshd. With ssh.socket active it is the daemon-reload and restart ssh.socket pair. If you edited a drop-in instead of sshd_config, restore that file. In the test, restoring the backup copy and reloading brought the listener back on the original port alone.

Output
$ ss -tlnp '( sport = :20022 or sport = :22122 )'
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:20022      0.0.0.0:*    users:(("sshd",pid=19297,fd=4))

Keep the firewall rule for 22 until you have finished, since a roll back needs it.

Questions people ask

What port does SSH use?

SSH uses TCP port 22 by default. A server can be set to another port with the Port option, so check the server if 22 does not answer.

Is SSH TCP or UDP?

OpenSSH listens on TCP. IANA also lists UDP and SCTP for port 22, but an OpenSSH server needs only the TCP port open in the firewall.

How do I find the SSH port on my server?

Run sudo sshd -T and read the port line, then confirm the live listener with sudo ss -tlnp. The first shows the config and the second shows what is open.

Why does Ubuntu 24.04 ignore the Port line in sshd_config?

It does not ignore it, but with socket activation systemd holds the port. Run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket after the edit.

How do I connect to SSH on a different port?

Add -p and the number, for example ssh -p 22122 user@203.0.113.10. For scp and sftp the flag is a capital -P.

Does changing the SSH port make a server secure?

No, it mainly reduces automated attempts aimed at port 22. Keys, a firewall limited to your addresses and updates do the real work.

Which port should I use instead of 22?

Pick an unused TCP port between 1024 and 49151 that no other software on the server needs. Staying below the Linux outgoing range avoids clashes.

Does changing the SSH port affect existing SSH connections?

It normally affects new connections, not sessions already open. In this test an open session survived the reload, but keep console access anyway.

Sources

  1. Ubuntu manual, sshd_config(5) for OpenSSH on Ubuntu 24.04
  2. RFC 4253, The Secure Shell (SSH) Transport Layer Protocol
  3. IANA Service Name and Transport Protocol Port Number Registry, port 22
  4. Ubuntu Discourse, SSHd now uses socket-based activation (Ubuntu 22.10 and later)
  5. Ubuntu bug 2069041, changing Port in sshd_config requires systemctl daemon-reload
  6. Ubuntu Server documentation, OpenSSH server
  7. Ubuntu manual, ufw(8)
  8. Nmap reference guide, service and version detection
  9. Red Hat Enterprise Linux 9, Using secure communications between two systems with OpenSSH

Written and tested by VPS Lane EngineeringWe run every step on real servers before publishing and update the guide when the software changes.