Copy your ssh public key to a server from a machine that doesn't have ssh-copy-id

cat ~/.ssh/ | ssh [email protected] "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
If you use Mac OS X or some other *nix variant that doesn't come with ssh-copy-id, this one-liner will allow you to add your public key to a remote machine so you can subsequently ssh to that machine without a password.

2009-02-05 19:37:56

19 Alternatives + Submit Alt

What Others Think

This can also be done with ssh-copy-id.
Burn · 624 weeks and 1 day ago
modded down because it absolutely duplicates ssh-copy-id ssh-copy-id [email protected]
markdrago · 624 weeks and 1 day ago
I would use: scp ~/.ssh/ [email protected]:.ssh/authorized_keys
pixel · 624 weeks and 1 day ago
Yep, what's wrong with scp?
fzero · 624 weeks and 1 day ago
pixel ... have you now not just overwritten the destination servers authorized_keys? ssh-copy-id does it all for you and as safely as possible... give it a whirl What have you got to loose? (apart from sanity, health, job, etc)
finkployd · 624 weeks and 1 day ago
it's still good for a case when your host doesn't have ssh-copy-id program, like freebsd or macosX do
codesnik · 624 weeks and 1 day ago
if you forget about ssh-copy-id, I like this version better which works even on a new machine: cat ~/.ssh/ | ssh machine 'mkdir .ssh ; cat >>.ssh/authorized_keys' to test: ssh machine id If it asks for a password, it's not working. Sometimes a server will disable passwordless logins.
shavenwarthog · 624 weeks and 1 day ago
ssh-copy-id fails if you have running sshd in other port than 22. This method work for this situation.
starchox · 618 weeks and 3 days ago
a temp solution for the workaround with ssh-copy-id for sshd in other port ssh-copy-id '-p 12345 [email protected]'
starchox · 618 weeks and 3 days ago
@pixel, @fzero scp would completely replace the authorized_keys file; cat >> appends.
mfisher911 · 615 weeks and 1 day ago
What about rsync?
hced · 475 weeks ago
For those of you asking why not just use scp or rsync, this is a solution to replace ssh-copy-id for OS's that don't have it natively (i.e. OS X). To use scp or setup rsync wouldn't be an issue if you only had a few servers. However, I have over 300 servers I needed to setup my public key on and scp or rsync would have been time prohibitive. I did however find someone who built a shell script that you can use as ssh-copy-id command that helped me immensely. It can be found at I then just created a list of all my hosts from my SecureCRT .ini files and dumped them into a file called HOSTS. I then just ran this command: for i in $(cat HOSTS); do ssh-copy-id [email protected]$i; done Did over 300 servers in less than 10 mins!
boogybren · 464 weeks and 4 days ago
On OS X you can install ssh-copy-id via Homebrew
alecthegeek · 395 weeks and 1 day ago
a bit updated version that checks if .ssh directory exist: cat ~/.ssh/ | ssh [email protected] "[ -d ~/.ssh ] || (mkdir ~/.ssh && chmod 700 ~/.ssh); cat >> ~/.ssh/authorized_keys"
franciszekk · 303 weeks and 5 days ago
Perfect! ssh-copy-id on QNAP devices is not available. This command like is perfect to do the same job. I realy thank DEinspanjer for his trick!
Olivier · 251 weeks and 4 days ago
scream, or enjoy the cicadas flap its wings when you bring the cool wind; when passing Hasuike also puffed breath goods products that Lotus aroma; hours because ignorance has been poked hornet's nest and stung his face was bruised. Dig once, because "small insects" (Sparrow) and took out a "snake" (Snake) scared to fall from the wall. Fortunately, nothing serious. Leisure time, will collect some cigarette paper folded triangle, and partners to play; (the triangle on the ground and put his hand on the ground quick photo shoot the other side of the triangle is the triangle that turned their a); and also than own older brother and sister were led to a wide flat on the grass or doing "of chicken" or "kill the lamb" of the game, before the election when one eagle, then tall in front when hens , when the rest of the chicken, the eagle chasing shot at the back of the chick, hen with open arms to protect chickens, smart dodge to keep eagle patting. Or a dozen people sitting in a circle, do a do Diushou Juan game: the hands of only one person to take the handkerchief out of his mouth in a circle singing Diushou Juan songs, danced around the circle turning quietly throw the handkerchief in the back of a person. Then run around in circles. If you have a lap behind the handkerchief patting, this people have not found the person you want to give a performance for everyone; a joke, sing songs, or back poem. Then began Diushou Juan by him or her. If this is discovered immediately took the handkerchief catch Diushou Juan people, that Diushou Juan will continue to lose, someone who can back into place; catch, and throw this man who has sat on the bit when this man will be punished performances. We will play along with a dozen "Stars over the month child" or "cat drilled 72 holes" game. (Read the article Network: I remember one summer, that the acquisition of medicinal city recovered cicada skin (medical term called cicada), catty two five. So every day, together with my brothers holding bamboo, Dianzhe plastic bags, and from the village in the woods to find a tree cicada skin, after the end of the summer, we harvest relatively big, full harvest four pounds, to buy ten dollars. The next summer we look for a summer, more than the first year looking for even more, I did not expect there to happily after being told not recovered. Cicada got hard skin, back at home after shuffling, and
zyf543 · 249 weeks and 6 days ago
trungpham · 206 weeks and 3 days ago ticket action
mancharter · 183 weeks ago
Watch Korean TV Show.Here you can watch daily Korean tv Dramas Tambayan Tv Show as well as Korean Teleserye Shows online. For more information subscribe our Korean Tv Channel website to watch latest shows.">Pinoy Channel|">Pinoy Tambayan|">Pinoy Tv|">Pinoy Tv Replay|">Pinoy Ako|">Pinoy Lambingan|">Pinoy Teleserye|">Zee tv|">Korean Tv Show
seanc8602 · 54 weeks and 5 days ago
Hahahah! That was well said! pi
aegyjingoo · 7 weeks and 3 days ago
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.">Click here
xoheko1229 · 3 weeks and 2 days ago
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. Click here
xoheko1229 · 3 weeks and 2 days ago
I felt very happy while reading this site. This was really very informative site for me. I really liked it. This was really a cordial post. Thanks a lot!. visit here
xoheko1229 · 3 weeks and 2 days ago
xoheko1229 · 3 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: