1. Connect to Your Server
Use your current SSH connection to access your server. If you’re not connected, you might need physical or console access to the server.
2. Edit the SSH Configuration File
Open the SSH configuration file in a text editor. The default location for the SSH configuration file is usually /etc/ssh/sshd_config
.
bashCopy code
3. Change the Port Number
sudo nano /etc/ssh/sshd_config
Find the line that specifies the port (it’s usually commented out with a #
at the beginning). Change the port number to your desired value. Choose a port number that is not already in use and is not reserved for other services.
bashCopy code
# Port 22 Port 2222 # Change this to your desired port number
4. Save and Close the File
Save the changes and close the text editor.
5. Restart the SSH Service
To apply the changes, restart the SSH service:
bashCopy code
sudo service ssh restart
6. Adjust Firewall Settings
If you’re using a firewall, update its settings to allow traffic on the new SSH port. If you’re using UFW, you can do this with the following commands:
bashCopy code
sudo ufw allow 2222 # Replace with your new port number sudo ufw reload
7. Test the New SSH Port
Open a new terminal window and try to SSH into your server using the new port:
bashCopy code
ssh -p 2222 username@your_server_ip
Replace 2222
with the port number you’ve chosen, username
with your actual username, and your_server_ip
with the actual IP address or domain name of your server.
Important Notes:
- Always choose a secure port that is not commonly used for other services.
- After changing the SSH port, make sure you remember the new port number. Otherwise, you might lock yourself out of your server.
- Updating the SSH port alone is just one security measure. Consider other security practices like disabling root login, using key-based authentication, and keeping your system and SSH software up-to-date.
After making these changes, your SSH server should be running on the new port. Ensure that you can still connect before closing your current SSH session. If you encounter any issues, you may need to troubleshoot and double-check your configuration.