How to Apply Password to Sudo in One Line Command and Execute Su Root

sudo su with user name, password and command

I've got two answers for you.

The first answer is "don't do it". There is, almost certainly, a better way. You can specify to sudo that certain users can perform certain commands without entering a password. In all likelihood, that is what you want to do.

Having failed to convince you, however, I will let you in on a little secret. sshpass works on sudo, so:

sshpass -p 'my password' sudo -S su -c some-user ./some_command

Of course, while there, we can cut the su middle man:

sshpass -p 'my password' sudo -S -u some-user ./some_command

How to pass the password to su/sudo/ssh without overriding the TTY?

For sudo there is a -S option for accepting the password from standard input. Here is the man entry:

    -S          The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device.

This will allow you to run a command like:

echo myPassword | sudo -S ls /tmp

As for ssh, I have made many attempts to automate/script it's usage with no success. There doesn't seem to be any build-in way to pass the password into the command without prompting. As others have mentioned, the "expect" utility seems like it is aimed at addressing this dilemma but ultimately, setting up the correct private-key authorization is the correct way to go when attempting to automate this.

How to enter sudo password if there are several commands on the same line in bash console

You could reverse it and execute the script explicitly as your normal user

sudo sh -c 'sudo -u $SUDO_USER test.py && shutdown now'

How to sudo su; then run command

Unless you have an unusual setup, you can't normally string su with other preceding commands like that. I would imagine it is running sudo su, then hanging in the root environment/session, because it's waiting for you to exit before preceding to the pm2 commands. Instead, I would consider something along the lines of this using the -c option:

CMD="sudo su -c 'pm2 restart 0; pm2 restart 1'"
ssh -i somepemfile.pem ubuntu@1.1.1.1 "$CMD"

As suggested in another answer, it would also probably be useful to encapsulate the $CMD variable in quotes in the ssh call.

pass a password to sudo su

You setup password-less ssh to localhost as user xyzuser for abcuser to achieve what you're trying. You'll need to add abcuser's public key as an authorized_key for xyzuser.

Then when you're logged in as abcuser, you can do:

ssh xyzuser@localhost do_something_as_xyzuser

If you've blocked ssh access to xyzuser, allow it only through loopback. If you're on linux, see your /etc/security/access.conf or equivalent on how to do that.

How can I pass a password to the su command?

Well, the best way to do that would be a setuid-root binary that ask for the password then execute whatever command is needed but it requires knowledge you say not to possess. I'd advise in looking at sudo(1) instead.



Related Topics



Leave a reply



Submit