| Subcribe via RSS

SSH Keys Howto Quicky

October 11th, 2007 Posted in Bash, HOWTO

Toss this little shell script in your bin dir and you can quickly create and setup ssh keys between your client and server. I called it sshkeys.sh but you can name it what you want.

#!/bin/bash

KEY_PRIVATE="$HOME/.ssh/id_dsa"
KEY_PUBLIC="${KEY_PRIVATE}.pub"

if [ "$1" == "" ] ; then
   echo "Usage: $0 <[user@]server>"
   exit
fi

if [ ! -f "${KEY_PRIVATE}" ] ; then
   echo Creating the private and public keys.
   ssh-keygen -t dsa -f "${KEY_PRIVATE}" -N ''
fi

if [ -f "${KEY_PUBLIC}" ] ; then
   cat "${KEY_PUBLIC}" |
   ssh "${1}" "mkdir -p ~/.ssh ; cat >> .ssh/authorized_keys2 ; chmod -R go-rwx ~/.ssh"
else
   echo Unable to find "${KEY_PUBLIC}"
fi

Run this, enter your pass once, and then you’re free to ssh without entering a password.

Watch for line wraps in your browser, especially the ssh line.

No Responses to “SSH Keys Howto Quicky”

  1. Dr Jan Says:

    Thanks for the nifty script :-)
    When setting this up previously I’ve found that I had to reset the permissions on both the .ssh directory (600) and the user’s home directory (755) before I could login without passwords.


  2. Anton Says:

    The home directory shouldn’t matter, but it’s important that on the remote system the public key not be writable by anyone but you.

    I’ve updated the script above to add a chmod just to ensure it’s all setup right.

    It’s probably overkill since most systems I tested allowed the public key to be read by anyone, just not writable.


  3. Tech Messages | 2007-10-17 | Slaptijack Says:

    [...] anton - SSH Keys Howto Quicky at antonolsen.comHere’s a quick script to help automate SSH key creation. [...]


Leave a Reply