SSH Keys Howto Quicky
Posted by AntonOct 11
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.
Related posts:







No comments
Comment by Dr Jan on October 15, 2007 at 3:40 am
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.
Comment by Anton on October 16, 2007 at 7:57 am
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.
Pingback by Tech Messages | 2007-10-17 | Slaptijack on October 17, 2007 at 6:31 pm
[...] anton – SSH Keys Howto Quicky at antonolsen.comHere’s a quick script to help automate SSH key creation. [...]