I have this command
cat ~/.ssh/id_dsa.pub | ssh root@[my_server] "cat >> ~/.ssh/authorized_keys"
How can I do the same thing but with sudo -iu user1
before the 2nd cat
? That is, I want to change a user after having been logged in.
If you don't mind getting the key on the screen as well (it is the public key, so this is not much of a security issue), you should be able to use tee
to avoid redirection troubles along the lines of
cat ~/.ssh/id_dsa.pub \ | ssh root@server 'sudo -i -u user1 tee -a ~user1/.ssh/authorized_keys'
tee
command with >/dev/null
and you don't get the key written to stdoutCommentedMay 24, 2022 at 6:41cat
could be replaced by an input redirection.Try this:
cat ~/.ssh/id_dsa.pub | ssh root@[my_server] $'sudo -i -u user1 bash -c "cat - >> ~/.ssh/authorized_keys"'
\n
or \t
etc. with literal newline or tab characters. However, in this instance, it would make absolutely no difference, so you may remove the $
.bash -c
by using sudo -u user1 tee -a ~user1/.ssh/authorized_keys >/dev/null
. Note that the cat
is then also not needed.
man ssh-copy-id
.