Understanding Sudoers File: Allowing Execution of Specific Commands for a User

Abstract: Grant a specific user passwordless sudo access to a single command by adding a NOPASSWD rule with the absolute binary path to /etc/sudoers via visudo.

Created: 2026-09-14 by
To allow a specific user to run a command without a password, edit the /etc/sudoers file using visudo. Add a line specifying the username, host, and full path to the executable. This method grants precise privileges while maintaining system security by avoiding global sudo access.

Understanding the Sudoers Configuration

The /etc/sudoers file is the configuration file that defines who can use the sudo command and what commands they are allowed to execute. It is critical to understand that this file controls privilege escalation on Linux systems. Modifying it incorrectly can lock you out of administrative functions or create significant security vulnerabilities. The standard and safest way to edit this file is by using the visudo command. This utility performs a syntax check before saving changes, preventing broken configurations that could render the system unusable.

By default, members of the sudo group have full root access. However, in many scenarios, such as running a web server script or managing services, you only need to grant permission for a single specific command. Granting NOPASSWD access to a single binary is far more secure than giving a user general root capabilities. This approach follows the principle of least privilege, ensuring that even if an attacker compromises the user account, their ability to harm the system remains limited to that specific action.

Identifying the Command Path

Before editing the sudoers file, you must know the absolute path to the command you wish to authorize. Sudo requires the full path because it does not rely on the user's $PATH environment variable for security reasons. To find the path, use the which command followed by the program name. For example, to find the location of the systemctl command, you would run:

which systemctl

This typically returns /usr/bin/systemctl. It is important to note that some commands might be aliases or shell built-ins. Sudo can only execute external binaries. If the command is a shell function or alias, you must either call the underlying binary directly or wrap the execution in a script. For service management, /usr/bin/systemctl is the standard target. Always verify the path to ensure accuracy, as typos here will result in permission denied errors.

Editing the Sudoers File

Open the sudoers file with elevated privileges using the visudo command. This ensures that the file is locked against concurrent edits and checked for syntax errors upon exit:

sudo visudo

Scroll to the bottom of the file or locate the section dedicated to user specifications. The syntax for a custom rule is as follows:

username ALL=(ALL:ALL) NOPASSWD: /path/to/command

Let us break down these components. The first field is the username, which should match the login name of the user requiring access. The second field, ALL, specifies from which hosts the user can run the command; typically, this is left as ALL for local servers. The third part, (ALL:ALL), indicates that the command runs as any user and any group, which is standard for most administrative tasks. The keyword NOPASSWD tells sudo not to prompt for a password. Finally, the absolute path to the command must be specified exactly.

For our example, allowing a user named deploy to start services without a password, the line would look like this:

deploy ALL=(ALL:ALL) NOPASSWD: /usr/bin/systemctl

Save the file and exit. Visudo will automatically validate the syntax. If there is an error, it will warn you and ask if you want to abort the changes. Do not ignore these warnings. Correct any syntax issues before proceeding.

Refining Command Arguments

In some cases, you may want to restrict the user to only start services, but not stop them or restart them. You can do this by appending specific arguments to the command path. For instance, to allow only starting services:

deploy ALL=(ALL:ALL) NOPASSWD: /usr/bin/systemctl start *

Note that wildcards are allowed in argument lists, but they must be quoted or escaped properly depending on the shell interpretation. However, using wildcards can sometimes introduce security risks if not carefully managed. A safer approach is often to list each allowed subcommand explicitly, such as /usr/bin/systemctl start * /usr/bin/systemctl status *. This provides granular control over what the user can achieve with the tool.

Testing the Configuration

After saving the changes, test the configuration by switching to the target user or using su. Attempt to run the command using sudo. Since NOPASSWD is set, you should not be prompted for a password. If you receive a "permission denied" error, double-check the username spelling and the command path. Also, ensure that the user is not being overridden by a more general rule higher up in the file. Sudo reads rules in order, and the last matching rule usually takes precedence, but specific user definitions generally override group memberships.

For additional context on securing your Ubuntu server, consider reviewing Securing an Ubuntu Server for Development Purposes. Properly configured sudo rules are just one layer of defense in a comprehensive security strategy.

Frequently Asked Questions

Can I use wildcards for the command path?

No, you cannot use wildcards in the command path itself. The path must be an exact match to the binary location. Wildcards are only permitted in the arguments section after the path. Using a wildcard in the path will cause the rule to fail or behave unpredictably.

What happens if I make a syntax error in sudoers?

If you use visudo, the editor will detect syntax errors before saving. If you bypass visudo and edit the file directly, you may lock yourself out of sudo access. In such cases, you must boot into recovery mode or use a live USB to fix the file manually.

Does NOPASSWD affect other commands?

No, the NOPASSWD tag applies only to the specific command listed in that line. The user will still need to enter their password for any other sudo commands not explicitly listed with NOPASSWD. This keeps the rest of the system secure.

How do I remove a sudoers rule?

Edit the file again using visudo and delete the line corresponding to the user and command. Save the file and exit. The changes take effect immediately without needing a reboot. Always verify the change by testing the command as the user.

Is it safe to allow systemctl without arguments?

Allowing /usr/bin/systemctl without argument restrictions gives the user full control over all systemd services. They can stop critical services, enable malware persistence, or disrupt system functionality. It is highly recommended to restrict arguments to only the necessary actions like start or status.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More