LinuxTutorialsUbuntu

How To Change SSH Port in Ubuntu

Change SSH Port in Ubuntu

In this tutorial, we will show you how to change SSH port on Ubuntu. For those of you who didn’t know, SSH is a program and protocol for securely connecting to remote machines across a network. It allows you to run programs, and do a variety of tasks as if you were sitting at the machine. SSH is very similar to telnet except it is with encryption to protect the transferred information and authentication. The Secure Shell (SSH) Protocol by default uses port 22. Accepting this value does not make your system insecure, nor will changing the port provide a significant variance in security. However, changing the default SSH port will stop many automated attacks and a bit harder to guess which port SSH is accessible from

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the change of the default SSH port on Ubuntu Linux. You can follow the same instructions for Ubuntu 20.04, 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu or Debian-based.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • Before changing the SSH port, it’s always a good idea to take a backup of the SSH configuration file in case something goes wrong.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, you can harm your system if you’re not careful when acting as the root.

Change SSH Port in Ubuntu

Step 1. Log into your server as the root user.

ssh root@hostname/IP

Step 2. Open the SSHD Configuration File.

The SSH server configuration file is located at /etc/ssh/sshd_config. Open the file using your preferred text editor. For example, to open the file using the nano editor, run the following command:

nano /etc/ssh/sshd_config

Edit the line which states ‘Port 22’. Choose an appropriate port, and also make sure it is not currently used on the system. Here I am using 959:

# What ports, IPs and protocols we listen for
Port 959

Once you have the change done, simply exit and save the sshd_conf file. Now all you need to run is the below command and it will restart the SSH server. The next time you want to connect via SSH, you will need to do so on your new port, in our case, 959:

sudo systemctl restart sshd

SSH will listen on port 959. You can check this by executing the following command in the terminal:

netstat -tunlp |grep ssh

Verify SSH is listening on the new port by connecting to it. Note how the port number now needs to be declared:

ssh -p 959 username@hostname.com

Step 3. Configure Firewall.

If you are using UFW, the default firewall configuration tool for Ubuntu, run the following command to open the new SSH port:

sudo ufw allow 959/tcp

Congratulations! You have successfully changed the default SSH port. Thanks for using this tutorial to change the OpenSSH port number on Ubuntu 20.04 systems. For additional help or useful information, we recommend you check the official Ubuntu website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Save

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button