Trivial but useful: One-line ssh key setup

For some reason, a lot of people have issues setting up ssh key authentication on remote boxes; sometimes it's cut-and-paste problems, sometimes mistakes editing the file manually. Also some aren't aware that you don't need to push your pubkey file up to the box, then ssh in and copy/paste the contents into authorized_keys. You can do it all in one go:

$ cat ~/.ssh/id_dsa.pub | ssh user@remotebox \
"cat - >> ~/.ssh/authorized_keys"

A minor improvement to a small problem, perhaps, but it removes a bunch of failure-prone steps.

Update: I wrote a function that pushes your key out to the remote box, then modifies your ~/.ssh/config to use the specified username:

setupssh () {
USER=${1%@*}
BOX=${1#*@}
if [ "$USER" = "$1" ]; then
USER=`whoami`
else
# Set up user
echo "
Host $BOX
User $USER" >> $HOME/.ssh/config
fi
# Install the key
cat ~/.ssh/id_dsa.pub | ssh $USER@$BOX "cat - >> ~/.ssh/authorized_keys"
}

Shove that in your ~/.bashrc, source it, and type:

$ setupssh remoteuser@my.remotebox.com

You'll have to enter remoteuser's password once when it pushes out your pubkey. From then on, you can just ssh my.remotebox.com, no passwords.

3 comments

Post a Comment