Secure Shell (SSH) is a network protocol that provides a secure way to access remote servers. It is widely used by system administrators to manage servers and perform various tasks. However, the default SSH port (22) is often targeted by hackers, who attempt to gain unauthorized access to servers. Changing the SSH port is a simple and effective way to enhance the security of your server. In this article, we will guide you through the process of changing the SSH port on a Linux server.
Prerequisites
Before we begin, you will need:
- A Linux server with SSH access
- Root or sudo access to the server
- A text editor (such as nano or vim)
Step 1: Connect to your server
To begin, connect to your Linux server using SSH. Open your terminal and type:
ssh username@server_ip_address
Replace username
with your username and server_ip_address
with the IP address of your server.
Step 2: Open the SSH configuration file
Once you are connected to your server, open the SSH configuration file using your preferred text editor. The location of the SSH configuration file may vary depending on the Linux distribution you are using. In most cases, the file is located at /etc/ssh/sshd_config
.
sudo nano /etc/ssh/sshd_config
Step 3: Change the SSH port
Look for the line that specifies the SSH port:
#Port 22
Uncomment the line by removing the #
symbol and change the port number to a number between 1024 and 65535. For example:
Port 2222
Save the file and exit the editor.
Step 4: Restart the SSH service
To apply the changes, restart the SSH service:
sudo systemctl restart sshd
Step 5: Test the new SSH port
Now that you have changed the SSH port, you will need to specify the new port when connecting to your server via SSH. For example:
ssh username@server_ip_address -p 2222
Replace 2222
with the new SSH port number you specified in the configuration file.
If you are able to connect to your server using the new SSH port, congratulations! You have successfully changed the SSH port on your Linux server.
Additional Tips
Here are some additional tips to help you secure your server:
Use a strong password or SSH key
Changing the SSH port is just one part of securing your server. It is also important to use a strong password or SSH key to prevent unauthorized access to your server.
Disable root login
By default, the root user can log in to a Linux server via SSH. This is not recommended, as it makes your server vulnerable to brute-force attacks. Instead, create a new user with sudo privileges and disable root login.
Use a firewall
A firewall can help protect your server from unauthorized access. Configure your firewall to only allow traffic on the new SSH port you have specified.
Conclusion
Changing the SSH port is a simple and effective way to enhance the security of your Linux server. By following the steps outlined in this article, you can easily change the SSH port and improve the security of your server. Remember to also use a strong password or SSH key, disable root login, and use a firewall for additional security.