Copy ssh Public key to Server in one line

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.

Tags: 

Comments

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 <a href="mailto:user@hostname">user@hostname</a>:~/.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.

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"

This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works.
read more

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing.
TutuApp

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

Thanks for the command lines :)

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

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:

Host machine1
 Port 2222
 User user1

This also works with sftp and scp, so makes your life much simpler.

Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing.
Sheraton Cebu

Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing.
learn here

Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing.
festari for men

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
melbourne wreckers

I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think.
38 Park Avenue

Pages

Add new comment