How To Change SSH Port in Ubuntu

By default, SSH server runs on port 22 in Ubuntu. Sometimes, you may need to run SSH on a different port, for different reasons. In this article, we will look at how to change SSH port in Ubuntu.

How To Change SSH Port in Ubuntu

Here are the steps to change SSH port in Ubuntu.

1. Verify current SSH port

Open terminal and run the following command to check SSH port number in your Linux.

# grep -i port /etc/ssh/sshd_config
#Port 22

If you see the above output, it means SSH is running on port 22 in Linux.

Also read : How to Set up automatic updates in Ubuntu

2. Open sshd_config

SSH config details are stored in /etc/ssh/sshd_config. Open it in a text editor

# sudo vi /etc/ssh/sshd_config

Also read : How to Set up static IP in Ubuntu

3. Change SSH port

Uncomment the following line

#Port 22

and change 22 to a port number of your choice (e.g 5000)

Port 5000

Please make sure that you have opened port 5000 in your firewall/iptables.

Also read : How to Enable Key Based Authentication in SSH

4. Restart SSH server

Run the following command to restart SSH server.

# sudo systemctl restart sshd

Also read : How to Run Cron Job Every 5, 10, 15 minutes

5. Verify SSH port change

Run the netstat command to ensure that SSH is running on new port 5000.

# sudo netstat -tulpn | grep ssh
tcp        0      0    *               LISTEN      957/sshd
 tcp6       0      0 :::5000                   :::*                    LISTEN      957/sshd

From now on, you will need to specify new SSH port number while connecting using SSH command. Here’s an example,

ssh -p 5000