Take the 2-minute tour ×
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.

The following command puts the content of the local id_rsa.pub into the authorized_keys file on the host.

But I don't quite understand how cat and | works together to achieve this goal, can someone explain?

cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
share|improve this question
6  
Just wanted to point out the UUOC (useless use of cat). ssh b@B 'cat >> .ssh/authorized_keys' < .ssh/id_rsa.pub is better. –  jw013 Apr 7 '12 at 3:17
    
Good point. Thx @jw013 –  mitnk Apr 7 '12 at 4:29

2 Answers 2

up vote 2 down vote accepted

The cat command outputs contents of the file .ssh/id_rsa.pub; the | (pipe) receives this text output and then sends (i.e. pipes) the text to ssh.

Then, ssh uses this text as input for the cat >> .ssh/authorized_keys command.

share|improve this answer
1  
Thanks @Renan. Now I think I understand. –  mitnk Apr 7 '12 at 4:28
1  
I think this command can be clear if we ignore ssh part ssh b@B. It will become: cat .ssh/id_rsa.pub | cat >> .ssh/authorized_keys, which I can understand. ;) –  mitnk Apr 7 '12 at 4:33
    
@mitnk pretty much that, if you see that the cat after the pipe is running on the other machine. –  Renan Apr 7 '12 at 4:35

For cat command if no file name is provided it takes it from stdin. In this case its the output of the |.

share|improve this answer

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.