I have 2 machine machine1 and machine2.

I want to write from write some contents into file in machine2 from machine1.

In a same machine I can run sudo cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys to write the contents of ~/.ssh/id_dsa.pub into ~/.ssh/authorized_keys file.

But I want to contents of ~/.ssh/id_dsa.pub machine1 into ~/.ssh/authorized_keys file in machine2. I can do ssh and do this. Since I'm installing hadoop in a silent mode in single command with username and password.

I don't want to do scp which replaces the whole file.

How can I do it in a single command supplying with username/password of another machine.

  • 1
    ssh user@machine2 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_dsa.pub. But you don't need that. Use ssh-copy-id instead. – mosvy Nov 11 '18 at 23:45
  • 1
    sudo cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys There is no point using sudo there, you should avoid using sudo unless you actually need it. – cryptarch Nov 11 '18 at 23:46

You can do something like this:

cat ~/.ssh/id_dsa.pub | ssh machine2 'cat >> ~/.ssh/authorized_keys'

But you should probably use ssh-copy-id instead, which is a tool for that exact purpose:

ssh-copy-id machine2


ssh-copy-id id_dsa machine2
  • needless cat alert – qubert Nov 12 '18 at 21:34
  • Is this the cat police ? :) – V13 Dec 16 '18 at 20:20

The ssh command captures local stdin and can also run external commands. If you combine these two facts then this becomes possible:

ssh machine2 tee -a "~/.ssh/authorized_keys" < ~/.ssh/id_dsa.pub

There is also a command called ssh-copy-id intended to automate this process. You can see man ssh-copy-id for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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