1

I'm using a python script to run docker, but docker requires sudo commands to run and it's bad practice to store the sudo password in the file or give the whole python script sudo rights, I've seen a method where you enable the system to run certain commands without entering a password, I've tried it but I can't get my head around it, I mainly entered it wrongly (I think) I even corrupted my sudoers file once and fixed it.

So can anyone explain this line for me and why it's not working?

username ALL = (root) NOPASSWD: /usr/bin/docker
Maythux
  • 87,123
Newbie
  • 590
  • 1
  • 5
  • 12

2 Answers2

4

To prevent corrupting the /etc/sudoers file, always use the visudo command. If you don't like the default editor set the EDITOR environment variable to your desired one.

Put your user specification line as the last line in the sudoers file.

Update:

Please see: Adding NOPASSWD in /etc/sudoers doesn't work

3

Run the command:

sudo visudo

Now go to the entry of %sudo

 %sudo   ALL=(ALL:ALL) ALL

and replace it with:

 %sudo   ALL=(ALL) NOPASSWD:/usr/bin/docker

this will affect all sudo users. If you just want to do that option for one specific user"must have sudo permissions":

user ALL=(ALL) NOPASSWD:/usr/bin/docker

Now save and exit.

To be sure that everything is correct run the command:

sudo docker

It should run without prompting for user password.

Hint: Be sure your user has sudo permissions and be sure the path of the command is correct. you can check using which docker

Maythux
  • 87,123