How to Run sudo Without a Password on Linux

You can run sudo without a password by adding a NOPASSWD rule to the sudoers policy. The safest approach is to allow only the exact administrative command you need instead of disabling password prompts for every sudo command.

Before changing anything, remember what this setting does: anyone or anything that gains control of your logged-in account may be able to run the permitted command as root without another authentication step. The sudoers manual documents NOPASSWD as the tag that removes authentication for matching commands.

Why sudo -s is not a passwordless sudo method

The older advice to run sudo -s does not actually configure passwordless sudo. The -s option starts a shell under sudo, but the sudo policy can still require you to authenticate first. You may also see several commands run without another prompt because sudo normally caches successful authentication for a short period. That is different from a permanent NOPASSWD rule.

Run a specific sudo command without a password

This example lets the user alex restart Nginx without a password. Replace the username, command, and arguments with values that match your system.

1. Confirm your username and the command path

Run the following commands as your normal user:

whoami
command -v systemctl

The second command may return /usr/bin/systemctl. Use the path your own system reports. Sudoers rules should identify commands by their full path.

Linux terminal showing whoami and command -v systemctl to identify the username and full command path
Check the account name and the full path of the command before writing the sudoers rule.

2. Create a dedicated sudoers drop-in file

Instead of editing /etc/sudoers directly, use visudo with a file in /etc/sudoers.d:

sudo visudo -f /etc/sudoers.d/alex-nopasswd

You will normally enter your password for this setup step. visudo is preferred because it checks syntax before accepting changes. The Ubuntu visudo manual also notes that visudo locks the file during editing and checks for parse errors.

Linux terminal opening a dedicated sudoers.d file with sudo visudo for passwordless sudo configuration
Open a separate sudoers drop-in file with visudo rather than editing the main policy file with a normal text editor.

3. Add the narrowest NOPASSWD rule you need

For the Nginx restart example, add:

alex ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx

This rule allows only that command with those arguments to run as root without authentication. To allow several specific commands, separate command specifications with commas.

If you deliberately want every sudo command for one user to skip authentication, the broad rule is:

alex ALL=(ALL:ALL) NOPASSWD: ALL

This is much less secure and is rarely the best choice on a workstation or shared system. A compromised user session would have a direct path to unrestricted root privileges.

Nano editor showing a sudoers NOPASSWD rule for restarting nginx and a commented unrestricted example
A command-specific rule is safer than granting NOPASSWD: ALL.

4. Validate and test the rule

After saving and leaving the editor, check the complete sudoers policy:

sudo visudo -c

Do not skip this check. The visudo documentation explains that sudoers policy is evaluated as a whole, so checking the complete configuration catches problems that may involve included files.

Then invalidate any cached sudo credential and test non-interactively:

sudo -k
sudo -n /usr/bin/systemctl restart nginx

The -n option tells sudo not to prompt. If the rule matches, the command runs without asking for a password. If authentication is still required, sudo exits with an error instead of waiting for input. This makes sudo -n especially useful for scripts that must never hang on a password prompt.

Linux terminal validating sudoers with visudo -c, clearing cached credentials, and testing a NOPASSWD command with sudo -n
Validate the policy, clear cached credentials, and use sudo -n to confirm that the command truly no longer needs a password.

Troubleshooting passwordless sudo

  • The command still asks for a password: confirm the username, command path, and arguments exactly match the sudoers entry.
  • The command is not allowed: run sudo -l to review the privileges assigned to your account.
  • The drop-in appears to be ignored: use a simple filename such as alex-nopasswd. Some sudoers configurations ignore files in /etc/sudoers.d whose names contain a dot or end in backup-style suffixes.
  • You need to undo the change: reopen the drop-in with sudo visudo -f /etc/sudoers.d/alex-nopasswd, remove or narrow the rule, save, and run sudo visudo -c again.

Best practice

Use passwordless sudo only when there is a concrete need, such as a tightly controlled service action or automation task. Prefer an exact command path and exact arguments, avoid NOPASSWD: ALL when possible, and keep the sudoers rule as small as the job permits. That preserves most of sudo's protection while still removing the password prompt where it is genuinely necessary.

Leave a Comment

How to Record Speaker Output on Linux with Audacity

How to Record Speaker Output on Linux with Audacity

Capture Linux desktop audio in Audacity by routing its recording stream to your output device’s monitor source. Follow PulseAudio and PipeWire steps, export tips, and fixes for silent tracks.

How to Use Dropbox on Linux: Install and Sync Your Files

How to Use Dropbox on Linux: Install and Sync Your Files

Install Dropbox on Linux from the official package, sign in, sync a local folder, manage selective sync, and check status from the command line.

How to Install Inkscape on Linux: Ubuntu, Fedora, Arch, and More

How to Install Inkscape on Linux: Ubuntu, Fedora, Arch, and More

Install Inkscape from your Linux distribution’s repositories, the Ubuntu stable PPA, Snap, or Flathub, with current commands and notes for choosing a package source.

How to Find and Remove Duplicate Files on Linux With FSlint

How to Find and Remove Duplicate Files on Linux With FSlint

Use FSlint to scan selected Linux folders, review duplicate groups, and safely delete redundant files or learn when hard-link merging is appropriate.

How to Install SABnzbd on Linux and Configure It

How to Install SABnzbd on Linux and Configure It

Install SABnzbd on Ubuntu or Debian, or run it in Docker on another Linux host. Then open the web interface and configure your provider and download folders.

How to Install the Paper GTK Theme on Linux

How to Install the Paper GTK Theme on Linux

Install the Paper GTK theme from supported Linux repositories, enable it for GTK apps in GNOME Tweaks, and see what the legacy theme can and cannot change.

How to Install and Set Up Plex on OpenMediaVault with Compose

How to Install and Set Up Plex on OpenMediaVault with Compose

The old OpenMediaVault Plex plugin is archived. Set up Plex on OMV 8 with OMV-Extras, the Compose plugin, persistent storage, and Plex’s official container.

How to Clean and Speed Up Ubuntu Safely

How to Clean and Speed Up Ubuntu Safely

Clean and speed up Ubuntu with safe APT cleanup, storage checks, and startup controls that help free space and troubleshoot a sluggish desktop.

How to Install the Hiri Email Client on Linux

How to Install the Hiri Email Client on Linux

Install Hiri on Linux with its Snap package or official tarball, add a desktop launcher, and check supported accounts plus a warning about Hiri’s old Snap release.

How to Get System Specifications for a Linux PC

How to Get System Specifications for a Linux PC

Use Fastfetch and built-in Linux commands to check your distro, kernel, CPU, memory, graphics, storage, and connected devices, then save a clear report.