Friday, January 1, 2010

remember_me, passwordless login in bash.

If you find yourself having to log in to remote servers a lot, ssh-keys could be nice to use. If you don't know about ssh-keys, here is a nice introduction.

Its quite easy to set up keys following a tutorial as this. But once you have it generated, it could be quite a hassle to do all the required steps on every machine you need to access. Especially if you find your self having to access many machines, often with more than one user.

Anyways, here's a nice bash-function to set up your keys for you.

function remember_me {
ssh $1 'mkdir -p .ssh && touch .ssh/authorized_keys \
&& chmod 0700 .ssh \
&& chmod 0600 .ssh/authorized_keys \
&& cat >> .ssh/authorized_keys' <~/.ssh/id_rsa.pub
}
Add this to your ~/.bashrc or ~/.bash_profile file. Reload your settings using

source ~/.bashrc
or equivalent. Then just type

remember_me youruser@your.domain.com
You will be prompted for your credentials and then. BAM! Your keys are set up.
On the subject; an other related tip is to setup your servers as aliases adding something like

alias server="ssh youruser@your.domain.com"
to your ~/.bashrc file.
If you set up the ssh-keys correctly you just have to type

server
And there you are. Now you're on the remote server.

No comments: