How to Allow Running Commands with Sudo Without a Password Prompt

Michael Kasingye
3 min readNov 2, 2023
Photo by Christina @ wocintechchat.com on Unsplash

When using sudo to run commands with elevated privileges on a Linux system, it often prompts for the user's password to verify their identity. However, in some cases, you may want to allow certain commands to be executed with sudo without a password prompt. This can be useful, for example, in automation scripts or CI/CD pipelines.

In this blog post, we’ll guide you through the process of allowing specific commands to run with sudo without requiring a password prompt. We'll use an example of running Docker commands without a password prompt in a GitHub Actions workflow.

Step 1: Edit the sudoers File

The first step is to edit the sudoers file, which controls the sudo permissions for users on the system. You should use the visudo command, which provides a safe way to edit the file.

sudo visudo

Step 2: Define the NOPASSWD Rule

In the sudoers file, you can define a rule that specifies which user can run a specific command without a password prompt. The syntax for such a rule is as follows:

username ALL=(ALL) NOPASSWD: command
  • username: Replace this with the username for which you want to allow the command without a password prompt.
  • ALL: This allows the user to run the command as any user.
  • NOPASSWD: This keyword indicates that no password prompt is required.
  • command: Specify the full path to the command that you want to allow.

For example, to allow the user “sprintdevs” to run Docker commands without a password prompt, you can add the following line to the sudoers file:

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

Make sure that the path to the command is correct and matches the actual command you intend to run.

Step 3: Save and Verify

After adding the rule, save the sudoers file and exit the visudo editor. It will automatically check the file for syntax errors. If there are any errors, you'll be prompted to correct them. Ensure that there are no syntax errors in your sudoers file.

Step 4: Test the Configuration

Once you’ve saved the sudoers file, you can test the configuration by running the specified command with sudo. It should execute without a password prompt.

For example, to test the Docker command, you can use:

sudo /usr/bin/docker [your-docker-command]

Replace [your-docker-command] with the actual Docker command you want to execute.

Conclusion

By following these steps, you can allow specific commands to be executed with sudo without a password prompt. This can be particularly useful in scenarios where automation or CI/CD pipelines require running privileged commands.

Please note that configuring sudo without password prompts should be done with caution to ensure the security of your system. Only grant this privilege to trusted users and for specific commands that are necessary for your workflow.

If you encounter any issues or have specific requirements, consult your system administrator or refer to the sudo documentation for more information.

That’s it! You’ve successfully configured sudo to allow specific commands without a password prompt.

--

--

Michael Kasingye

I am a software developer. I love to build and work with teams to establish virtual platforms and systems that meet user satisfaction.