In this guide, I will be showing you how you can set up SSH Keys to in to CentOS. To do this, you will need to install Putty on your Windows machine you would like to log in from. You can download it here. Once you have done this, we will need to log in to our server by ssh.
Once logged in, we just need to quickly update our server.
Now that we have done that, we need to create our keys for the root user.
Once you do this, you will be asked to choose a filename
Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa):
You should leave this blank and just press the enter key.
You will now be asked to enter a password. You can use a password if you like or leave this blank to log in to the server without a password.
Once you have done this, it should look like this:
Next, we need to change to the location where we store the keys we have just generated and change the permissions on them.
cd ~/.ssh; ls; chmod 700 ~/.ssh; chmod 600 ~/.ssh/*
You should see the following been outputted.
The id_rsa file is our private key (which we will be using) and the id_rsa.pub is our public key.
What we need to do now is to authorise the key so that we are allowed to use it.
cat id_rsa.pub >> authorized_keys
One last thing we need to do is to allow us to use the ssh keys in the configuration file. To do this, we need to just make sure our server is up to date and then install nano to open our config file.
yum install nano -y; nano /etc/ssh/sshd_config
The line we need to look out for is (You can search in nano by typing Ctrl + W and then the word you would like to search for)
You will need to delete this line and use this instead.
You can then save and exit the file.
Finally, we need to open up our private key and make a copy of it.
When his is open, it should look something like this
To copy this, you will need to highlight everything and then right click. This should also paste it. Once you have this copied, you should then exit the file WITHOUT saving it.
We can finally restart our ssh server
service sshd restart
After this, we are just going to make sure the authorised keys file has the correct permissions.
cd ~/.ssh; chmod 700 ~/.ssh; chmod 600 ~/.ssh/*
Now that we have done this, we are pretty much done CentOS now and we can swap back to windows. In Windows, you will need to open Notepad.exe and paste the key in to here and save it. It doesnt really matter what you save this as at the moment.
Next, you need to open PuTTYgen. You will need to go to Conversions and click Import Key.
You then select the key we created in notepad. If you chose a password earlier, you will need to enter this where it says key passphrase and then confirm it below.
Click the save private key button. You can save the file as whatever you would like.
Now we have done this, we will need to go into PuTTY. In PuTTY, you will need to enter your username@IP address and port in the port box. You can then go into the Connection settings and in to the SSH section and click Auth. In auth, you can select your private key using the Browse button.
Once you have done all of this, we can go back to session and save the session so we can connect at a later date without all of this. Enter the session name and save it. If you then click open, it should connect you without having to enter your username or password. If you had a passphrase, you will need to enter that instead.