How To Securely Configure SSH On Linux Servers
SSH best practices to keep yourself protected
Secure Shell, commonly referred to as SSH, is a cryptographic protocol used to secure communication between two hosts over a network. SSH is useful because it allows for secure communication, even when the network may be insecure.
Here are some best practices for securing your SSH connection and preventing unauthorized users on your network.
Use a strong username/password combination
Having an easy-to-guess password is one of the quickest ways to let someone gain access to your server (or any device, for that matter). For example, “123456” or “password” are strongly advised against because they are too easy to guess. An attacker can attempt to brute force your device by trying many of these ill-advised, common passwords — a strong password will stop this from succeeding.
Disable empty passwords
Another security measure that should be taken is to disable an empty or blank password. On your server, open the /etc/ssh/sshd_config file in your text editor of choice and add the line PermitEmptyPasswords no. If it exists already, simply change it from yes to no. This disables blank passwords, which are just as bad as weak ones.
Disable root login
The root user has admin privileges on the server, which is something we never want an attacker to get a hold of. Instead of accessing your server with root, you should create a new user and give it sudo access. You can add a user to the sudoer group with usermod -aG sudo newuser, where “newuser” is the username of the account.
Then, you will want to disable the actual root account from being accessed. In the same /etc/ssh/sshd_config file that we disabled the blank passwords in, add the line PermitRootLogin no. Again, if it exists already, just change it from yes to no.
Change the default port
When an attacker wants to try to gain access to a server via SSH, they will often scan all devices on the internet for the open port 22, which is the default SSH port. One trick to prevent your device from showing up here is to change the default port that SSH operates on.
In our sshd_config file, add Port XXXX where “XXXX” is the port value that you want to use. This will work as long as the port number you select isn't already being used by another service. Once you change the port number, you will have to restart your SSH service in order for the changes to take effect. Do this with service sshd restart.
While this doesn't completely prevent an attacker from discovering your device, it makes it more difficult to be found.
Use public/private keys
One last security measure you can take to prevent an attacker from gaining access to your server is by using a public/private key pair for authentication. This method is an alternative to logging in with a username/password combination.
First, on your client machine, you will want to run ssh-keygen -t rsa. Follow the steps to select the directory the keys will be saved in, and if you want to add a password for the private key. Once the process is complete, it will have generated an id_rsa (private key), and id_rsa.pub (public key).
Next, you want to copy the SSH public key to the server, which can be done with ssh-copy-id. Alternatively, you can use scp to copy the public key as well. Place the id_rsa.pub in the /.ssh/authorized_keys directory and set its permissions with chmod 600 /.ssh/authorized_keys.
Finally, we can disable server login via password, so that only logins with public/private key authentication are allowed. We can do this in the same sshd_config file by adding/changing the line PasswordAuthentication no. Now, when you login, you won’t be asked for your password because the authentication is coming from the public/private key pair.
You can follow the steps here as well if you would like more information on setting up the SSH keys.
Takeaways
SSH can be a very useful protocol to help with communication between devices. However, if configured insecurely and improperly, SSH can be the perfect entryway for a malicious actor to gain access to your network. If you follow the steps outlined above, you’ll no longer have to worry about a hacker taking over your personal data. This guide is meant to be simple and straightforward — I hope these tips help you secure your servers and eliminate your fear of malicious attackers!
