0

I had been trying to write PermitRootLogin yes in /etc/ssh/sshd_config file as a normal user.

In search of that from somewhere I got this,
echo "PermitRootLogin" yes | sudo tee -a /etc/ssh/sshd_config

Also, I got to know that "I can explicitly pass the root password for any sudo command on the same line"
Like echo <password> | sudo -S apt-get install tmux

So, my question is how can I merge these two commands and make a single command, that appends the PermitRootLogin yes to the specified file as a normal user using a root password.

pLumo
  • 26,204
  • 2
  • 57
  • 87
Aditya Vaste
  • 27
  • 1
  • 4
  • 1
    Don't! Use `sudoers` config if you need to allow [passwordless `sudo`](https://askubuntu.com/questions/334318/sudoers-file-enable-nopasswd-for-user-all-commands?rq=1). Be aware of the consequences! – pLumo Jul 21 '22 at 06:13
  • Thanks for the answer @pLumo, I got to know that method by which I was trying to achieve the results is Insecure. So I will look up `sudoers`. – Aditya Vaste Jul 21 '22 at 06:20
  • @pLumo Yeah, `-S` is necessary, I forgotten – Aditya Vaste Jul 21 '22 at 06:22

1 Answers1

1

You could achieve this with a nested bash -c or sh -c command:

echo "mypassword" | sudo -S bash -c 'tee -a /etc/ssh/sshd_config <<< "PermitRootLogin yes"'

#or

echo "mypassword"  | sudo -S sh -c 'echo "PermitRootLogin" yes | tee -a /etc/ssh/sshd_config'

Note, this is fairly unsafe. Consider at least adding sudo -S to HISTIGNORE (see also).

Generally better option would be to use sudoers configuration to allow some (or all) commands passwordless.


Anyways, it is generally a very bad idea to allow root login through ssh!

pLumo
  • 26,204
  • 2
  • 57
  • 87