Comments on: How to Run ‘sudo’ Command Without Entering a Password in Linux https://www.tecmint.com/run-sudo-command-without-password-linux/ Tecmint - Linux Howtos, Tutorials, Guides, News, Tips and Tricks. Thu, 13 Jul 2023 11:10:12 +0000 hourly 1 By: Nitin https://www.tecmint.com/run-sudo-command-without-password-linux/comment-page-1/#comment-1472946 Thu, 08 Apr 2021 15:34:06 +0000 http://www.tecmint.com/?p=24302#comment-1472946 In response to the question asked here: https://stackoverflow.com/questions/25189348/unable-to-provide-password-to-a-process-with-subprocess-python

On Linux you can bypass the sudo prompt using:

````bash
echo  | sudo -S whoami
````

Pass the formatted command to subprocess and you are good to go:

````python
import subprocess as sp

cmd = "whoami"
password= ""

stdout, stderr = sp.Popen("echo {} | sudo -S {}".format(password, cmd), shell=True, stdout=sp.PIPE, stderr=sp.PIPE).communicate()

print(stdout.decode())

if stderr:
    print("error occured:")
    print(stderr.decode())
````

It should print `root` as the command is executed by the sudo user.

]]>
By: Madhusudhan Kasula https://www.tecmint.com/run-sudo-command-without-password-linux/comment-page-1/#comment-1316652 Fri, 14 Feb 2020 15:09:48 +0000 http://www.tecmint.com/?p=24302#comment-1316652 Alternately you can use python pudo package: https://pypi.org/project/pudo/1.0.0/

Installation:

$ sudo -H pip3 install pudo # you can install using pip2 also

Below is the code snippet for using in python automation for running commands under root privilege::

    user$ python3 # or python2
    >>> import pudo
    >>> (ret, out) = pudo.run(('ls', '/root')) # or pudo.run('ls /root')
    >>> print(ret)
    >>> 0
    >>> print(out)
    >>> b'Desktop\nDownloads\nPictures\nMusic\n'

Below is the cmd example for running commands under root privilege

    user$ pudo ls /root
    Desktop  Downloads  Pictures  Music
]]>
By: Aaron Kili https://www.tecmint.com/run-sudo-command-without-password-linux/comment-page-1/#comment-875892 Wed, 15 Mar 2017 21:30:49 +0000 http://www.tecmint.com/?p=24302#comment-875892 In reply to Hubo.

@Hubo

Yap, according to the sudoers man page, which explains that, “Multiple users and groups may be present in a Runas_Spec(run as user or group), in which case the user may select any combination of users and groups.

In this example:

aaronkilik ALL = (root, bin : operator, system) ALL

The user aaronkilik may run any command as either user root or bin, optionally setting the group to operator or system. ”

For additional info, go through the sudoers man page:
man sudoers

]]>
By: Hubo https://www.tecmint.com/run-sudo-command-without-password-linux/comment-page-1/#comment-875788 Wed, 15 Mar 2017 10:32:45 +0000 http://www.tecmint.com/?p=24302#comment-875788 In reply to Hubo.

Sorry, some mistakes have occured, the syntax to add a record , for example, “root ALL=(user1, user2: group1, group2) NOPASSWD: ALL”, is it right, Thanks.

]]>
By: Hubo https://www.tecmint.com/run-sudo-command-without-password-linux/comment-page-1/#comment-875782 Wed, 15 Mar 2017 10:26:02 +0000 http://www.tecmint.com/?p=24302#comment-875782 In reply to Aaron Kili.

Follow my understanding, the syntax to add a record is ” =( [, …]:[ ,…]) “, is it right, Thanks.

]]>