logo
. . .

How to Change SSH Port in Linux

Changing your default port is one of the best ways to protect your SSH server. In this blog, we will explain how to Change SSH Port in Linux and walk you through changing it on your virtual private server (VPS).

Step 1
:-
Login to Your Server.
Connect to your Linux VPS server using your current SSH port. Open a terminal and use the following command, replacing ‘username’ and ‘your-server-ip’ with your actual username and server IP address.
# [ ssh username@your-server-ip ]

Step 2:- Edit the SSH Configuration File.
Once logged in, edit the SSH daemon configuration file. The location of this file can vary, but it is commonly found at ‘/etc/ssh/sshd_config’. You can use a text editor like ‘nano’ or ‘vi.’
# [ sudo nano /etc/ssh/sshd_config ]

Step 3:- Locate the Port Setting.
Look for the line in the configuration file that specifies the port. It will look something like this.
# [ #Port 22 ]
Uncomment the line (remove the # at the beginning) and change the port number. For example:
# [ Port 2222 ]

Step 4:-Save and Close the File.
Save your changes and exit the text editor.

Step 5:- Restart the SSH Service.
To apply the changes, restart the SSH service. The command can vary depending on your Linux distribution. Common commands include:
For systems that use ‘systemd’ (e.g., Ubuntu 16.04 and later, CentOS 7 and later):
# [ sudo systemctl restart ssh ]
For systems using ‘service’ (older systems):
# [ sudo service ssh restart ]

Step 6:- Verify the New Port.
Before closing your current SSH session, open a new terminal window and attempt to reconnect using the new port.
# [ ssh -p 2222 username@your-server-ip ]
Replace ‘2222’ with the new port number.

Step 7:- Update Firewall Rules.
Start by making sure the newly selected port is not blocked. If this is a new VPS setup, all ports should be open by default. For example, using ‘ufw’ on Ubuntu.
# [ sudo ufw allow 2222 ]
# [ sudo ufw reload ]

Step 8:- Disconnect and Reconnect.
If everything is working correctly, you can now disconnect from the old SSH session and reconnect using the new port.

Note:- Remember that changing the default SSH port alone is not a foolproof security measure. Ensure that you have strong authentication methods in place, such as key-based authentication, and consider implementing additional security measures to protect your server.