Linux – How to change the root password using sudo over SSH

bashlinuxsshsudo

I am trying to reset the root password over SSH, using sudo, as directly logging in as root is disabled.

I'm using the below command to reset the password:

sshpass -f .pass ssh kj@server2 'bash -s' < password.sh

When running this I am prompted for the sudo password and it's failing.

password.sh has the below content.

MYPASS=abcd@123

echo $MYPASS | sudo -kS bash -c 'echo passwd | passwd root --stdin'

How can I successfully reset the password using SSH and sudo?

Best Answer

I was able to resolve this by running the below command:

sshpass -f .pass ssh kj@server2 "echo abcd@123 | sudo -kS bash -c 'echo passwd | passwd root --stdin'"

If there is a better solution I'd welcome your suggestions.

Related Question