
September 11, 2026
13 min read
By Kokil Thapa | Last reviewed: September 2026
Your deploy script fails because a CI user cannot restart PHP-FPM. A contractor needs server access but root login is disabled. This Ubuntu Sudo Permissions Guide shows how to grant the right elevated privileges without opening the whole machine. Sudo is the standard privilege boundary on Ubuntu server setups from 22.04 LTS through 24.04 LTS. You will learn safe sudoers editing, group-based rules, command-scoped access, and the mistakes I still see on production web hosts.
/etc/sudoers and /etc/sudoers.d/. Edit only with visudo, grant users the sudo group for full admin, or write narrow rules that allow specific commands as root without a password when automation requires it.What is sudo and how does Ubuntu Sudo Permissions work?
Sudo lets an ordinary user run selected commands as another user, usually root. Ubuntu ships sudo by default on desktop and server images. The binary checks /etc/sudoers plus files under /etc/sudoers.d/ before it runs anything elevated.
That check happens on every invocation. Sudo logs attempts to syslog and often to /var/log/auth.log. On client projects I maintain, those logs are the first place I look when a deploy user suddenly loses access after a package upgrade.
How sudo differs from su and direct root login
su - switches your entire shell to root if you know the root password. Sudo runs one command with elevated rights and keeps your user context for everything else. Ubuntu disables the root account password by default. That design pushes you toward auditable, per-user elevation instead of shared root credentials.
For web servers running Laravel, WordPress, or Symfony, I treat sudo as an operational tool. Developers get narrow rights. The deploy user gets only what the release pipeline needs. Nobody gets passwordless full sudo on a public-facing box unless there is a documented reason.
How do you safely edit the sudoers file on Ubuntu?
Never open /etc/sudoers in a normal text editor on a live server. A syntax error can lock every admin out, including you. Always use visudo, which validates syntax before it saves.
sudo visudo To add a drop-in file instead of touching the main file, use:
sudo visudo -f /etc/sudoers.d/deploy-user Drop-in files must be owned by root, mode 0440, and must not contain a dot in the filename. Ubuntu ignores files with dots or wrong permissions. That silent ignore has caused hours of confusion on servers I inherited.
Basic sudoers syntax you will use daily
Each rule follows this pattern:
who where=(as_whom) options: commands Examples that work on Ubuntu 22.04 and 24.04:
# Full admin for members of the sudo group (default Ubuntu line)
%sudo ALL=(ALL:ALL) ALL
# Allow user deploy to restart PHP-FPM only
deploy ALL=(root) NOPASSWD: /bin/systemctl restart php8.3-fpm
# Allow www-data to reload nginx without a password
www-data ALL=(root) NOPASSWD: /bin/systemctl reload nginx The % prefix means a group. ALL in the host field means any hostname. (ALL:ALL) means run as any user and any group. Options like NOPASSWD: sit before the command list.
Step-by-step: grant sudo to a new user
- Create the user if needed:
sudo adduser contractor - Add them to the sudo group:
sudo usermod -aG sudo contractor - Confirm group membership:
groups contractor - Test as that user:
sudo -lthensudo whoami - Verify logging:
sudo grep contractor /var/log/auth.log
For deeper user lifecycle work, see the Ubuntu user management guide. Pair sudo grants with correct file ownership and permissions on web roots and shared release directories.
Which Ubuntu groups and sudo rules should you use in production?
Ubuntu creates a sudo group on server installs. Members inherit the default line in /etc/sudoers that grants full admin. Desktop images historically used admin as an alias to the same privilege set. On modern Ubuntu server images, sudo is the group you should standardise on.
Production servers rarely need many full-sudo humans. A pattern I repeat on Deployer 7 hosts looks like this:
- One break-glass admin account with MFA-backed SSH and full sudo
- A
deployuser with passwordless rights tosystemctl reload php8.3-fpm,systemctl reload apache2, and the deploy script path - Application users such as
www-datawith no sudo at all - Developers SSH as their own user, then sudo only for logs and service status
Sister legal-tech sites I maintain on shared EC2 infrastructure follow that split. Full sudo stays with the operator. The GitLab runner user gets a three-line drop-in file. That limits blast radius when a pipeline credential leaks.
| Access pattern | Typical use case | Risk level | Recommended approach |
|---|---|---|---|
| Full sudo group | Server owner, senior admin | High | SSH keys, no shared accounts, monitor auth.log |
| Command-scoped NOPASSWD | CI/CD, cron reload hooks | Medium | Absolute binary paths, no shell wrappers |
sudo -u www-data | Run artisan as web user | Low–medium | Grant only to deploy user, not all developers |
| No sudo | DB service accounts, app users | Lowest | Default for www-data, mysql system users |
Command aliases and defaults blocks
Large teams sometimes define aliases at the top of a drop-in file:
Cmnd_Alias DEPLOY_CMDS = /bin/systemctl reload nginx, \
/bin/systemctl reload php8.3-fpm, \
/usr/local/bin/dep deploy production
deploy ALL=(root) NOPASSWD: DEPLOY_CMDS The Defaults section controls behaviour globally. Useful hardening entries include:
Defaults logfile="/var/log/sudo.log"
Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Defaults use_pty secure_path stops a user from shadowing system binaries through a manipulated PATH. use_pty allocates a pseudo-terminal, which improves logging and reduces some privilege-escalation tricks. Both appear in Ubuntu security hardening checklists I use before go-live.
How do you configure NOPASSWD sudo without creating a security hole?
NOPASSWD skips password prompts. Automation loves it. Attackers love it too if the rule is too broad. The safe rule is simple: one absolute path, one purpose, no shells.
Bad example — never do this:
deploy ALL=(ALL) NOPASSWD: ALL That is root without a password. Anyone who compromises the deploy SSH key owns the server. I have seen this copied from outdated Stack Overflow answers on otherwise well-hardened Laravel hosts.
Better example for a PHP-FPM reload after Deployer symlink swap:
deploy ALL=(root) NOPASSWD: /bin/systemctl reload php8.3-fpm.service Test the exact command string with:
sudo -l -U deploy Official syntax and option reference live in the sudoers manual page. Ubuntu packages the same policy grammar across 22.04 and 24.04 LTS.
Running commands as another user through sudo
Deploy scripts often need to run Artisan or console tasks as www-data:
sudo -u www-data php /var/www/current/artisan migrate --force Grant that explicitly if needed:
deploy ALL=(www-data) NOPASSWD: /usr/bin/php /var/www/current/artisan * Wildcards in command arguments are risky. Prefer wrapping approved operations in a root-owned script at a fixed path, then sudo only that script. Document the wrapper in your Symfony or Laravel deployment runbook so the next developer knows the boundary.
Password quality when sudo does ask
Human admins should use strong, unique passwords for their user account. Sudo reuses that password by default. Generate one with a local password generator tool, store it in a team vault, and rotate when staff change. Pair password policy with SSH key-only auth and UFW firewall rules on public interfaces.
What are common Ubuntu sudo permission errors and how do you fix them?
Most failures fall into a short list. Work through them in order before you assume corruption.
"User is not in the sudoers file"
The user lacks a matching rule. Fix from an existing root session or console:
sudo usermod -aG sudo username Log out and back in so group membership refreshes. Group changes do not apply to an already-open SSH session.
"Sorry, user is not allowed to execute"
Sudo found a rule but the command path does not match. Sudo requires exact paths unless you used a deliberate wildcard. systemctl might resolve to /bin/systemctl or /usr/bin/systemctl depending on the image. Run which systemctl as root and copy that path into sudoers.
visudo shows a parse error after edit
visudo refuses to save broken syntax. Read the line number it reports. Common mistakes include missing colons, tabs versus spaces in the wrong place, and unescaped special characters in usernames.
Locked out after a bad sudoers edit
If you still have root console access through your VPS provider, boot to recovery or open the provider's web console. Mount the disk read-write, then run visudo from recovery or rename the bad drop-in file under /etc/sudoers.d/.
Prevention beats recovery. Always keep one provider-level console path available. On AWS EC2 instances hosting projects like Adventure Third Pole Trek, I document the serial console steps before touching sudoers on production.
Useful diagnostic commands
# List effective rules for current user
sudo -l
# List rules for another user (requires root)
sudo -l -U deploy
# Validate sudoers syntax without editing
sudo visudo -c
# Check drop-in permissions
ls -la /etc/sudoers.d/
# Watch sudo attempts live
sudo tail -f /var/log/auth.log | grep sudo Cross-check related permission layers in the Linux file permissions and ACLs guide when sudo succeeds but the command still fails with "Permission denied". That usually means filesystem mode bits, not sudo policy.
How should you harden sudo on Ubuntu web servers?
Sudo hardening belongs in the same pass as SSH, firewall, and fail2ban configuration. Treat it as part of baseline server build, not a post-launch patch.
- Disable root SSH login in
/etc/ssh/sshd_config:PermitRootLogin no - Limit full sudo to named individuals, not shared "devops" accounts
- Prefer
/etc/sudoers.d/drop-ins over editing the main file - Enable
Defaults logfileor ship auth.log to central monitoring - Install
fail2banjails for repeated sudo auth failures — see the fail2ban configuration guide - Remove sudo from users who no longer need it:
sudo deluser username sudo - Audit quarterly with
getent group sudoand compare to your team roster
Canonical documents recovery and default policy in the Ubuntu Server sudo documentation. Debian's wiki explains historical admin versus sudo group behaviour on derivatives — useful if you migrate older images.
On stacks running PHP 8.3 or 8.4 with Apache or Nginx, sudo often touches service reloads, certificate renewal hooks, and cron wrappers. Align sudo rules with your PHP installation layout and scheduled tasks from the cron jobs guide. After Nginx installation, confirm whether reload is systemctl reload nginx or a distribution-specific unit name before you write NOPASSWD lines.
If this work sits outside your team's daily scope, Linux system administration support covers sudo policy design, deploy user hardening, and ongoing server maintenance for production apps. I apply the same patterns across Notary Kathmandu and other Deployer-managed sister sites on shared infrastructure.
Broader context lives in server hardening for Ubuntu web servers, Ubuntu server security best practices, and essential terminal commands for day-to-day ops. For operator identity hygiene, read about my infrastructure work and client feedback on customer reviews.
Key Takeaways
- Edit sudo policy only with
visudoorvisudo -f /etc/sudoers.d/name— never with a plain editor on a live box. - Grant full sudo sparingly via the
sudogroup; give CI and deploy users narrow NOPASSWD rules with absolute binary paths. - Drop-in files under
/etc/sudoers.d/must be mode0440, root-owned, and dot-free in the filename. - Run
sudo -landsudo visudo -cbefore you close an SSH session after any sudoers change. - Combine sudo hardening with SSH key policy, UFW, fail2ban, and regular auth.log review on every public server.
- Filesystem "Permission denied" after a successful sudo usually means file modes or ownership — not a missing sudo rule.
People Also Ask
What is the difference between the sudo group and the admin group on Ubuntu?
On current Ubuntu server releases, sudo is the standard group for full administrative rights. Older desktop installs mapped admin to the same privilege through an Admin alias. New deployments should use sudo exclusively so group audits stay obvious.
Can you give sudo access without sharing the root password?
Yes. That is the default Ubuntu model. Root has no enabled password. You add users to sudo or write user-specific sudoers rules. Each person authenticates with their own password unless a rule includes NOPASSWD.
How long does sudo remember your password?
By default sudo caches credentials for 15 minutes. The timestamp_timeout option in sudoers changes that value. Set timestamp_timeout=0 to require a password on every invocation, which is stricter for shared admin workstations.
Is NOPASSWD safe for automated deployments?
It can be safe when the rule allows one fixed command path and the account uses SSH keys with limited scope. It is unsafe when it grants blanket ALL commands. Wrap multi-step deploy logic in a root-owned script and sudo that script alone.
Build sudo policy that survives real operations
A correct Ubuntu Sudo Permissions Guide implementation is not about memorising syntax. It is about drawing small, auditable boundaries around humans and automation on boxes that run real revenue workloads. Start with tiered access, test with sudo -l, keep provider console recovery handy, and review auth logs after every personnel change.
Need sudo policy reviewed on an Ubuntu web stack, or locked out after a bad edit? Contact us for hands-on Linux administration, or explore the full services list and related guides on the blog.
Frequently Asked Questions
0 Comments
Leave a comment
Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

