Update (2011/02/06): you don't really need any of the commands below, you can just use the ssh-copy-id program that comes with ssh.
Here it is:
cat ~/.ssh/id_dsa.pub | ssh <a href="mailto:user@hostname">user@hostname</a> "cat - >> ~/.ssh/authorized_keys"
Apparently some people don't like this one that much. :-)
Here's an alternative:
ssh <a href="mailto:user@hostname">user@hostname</a> "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys"
It doesn't require the first cat or the pipe.
Comments
gregf (not verified)
Wed, 2007-05-23 19:49
Permalink
Nice one liner, if your
Nice one liner, if your using gentoo theres also a bash script that comes with ssh. /usr/bin/ssh-copy-id just in case you were unaware.
David Grant
Wed, 2007-05-23 21:48
Permalink
Thanks
No I wasn't aware of that. Thanks. My main box is Gentoo. The particular one I was using was Ubuntu Server.
bonsaikitten (not verified)
Thu, 2007-05-24 01:10
Permalink
Even easier ...
There's always scp, so no need to do whacky pipes.
David Grant
Thu, 2007-05-24 10:54
Permalink
patrick, that only works if
patrick, that only works if you don't mind wiping the authorized_keys file? I need to append a key while keeping the others.
Ciaran McCreesh (not verified)
Thu, 2007-05-24 04:12
Permalink
Miaow!
Don't abuse the cat like that!
Russell (not verified)
Thu, 2008-12-04 20:26
Permalink
Or if you want it to handle
Or if you want it to handle the directory and file maybe being there and maybe not (as well as still appending to existing ones):
cat ~/.ssh/id_rsa.pub | ssh user@hostname "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && cat - >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh"
bukmacher (not verified)
Thu, 2009-11-12 09:55
Permalink
thanks for key, bookmarked
thanks for key, bookmarked
Jonathan (not verified)
Wed, 2009-12-09 03:10
Permalink
Alternative method
This is the method I usually use:
ssh-copy-id user@remote.server.com
David Grant
Wed, 2009-12-09 08:42
Permalink
Awesome
OMG, that's so awesome. Thanks.
jayp (not verified)
Thu, 2010-12-30 11:52
Permalink
Thanks!
Just what I was looking for. It works just as you describe. The chmod permissions commands are a nice add.
Anthony Oliver (not verified)
Wed, 2011-04-20 09:33
Permalink
Awesome tip. Was having
Awesome tip. Was having issues with ssh-copy-id, this worked first time.
Varazir (not verified)
Sun, 2011-10-09 02:07
Permalink
Thanks
Thanks for the command lines :)
You can't connect to non standard port with ssh-copy-id :(
Anonymous (not verified)
Thu, 2012-02-16 00:04
Permalink
connect to a port != 22
ssh-copy-id -i /path/to/id_foobar.pub "-p 2222 user@server"
Colin (not verified)
Thu, 2014-06-26 00:31
Permalink
Re: connect to a port != 22
If you are wanting to add your ssh key to a host to speed up logging into it then you should really have an entry in your ~/.ssh/config file:
This also works with sftp and scp, so makes your life much simpler.
Amjad Mohamed (not verified)
Sat, 2012-02-25 02:20
Permalink
Great tip. Thank You. Can
Great tip. Thank You.
Can anyone shed some light on how the command works?
Anonymous (not verified)
Thu, 2013-03-21 14:17
Permalink
Thanks! One liner is really
Thanks!
One liner is really handy as ssh-copy-id is not available mac!
Add new comment