SSH key authentication

Generate ssh key

$> ssh-keygen

Start ssh agent

$> ssh-agent /bin/bash

Add ssh key

$> ssh-add ~/.ssh/id_rsa

List all used ssh keys

$> ssh-add -l

Show public key

$> cat ~/.ssh/id_rsa.pub

Copy key to remote host via ssh

$> ssh-copy-id -i id_rsa.pub ssh-user@host.domain

If ssh-copy-id isn’t available on the respektive system, you can use the following snipped:

$> cat ~/.ssh/id_rsa.pub 
| ssh user@hostname 'cat >> ~/.ssh/authorized_keys 
&& chmod 700 ~/.ssh/ 
&& chmod 600 ~/.ssh/authorized_keys'

Add permanent usage of key (SSH Client Configuration)

You can set identity file in ~/.ssh/config as follows:

$> vi ~/.ssh/config

Add both host names and their identity file as follows:

Host server1.domain.com
  IdentityFile ~/.ssh/id_dsa
Host server2.domain.com
  IdentityFile /home/userName/.ssh/id_rsa

Temporary access with key

$> ssh -i /home/user/.ssh/id_dsa user@host

2 thoughts on “SSH key authentication

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.