How to Change SSH Port: Step-by-Step Guide
Securing your server is a top priority, and one way to do this is by changing the default SSH port. The default SSH port is 22, which is well-known and often targeted by attackers. By changing it to a custom port, you can significantly improve your server's security.
Changing the SSH port is a straightforward process, but it's important to do it correctly to avoid locking yourself out of your server. In this guide, we'll walk you through the steps to change your SSH port on both the server and client sides, using Ubuntu as an example.
Changing the SSH Port on the Server
The first step is to change the SSH port on the server. SSH uses the OpenSSH server, which is configured in the /etc/ssh/sshd_config file.
However, before we proceed, make sure your server is fully backed up to prevent any data loss in case something goes wrong.
Editing the SSH Configuration File
Open the SSH configuration file with a text editor. For instance, you can use nano:
sudo nano /etc/ssh/sshd_config
Upgrading the SSH Port
Look for the line that says "Port 22". Change this to a new port. For this guide, let's use 2222:
Port 2222
You can choose any port number you like, but avoid using a port that's already in use or reserved by the system. After changing the port, save the file and exit the editor.
Restarting the SSH Service
Changes to the SSH configuration take effect when the SSH server is restarted. You can do this with the following command:
sudo service ssh restart
Congratulations! You've just changed the SSH port on your server. However, remember that this won't take effect on the client side yet. Let's address that next.
Connecting to the New SSH Port on the Client
Now that your server is using a new SSH port, you need to configure your client to connect to this new port. We'll use the OpenSSH client as an example.
Specifying the New Port in the SSH Command
The simplest way to connect to your server's new SSH port is by specifying it in the SSH command. For instance:
ssh -p 2222 user@remote_server
Here, -p specifies the port, user is your server's username, and remote_server is your server's IP address or hostname.
Adding a New SSH Configuration Alias
If you connect to your server often, it might be more convenient to create an SSH configuration alias. Open your SSH configuration file with a text editor:
nano ~/.ssh/config
Then, add the following lines:
Host new_server HostName remote_server Port 2222 User user
Replace 'new_server' with a name of your choice, 'remote_server' with your server's IP address or hostname, and 'user' with your server's username. Now, you can connect to your server using the new port like this:
ssh new_server
So there you have it! You've successfully changed your SSH port and connected to it from a client. This is a great step towards enhancing your server's security. Make sure to test your connection and, if everything works well, update your firewall rules to allow traffic on your new SSH port.