How to Change SSH Port in Ubuntu 18.04

This tutorial explains how to configure Ubuntu OpenSSH server to run on some specific port number rather than the default port number 22, in order to increase the overall security of your Ubuntu server.

The SSH server on Ubuntu listen on TCP port 22 by default. You can run the netstat command to check ssh port currently running on:

netstat -tulnp | grep ssh
run the netstat command to check ssh port currently running on

As you can see, the SSH daemon is currently running on TCP port 22.

In Ubuntu 18.04, the Port directive of the sshd_config config file specifies the port number that ssh server listens on. You can check the current configuration with following command:

grep -i port /etc/ssh/sshd_config

The Port directive is commented out by default, which means SSH daemon listens on  the default port 22.

The Port directive of the sshd_config config file specifies the port number that ssh server listens on

If you want to change the default SSH port in Ubuntu, perform the following steps with root privileges:

  1. Open the /etc/ssh/sshd_config file and locate the line:
    #Port 22
  2. Then, uncomment (Remove the leading # character) it and change the value with an appropriate port number (for example, 22000):
    Port 22000
  3. Restart the SSH server:
    systemctl restart sshd

After that, run the netstat command and make sure that the ssh daemon now listen on the new ssh port:

netstat -tulpn | grep ssh

When connecting to the server using the ssh command, you need to specify the port to connect using the -p flag:

ssh -p 22000 192.168.1.100

Note that if the Firewall is enabled, you need to add a rule to allow new SSH port.