Using SSH shared keys


OpenSSH has the ability to use a number of authenitcation mechanisms. One of the most popular (next to plain passwords) is using shared keys.  Shared keys can give you a lot of security while still allowing more flexability than simple passwords. Once the keys are setup, it allows a user to secure shell into a box without a password.  This is only used when you trust the remote users account as much as if it was on the local box. If the remote user is on an insecure machine or not trusted then another authentication mechanism would be a better choise. The real benifit for shared keys comes in when you need a script on one machine to be able to run a program on another machine in a secure and encrypted manner.

An example Setup 

ClientA wants to secure shell into ServerB and run a program without getting prompted for a password.
Here is how to setup the secure keys.

On ServerB (logged in as the user the will have the remote access):

Check first to see if ~/.ssh exists and if now then create it.

serverB$ mkdir ~/.ssh
serverB$ chmod 700 ~/.ssh

Generate the public and private keys

serverB$ ssh-keygen -q -f ~/.ssh/ClientA.key -t rsa -N "" -C "Key for ClientA Access"

Move the key over to the client in a secure manner (NOT Email). I’ll use ssh for this example.

serverB$ scp ~/.ssh/ClientA.key

Edit the public key and tighten up the security.

The from variable is a comma delimited list of hosts that you will accept this key from. Make sure that the client has a reverse pointer record setup for this to work. An example of what the from variable should look like is as follows:


The command variable lists the command that the remote user will be able to run when they execute the secure shell connection. The command listed is the only command that they will be able to run with this key. If this key is setup for shandard shell access than do not include a command variable. An example of what the command variable should look like is as follows:


Edit the public key ( ~/.ssh/ ) and add the from and command variable to the begining of the file as follows:

from="clienta,",command="/usr/local/bin/program" ssh-rsa AAAAB3NzaC1
LycWcmpAr87cIhm/hvC5sJY5r4hxXPi+eemONNpw== Key for ClientA Access

Copy the file to the end of the authorized_keys file

serverB$ cat >> ~/.ssh/authorized_keys
serverB$ chmod 600 ~/.ssh/authorized_keys

1 thought on “Using SSH shared keys”

  1. Pingback: Sync’ing content between two servers. –

Leave a Comment

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