To change the port for SSH service in Ubuntu. We just have to change the port number in our SSH configuration files.
- The SSH configuration files is located at /etc/ssh/sshd_config and /etc/ssh/ssh_config
- To open the file in edit mode, execute the following command. But make sure you have sudo privileges or you are executing command as a root user.
- In the upper part of the file, you will see the current port number.Here, we need to change the current port number and set the desired port number.the updated line should look like this.
- Once done, press CTRL+X followed by Y followed by Enter to save the file.
- Also, make the same changes on /etc/ssh/ssh_config
- Now, execute the following command to restart the SSH service. It is required to update configuration and make SSH listen on the new port.
- Note that if you are using any kind of firewall, allow incoming connections on your desired SSH port before disconnecting with the server.
sudo nano /etc/ssh/sshd_config
Port (new port number)
sudo service ssh restart