0

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.

2
  • 1
    ssh user@machine2 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_dsa.pub. But you don't need that. Use ssh-copy-id instead.
    – user313992
    Nov 11, 2018 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, 2018 at 23:46

2 Answers 2

4

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

or

ssh-copy-id id_dsa machine2
2
  • needless cat alert
    – user313992
    Nov 12, 2018 at 21:34
  • Is this the cat police ? :)
    – V13
    Dec 16, 2018 at 20:20
4

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.

0

You must log in to answer this question.

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