You are hereBlogs / David Grant's blog / Copy ssh Public key to Server in one line

Copy ssh Public key to Server in one line


By David Grant - Posted on 23 May 2007

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 user@hostname "cat - >> ~/.ssh/authorized_keys"

Apparently some people don't like this one that much. :-)

Here's an alternative:

ssh user@hostname "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys"

It doesn't require the first cat or the pipe.

Tags

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.

No I wasn't aware of that. Thanks. My main box is Gentoo. The particular one I was using was Ubuntu Server.

There's always scp, so no need to do whacky pipes.

scp ~/.ssh/id_dsa.pub user@hostname:~/.ssh/authorized_keys

patrick, that only works if you don't mind wiping the authorized_keys file? I need to append a key while keeping the others.

Don't abuse the cat like that!

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"

thanks for key, bookmarked

This is the method I usually use:
ssh-copy-id user@remote.server.com

OMG, that's so awesome. Thanks.

Just what I was looking for. It works just as you describe. The chmod permissions commands are a nice add.

Awesome tip. Was having issues with ssh-copy-id, this worked first time.

Thanks for the command lines :)

You can't connect to non standard port with ssh-copy-id :(

ssh-copy-id -i /path/to/id_foobar.pub "-p 2222 user@server"

Great tip. Thank You.

Can anyone shed some light on how the command works?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <s> <img> <h2> <h3>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Insert Google Map macro.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may use [view:name=display=args] tags to display views.

More information about formatting options

CAPTCHA
Sorry I had to add this test to combat the spam problem.
K
r
B
g
Y
S
Enter the code without spaces and pay attention to upper/lower case.