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/ | 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 () {
if [ "$USER" = "$1" ]; then
# Set up user
echo "
Host $BOX
User $USER" >> $HOME/.ssh/config
# Install the key
cat ~/.ssh/ | ssh $USER@$BOX "cat - >> ~/.ssh/authorized_keys"

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

$ setupssh

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


Post a Comment