2009-06-20 01:00:21

One line command to setup SSH keypairs

Setting up SSH keypairs isn't a terribly complex task, but it can be tedious if you have a lot of users who aren't Linux or SSH savvy. Simplifying a task to a simple command is always a nice goal when operating in such an environment.

So, keeping that in mind, here's a simple "one liner" alias that can be placed into auto executing profiles (such as /etc/profile, /etc/bashrc, etc). When a user runs the command, it will generate a custom RSA keypair, of 2048bit strength, with a null password. It copies the pub key first into the authorized_keys file on the local server, and then SSH's into the remote server to place it there, as well. Make sure to change "servername" to the name of your actual server!

Once this is done, the user is able to SSH from the local server to the remote without being prompted for a password. This is obviously useful for things like batched and automated processes.

Naturally, this type of passwordless authentication is a security concern, so use it sparingly and wisely.

export auto-keypairs='cd ~ ; mkdir ~/.ssh; ssh-keygen -t rsa -b 2048 -N "" -C "Auto Keypairs" -f ~/.ssh/rsa_auto ; cat ~/.ssh/rsa_auto.pub >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys ; ssh servername "mkdir ~/.ssh ; echo `cat .ssh/rsa_auto.pub` >> .ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys"'

Tags:   linux     |    Perm Link:   One line command to setup SSH keypairs



James Conner