1

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'
2
  • 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, 2012 at 3:17
  • Good point. Thx @jw013
    – mitnk
    Apr 7, 2012 at 4:29

2 Answers 2

2

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.

2
  • 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, 2012 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, 2012 at 4:35
-3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .