Skip to content

How to Configure sudo Without Password on Linux

DodaTech 2 min read

In this tutorial, you'll learn about How to Configure sudo Without Password on Linux. We cover key concepts, practical examples, and best practices.

The Problem

You run sudo apt update or sudo systemctl restart nginx and get prompted for a password every time. For CI/CD scripts, automated provisioning, or personal workstations, typing the password repeatedly slows down your workflow.

Quick Fix

Step 1: Edit the sudoers file with visudo

Always use visudo instead of editing /etc/sudoers directly to prevent syntax errors:

sudo visudo

Step 2: Add NOPASSWD for your user

Add this line to grant passwordless sudo for a single user:

username ALL=(ALL) NOPASSWD: ALL

Replace username with your actual username.

Step 3: Add NOPASSWD for a group

Grant passwordless sudo to all members of the sudo group:

%sudo ALL=(ALL) NOPASSWD: ALL

Step 4: Restrict to specific commands

Allow passwordless execution of specific commands only:

username ALL=(ALL) NOPASSWD: /usr/bin/apt, /usr/bin/systemctl

Now sudo apt update and sudo systemctl restart nginx work without a password, but other sudo commands still prompt.

Step 5: Create a separate sudoers drop-in file

For better organization, create a dedicated file in /etc/sudoers.d/:

echo "username ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/username-nopasswd
sudo chmod 440 /etc/sudoers.d/username-nopasswd

Step 6: Verify the configuration

Test that the change works:

sudo -k
sudo whoami
root

If it returns root without asking for a password, the configuration is correct.

Alternative Solutions

Set sudo timeout to a longer duration

Keep the password cached for longer:

Defaults timestamp_timeout=60

Use sudo -i for a root shell

Open a root shell once and run multiple commands:

sudo -i

Common Mistakes to Avoid

Editing /etc/sudoers with a regular text editor. A syntax error can lock you out of sudo. Always use visudo which validates the file before saving.

Granting NOPASSWD to ALL commands without restrictions. This gives full root access without a password. Use command-specific entries for limited use cases.

Not keeping a root shell open while testing. If you make a mistake, a second root shell can fix the sudoers file before logging out.

Pro Tips

Use sudo -l to check your sudo privileges. Before assuming NOPASSWD works, run sudo -l to see exactly which commands you can run without a password.

Use sudoers entries with command arguments. Restrict NOPASSWD to specific arguments: username ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx.

Use log_input and log_output sudo options. Enable logging for security auditing: Defaults log_input, log_output in sudoers.

Use sudo -i for a persistent root shell. When running multiple administrative commands, start a root shell with sudo -i to avoid repeating sudo for each command, even with NOPASSWD configured.

Prevention

  • Always use visudo to edit sudoers files -- it checks syntax before saving.
  • Restrict NOPASSWD to specific commands in production environments.
  • Keep a root shell open (sudo -i) while testing sudoers changes to avoid locking yourself out.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro