Use Sudo With Password as Parameter

How to supply sudo with password from script?

If, as you say, you completely don't care about security...

Run visudo to edit /etc/sudoers with validation in place. Add the following line:


This will prevent sudo from ever asking for a password, for any user, for any 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.

su pass password to script

From man sudo:

-S    The -S (stdin) option causes sudo to read the password from the standard
input instead of the terminal device. The password must be followed by a
newline character.

So, while it defies all security principles, echo 'password' | sudo -S su [...] should work.

Alternatively, you could make your script writeable only by root and add the following to /etc/sudoers to allow the user johndoe to run it with root priviledges without having to enter his password:

johndoe ALL = NOPASSWD: /full/path/to/your/script

The part writeable only by root is important to prevent johndoe from modifying the script and executing arbitrary commands as root.

Storing sudo password as variable in script - is it safe?

No because you can see it via /proc/$PID/cmdline.

I suggest not to try to reinvent security tools. The sudo program can cache your password.

Using sudo with Python script

sudoPassword = 'mypass'
command = 'mount -t vboxsf myfolder /home/myuser/myfolder'
p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

Try this and let me know if it works. :-)

And this one:

os.popen("sudo -S %s"%(command), 'w').write('mypass')

How to pass pasword in call method of subrpocess module

Use the -S option to pipe the password to sudo.

echo "mypass" | sudo -S ifconfig...

or with Python subprocess, according to the duplicate answer:

call('echo {} | sudo -S {}'.format("mypass", "ifconfig..."), shell=True)

New users tend to overuse sudo. Sudo runs commands with the security privileges of another user. Sudo defaults to superuser or root privileges.

Administrators can configure which users are permitted to run which commands as another user, with audit trails and logs. The file, /etc/sudoers configures who can use sudo without a password, among other things. More...

Another way to send a password to sudo during invocation is to use the -A or --askpass option. Sudo uses the environment variable, SUDO_ASKPASS to tell sudo where to look for a program to supply a password on the standard output. One could use a graphical program like zenity to pop up a window asking for a password. Or simply create a script to echo the password. Although this works as a demo, it is probably a bad idea to have plain text passwords sitting around on the filesystem.

$ export SUDO_ASKPASS=$(pwd)/
$ echo "echo mypassword" >; chmod +x
$ sudo -A -u testuser bash
# whoami

Sudo has many command options that were unknown prior to answering this question. Type man sudo to see what this author learned today.

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 && shutdown now'

Specify sudo password for Ansible

You can pass variable on the command line via --extra-vars "name=value". Sudo password variable is ansible_sudo_pass. So your command would look like:

ansible-playbook playbook.yml -i inventory.ini --user=username \
--extra-vars "ansible_sudo_pass=yourPassword"

Update 2017: Ansible now uses var ansible_become_pass. Either seems to work.

Update 2021: ansible_become_pass is still working, but for now, we should use -e instead of --extra-vars

Related Topics

Leave a reply