Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Here is what I have tried, and I got an error:

$ cat /home/tim/.ssh/id_rsa.pub | ssh tim@just.some.other.server 'cat >> .ssh/authorized_keys'
Password: 
cat: >>: No such file or directory
cat: .ssh/authorized_keys: No such file or directory

Thanks and regards!

share|improve this question
Why not do it in two steps? Copy it across and then append it? – Faheem Mitha Jan 18 '12 at 20:01
@FaheemMitha: That works, thanks! I actually might realize the cause of trouble. Please see my new post? – Tim Jan 18 '12 at 21:23

2 Answers

up vote 12 down vote accepted

OpenSSH comes with a command to do this, ssh-copy-id. You just give it the remote address and it adds your public key to the authorized_keys file on the remote machine:

$ ssh-copy-id tim@just.some.other.server
share|improve this answer
Thanks! Why my command doesn't work? – Tim Jan 18 '12 at 20:18
@Tim This answer explained it; >> is handled by your shell, and you're running the command through SSH instead of through a shell. His fix of having SSH run a shell, which then runs your command, should work – Michael Mrozek Jan 18 '12 at 20:22
Thanks! (1) ssh-copy-id doesn't work either. I manually copy the file to the remote and append its content, then it works. I wonder why is this? I found that my default shell on the server is some script, which I update to my original post, and might be the reason. Please have a look. (2) I wonder if ssh-copy-id is just copy the public key to the remote, it doesn't create the private and public key, does it? – Tim Jan 18 '12 at 20:37
Okay, I crated a new post for my default shell problem – Tim Jan 18 '12 at 21:19
Ah, ok. It turns out SSH does run commands through the default shell (so the method from the question would work on most machines), but your default shell is some bizarre wrapper script that doesn't handle it properly – Michael Mrozek Jan 18 '12 at 23:42

You could always do something like this:

scp ~/.ssh/id_rsa.pub user@remote.example.com:/tmp/id_rsa.pub
ssh user@remote.example.com 
cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys"

I'm not sure if you can cat from a local machine into an ssh session. Just move it to /tmp as suggested.

Edit: This is exactly what ssh-copy-id does. Just like Michael said.

share|improve this answer
Thanks! I wonder if ssh-copy-id is just copy the public key to the remote. It doesn't create the private and public key, does it? – Tim Jan 18 '12 at 20:51
No it doesn't create it. Just adds it. – Mr. Monkey Jan 18 '12 at 21:22
@Mr.Monkey Yes, you can pipe data into an ssh session (from cat or otherwise). What you're describing is the old-fashioned way; ssh-copy-id is recommended because there's less risk of typos or giving files wrong permissions. – Gilles Jan 18 '12 at 23:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.