Jump to content

  •  

Photo
- - - - -

How to: Set up SSH Keys to log in on Ubuntu/Debian from Windows

Linux Tutorials Debian Debian 7 Debian 6 Ubuntu Ubuntu 12.04 Ubuntu 14.04

  • Please log in to reply
No replies to this topic

#1 Wilson18

Wilson18

    Administrator

  • root
  • 112 posts
  • LocationUniversity of Birmingham

Posted 22 July 2014 - 10:00 PM

In this guide, I will be showing you how you can set up SSH Keys to in to Ubuntu/Debian. 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.

apt-get update; apt-get upgrade

Now that we have done that, we need to create our keys for the root user. 

ssh-keygen

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:

prettypicture.jpg

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. 

id_rsa  id_rsa.pub

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. 
 

sudo apt-get install nano; sudo nano /etc/ssh/sshd_config

The line we need to look out for is 
 

#AuthorizedKeysFile      %h/.ssh/authorized_keys

You will need to remove the # symbol. You can then save and exit the file. 
 
Finally, we need to open up our private key and make a copy of it.

nano id_rsa

When his is open, it should look something like this

key.jpg

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

sudo service ssh restart

Now that we have done this, we are pretty much done Ubuntu/debian 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. 

notepad.jpg

Next, you need to open PuTTYgen. You will need to go to Conversions and click Import Key. 

importKey.JPG

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. 

savePassKey.JPG

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. 
 

PuTTY.JPG

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.

login.jpg


Edited by Wilson18, 05 June 2015 - 08:30 PM.
Added sudo for permissions requiring it

List what you read?

Check me out on: YouTube, Twitter, Facebook, Google+






Also tagged with one or more of these keywords: Linux, Tutorials, Debian, Debian 7, Debian 6, Ubuntu, Ubuntu 12.04, Ubuntu 14.04