Claudiu Persoiu

Blog-ul lui Claudiu Persoiu


ssh key based authentication

without comments

The ssh key based authentication is useful when you don’t want to type the password each time. Also is useful when for instance sshfs is used and mounting takes place without entering a password.

The end result should do login with the following command:

$ ssh work

Server

In Ubuntu the server installation is done with:

$ sudo apt-get install ssh

For other distributions the packege is usually called ssh or OpenSSH.

On server-side you must activate the public key authentication in the sshd_config file:

#/etc/ssh/sshd_config

PubkeyAuthentication yes

Client

On client-side the public/private key pair must be generate:

$ ssh-keygen -t rsa -b 1024

Where the type of the key is rsa and 1024 is the size. Bigger is safer.

The public key must be copied on the server using the following command:

$ ssh-copy-id -i ~/.ssh/id_rsa user@192.168.1.1

The -i option and key path are optional, if there was generated a single key on the client in ~/.ssh directory that one will be used as default.

Right now it should work to login using the next command without prompting for password:

$ ssh user@192.168.1.1

Alias

The last step is creating an alias, in this case it will be called “work”.

You must make a config file in the ~/.ssh directory. The file must have 600 rights, that is reading and writing only for owner.

$ cd ~/.ssh
$ touch config
$ chmod 600 ~/.ssh/config

In this file aliases will be set:

Host work
HostName 192.168.1.1
User user
IdentityFile ~/.ssh/id_rsa

At this step authentication to the previously defined host can be made with the command:

$ ssh work

To learn more about ssh aliases checkout the manual:

$ man ssh_config

Written by Claudiu Persoiu

5 April 2011 at 8:35 PM

Posted in Diverse,Linux-Unix

Tagged with

Leave a Reply