2

Can I input a password on the sudo command itself? Something like:

sudo rm (dir) -p (password)

I used sudo help but couldn't find a correct syntax (if there is one)

4

No. The best you can do is to:

  • configure sudo such that a user or group of users never need to enter a password for sudo
  • configure sudo such that a user or group of users never need to enter a password when running a particular script with sudo.

The latter is recommended practice. Write a wrapper script that does exactly what needs to be done as root, preferably without taking any user input (e.g. from command-line or env vars), and nothing more. If you do need to take user input for the script, make sure you properly quote all variables and use other defensive-programming practices.

3

From this StackOverflow thread, it seems there is a flag -S that tells sudo to read the password from standard input. The example given there is

echo myPassword | sudo -S ls /tmp

However it doesn't seem there's any way to give the password as an argument to sudo directly.

Also, the security implications of using the password this way must be considered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.