Trivial but useful: One-line ssh key setup
written by Ian McCracken
at Tuesday, October 7, 2008
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:
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:
Shove that in your ~/.bashrc, source it, and type:
You'll have to enter remoteuser's password once when it pushes out your pubkey. From then on, you can just
$ 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.
October 7, 2008 at 10:16 PM
Permissions on $HOME and $HOME/.ssh are often an issue.
I don't know why I'm always doing this by hand, or why my public key isn't widely available, or why I don't just set this up by default. But no... I keep using bad passwords, empty .ssh configs and hand-entered 80-column long ssh commands.
There's this menu on ubuntu: make a remote ssh account look like a network mounted file system. Do I use it? No. Why? I'm old and crusty.
November 20, 2008 at 3:40 PM
The built in command ssh-copy-id can shorten part of this. I just discovered it today from this post.
November 20, 2008 at 3:43 PM
Sadly, ssh-copy-id isn't part of Darwin.