I have been setting up a number of Centos servers over the last few months, and something that I’ve needed to do over and over again is secure SSH. It is actually quite simple to do, and by following the steps below you’ll really make sure that your server is a lot more secure than the default configuration.
The first step is to disable root login. The root username is predictable, and provides complete access and control over your system. To lock it down, edit the /etc/ssh/sshd_config file, and set PermitRootLogin as follows:
PermitRootLogin no
The next thing that I like to do is change the port number that SSH listens on. To do this, edit the /etc/ssh/sshd_config file as follows:
Port 21000
The port number that you choose is of course up to you, I have just used 21000 as an example here.
The last, and most complex step in securing SSH is to generate keys, and then disable password authentication. Most people, myself included unfortunately, use a Windows desktop, so the steps below are for this type of setup.
- If you haven’t already done so, create a user on the server that you will be using in the future.
- Log in as that user. I have used a user called admin.
- Run ssh-keygen on the server. The output will look as follows:
- It has now created two files, id_rsa, the private key, and id_rsa.pub, the public key. The public key needs to be added to theauthorized_keys file. If that file doesn’t already exist in ~/.ssh then create it with touch authorized_keys. Then execute cat id_rsa.pub » authorized_keys.
- The last, and very important step on the server is to change the permissions on that file with chmod 600 authorized_keys
- Now you need to copy the private key to your Windows desktop to create the Putty key. SCP is the best way to do this, so you can run pscp –P 21000 sysadmin@augusta:/home/admin/.ssh/id_rsa C:\
- Open PUTTYGEN.EXE
- Select File->Load Private Key
- Browse for id_rsa and select it
- When it prompts you, enter the passphrase.
- You can change the comment on the key. A good idea is to use your email address.
- Now press save private key, and save it to your hard drive.
augusta ~/.ssh: ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/admin/.ssh/id_rsa.
Your public key has been saved in /home/admin/.ssh/id_rsa.pub.
The key fingerprint is:
90:92:2e:7b:14:86:66:qa:b9:6f:a4:9f:0e:g2:06:11 admin@augusta
That’s it, now in your Putty connection, be sure to use the port number you chose earlier, and in the SSH->auth section select the key you just created. Under the Connection->Data section I also like to enter the Auto-login username which saves a bit of time when connecting.
The last step on the server is to disable password authentication, ONCE YOU HAVE TESTED THAT YOUR KEY’S WORK. Again edit /etc/ssh/sshd_config as follows:
PasswordAuthentication no
Now restart SSH with sudo /etc/init.d/sshd restart and you are all done!