The SSH port is the network port used for establishing secure remote connections between computers. By default, SSH runs on port 22. However, you can secure your connection by changing the SSH port.
This write-up will present you the process of changing the SSH port on Linux-based systems.
How to Change the SSH Port on Linux?
The below simple commands will guide you to change the SSH port on your Linux system.
Step 1: Check SSH Status
First, check the current SSH status from the terminal using the command:
sudo systemctl status ssh
When the command executes, you will find the status of the SSH service. Ensure that SSH status is active (running).
Step 2: Connect Using the Default SSH Port
The SSH port is configured with the default port, which is Port 22. To connect to the remote server via SSH, utilize the below-mentioned syntax:
ssh -p 22 ubuntu@192.168.231.213
While establishing the SSH connection, you will be prompted with a few questions that you need to respond to.
Note: Replace the ubuntu@192.168.231.213 with your preferred connection (remote server).
Step 3: Change SSH Port
Now, you are on the remote server (i.e. ubuntu@192.168.231.213). To modify the SSH port according to your desired port number, run the following command:
sudo nano /etc/ssh/sshd_config
Navigate the line containing the #Port 22, uncomment and replace the port number such as:
Port 2024
Save the file using the keyboard shortcut key Ctrl+S and return to the terminal window with Ctrl+X.
Step 4: Logout From Default SSH Port
Type the “exit” in the terminal to close the remote connect:
exit
You will be notified after closing the connection.
Step 5: Connect via Modified SSH Port
After successfully changing the SSH port, let’s make a connection using the newly added SSH port. Here is the command:
ssh -p 2024 ubuntu@192.168.231.213
You can see in the above output that you have successfully connected with the new SSH port.
Note: Do not share the secure SSH port with unauthorized users.
Conclusion
In Linux-based distributions including Ubuntu 22.04, you can change the default SSH port (i.e. Port 22). To update or change the SSH port, first, connect to the remote server. Then, run the command “sudo nano /etc/ssh/sshd_config” to enter into the SSH configuration file. Change the SSH port number, save the file, and reconnect with the updated port number.