Skip to content

SSH Keys Howto Quicky

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.

2 Comments

  1. 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.

    Posted on 15-Oct-07 at 3:40 am | Permalink
  2. 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.

    Posted on 16-Oct-07 at 7:57 am | Permalink

One Trackback/Pingback

  1. Tech Messages | 2007-10-17 | Slaptijack on 17-Oct-07 at 6:31 pm

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

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*