Difference between revisions of "Ssh keys"

From ALICE Documentation

(Setup Linux ssh for key based login)
(Setup Linux ssh for key based login)
Line 42: Line 42:
   username@remote-host's password:
   username@remote-host's password:
Type in in yoyr password to actually start the file copy.
Type in in your password to actually start the file copy.
   Number of key(s) added: 1
   Number of key(s) added: 1

Revision as of 18:27, 8 July 2019

It is possible to setup ssh using keys only.

Setup Linux ssh for key based login

We need to create a private/public key set to allow passwordless login via ssh. To do this run the sshkey-ge command:

 $ ssh-keygen -t rsa
 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/testuser1/.ssh/id_rsa): 
 Enter passphrase (empty for no passphrase): 
 Enter same passphrase again: 
 Your identification has been saved in /home/testuser1/.ssh/id_rsa.
 Your public key has been saved in /home/testuser1/.ssh/
 The key fingerprint is:
 The key's randomart image is:
 +---[RSA 2048]----+
 | .o=+o.. . ..    |
 | ++.oo. . .  .   |
 | .o.+ .+..  .    |
 |  o. oo.=o o     |
 | o o=.o=S ...    |
 |  +..=.+. . o    |
 |   .+ .  . E     |
 |.+ +. .          |
 |+o* .o           |

For both question about passphrase, just hit enter (we will not be using passphrases). This will also have generated two files in your personal .ssh directory:

 $ ls -ltr id_rsa*
 -rw------- 1 testuser1 users 1843 Jul  8 19:50 id_rsa
 -rw-r--r-- 1 testuser1 users  415 Jul  8 19:50

The file we need to transfer to the remote host. For this we can use the ssh-copy-id tool:

 $ ssh-copy-id -i ~/.ssh/ username@remote-host

This may produce the following message:

 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/username/.ssh/"
 The authenticity of host 'remote-host (' can't be established.
 ECDSA key fingerprint is SHA256:tygMarTe3SOjTcY9HzldKThxQzsTeiYHg5JmjB2bxeg.
 Are you sure you want to continue connecting (yes/no)? yes

Having confirmed the access key to remote-host, the copy operation will commence:

 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
 /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
 username@remote-host's password:

Type in in your password to actually start the file copy.

 Number of key(s) added: 1
 Now try logging into the machine, with:   "ssh 'username@remote-host'"
 and check to make sure that only the key(s) you wanted were added.

The passwordless ssh login is now in place.

Alternative public key copy

It may be the case that ssh-copy-id is not available with your version of OpenSSH. In that case you need to copy the information yourself. This can be done following the below pocedure.

It is possible to copy the public key directly to the remote host. The command below pipes the content of the public key through the ssh login to the remote host. On the remote host we first create the .ssh directory (if not there) and than append the public key content to the athorized_keys file, all in one command:

 cat ~/.ssh/ | ssh username@remote-host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys"

After successful execution of above command you can login to remote-host without specifying a password.

Setup PuTTY to use key based login

Before we can use key based login we first need to create a private/public key set. This is done by the program puttygen. Open this program:


and hit the 'Generate'key. You will have to move your mouse around as this will help randomness in the creation of the key pair. Once the keys have been created you need to save each key in its own file.


Make sure to use sensible file names for the two key files. The private key is automatically appended with the .ppk extension, while the public key does not need an extension.


Now we must make PuTTY aware of the private key. Go to SSH - AUTH and use the Browse button to select the file in whuch you have previously saved the private key (usually extenstion .ppk).


Finnaly we need to tell PuTTY to use a particular account name for accessing the public key (which we still need to upload). So go to SSH - DATA and fill in your ULCN account name in the 'Auto-login username' box.


With all these changes make sure to save the setting: go to Session and click the Save button.

We still need to copy the public key to the server. To do so user Wordpad to open your public key file and select the text part. In this case from 'AAA' to '=='. User CTRL-C to copy.


Open the login session to the server (you still have to provide your password), then go to the .ssh directory and edit the authorized_keys file using an editor (e.g. vi) and paste in the copied text from the public file. Make sure the pasted text is one line! Perpend that line with ssh-rsa (the default key type from puttygen) and save the file.


Once the authorized_key file is saved, you can login without the need to type in you password.

This procedure can be used for the second login profile (the one you use to log in onto the ALICE login node).