Shinji Kuwayama

23 Jun, 2008

An SSH one-liner to authorize via public-key

Posted by: Shinji Kuwayama In: Tech Tips

Here’s a two-part tip on SSH productivity—you can use public-key authentication and SSH’s built-in configuration options to save some time every day.

1) Authorize your public key. If you’re like me, it’s a bother to remember each little step — here’s the one-liner I use for convenience.

> ssh someuser@remotehost.com "echo `cat ~/.ssh/id_rsa.pub` >> ~/.ssh/authorized_keys"

This will append your key to the remote host’s list of authorized keys.

Don’t have a public key yet? You can generate one with:

> ssh-keygen

You’ll find a brand-new keypair in your ~/.ssh directory. Read up on public-key cryptography if it’s new to you.

2) Now add the remote host to your (local) .ssh/config file:

Host rh
Hostname remotehost.com
User someuser

The SSH config file can contain many entries; just add this to the list. "Host" is an alias/nickname/shortcut of your choosing.

So, you can now SSH to your remote host with:

> ssh rh

This will save you a little typing. Public-key authentication is a "best practice" anyway -- it's good to develop the habit. For example, this is the only way to SSH into Amazon EC2 instances, and for administrators, it means you can share access to a SSH account (say, a deployer) without floating the password around.

Bonus tip: If you find Capistrano and Net::SSH behaving quirkily, make sure you have the latest versions of both. Recent updates to Net::SSH have included various updates and fixes relevant to public-key authentication.

cf. http://net-ssh.rubyforge.org/
cf. http://groups.google.com/group/capistrano/

1 Response to "An SSH one-liner to authorize via public-key"

1 | Steven Walker

November 4th, 2009 at 12:57 am

Avatar

I continually refer to this short but informative article, thanks Shinji. Surprising how vague the internet can be on this issue.

Comment Form

About

View Shinji Kuwayama's profile on LinkedIn Shinji Kuwayama is a Rails developer in Chicago, Illinois.

Twitter

Posting tweet...