SSH & ACCESS
Jun 23/2026

How to Create and Manage Sudo Users on a Linux Server

7 min read
Author
CloudStick Team
Backend Developer
Share this article
How to Create and Manage Sudo Users on a Linux Server
CloudStick
Sudo Users on Linux

Why Sudo Access Matters on a Production Server

Running everything as root is the fastest path to an irreversible mistake. A single mistyped command — rm -rf / instead of rm -rf ./ — can destroy an entire server. The sudo model is the solution: users operate with normal privileges by default and escalate only when a specific command demands it, with every escalation logged.

This separation of privileges also matters for teams. When a developer needs to restart Nginx but shouldn't have full root access, sudo lets you grant exactly that — nothing more. On Ubuntu 24.04, membership in the sudo group grants full administrative access; the sudoers file lets you lock down access to specific binaries per user.

PREREQUISITE
You need SSH access to your server as the root user or an existing sudo user. Ubuntu 24.04 is assumed throughout this guide. The commands also work on Debian 12 and later.

Adding a New Sudo User with adduser and usermod

There are two steps: create the user account, then add it to the sudo group. The adduser command creates a home directory and prompts for a password. usermod -aG sudo appends the user to the sudo group without removing them from other groups.

# Create a new user account
sudo adduser deploy
# Grant sudo privileges
sudo usermod -aG sudo deploy
# Verify the user is in the sudo group
groups deploy
# Expected: deploy : deploy sudo
# Switch to the new user and test sudo access
su - deploy
sudo whoami
# Expected: root

The group change takes effect immediately for new SSH sessions. If the user is already logged in, they need to log out and back in for the sudo group membership to be recognized.

Editing the Sudoers File Safely with visudo

Never edit /etc/sudoers directly with a text editor. A syntax error in that file can lock you out of sudo entirely. The visudo command opens the file in your editor and validates the syntax before saving — it will refuse to write a broken configuration.

# Open the sudoers file safely
sudo visudo
# The sudoers file uses this syntax:
# user ALL=(ALL:ALL) ALL
# user hosts=(run_as_user:run_as_group) commands
# Add a drop-in file (preferred — keeps sudoers clean):
sudo visudo -f /etc/sudoers.d/deploy
# Full sudo access for deploy user:
deploy ALL=(ALL:ALL) ALL
# Sudo without password prompt (CI/CD use only):
deploy ALL=(ALL) NOPASSWD: ALL

Prefer drop-in files under /etc/sudoers.d/ over editing the main sudoers file. They are included automatically and are easier to manage and audit per user or service.

Restricting Sudo to Specific Commands

Full sudo access is rarely necessary. A deployment user should be able to restart services but never modify system files. A database admin needs access to MySQL but not to the network configuration. The sudoers syntax supports per-command restrictions with full path specification.

# Allow deploy user to restart nginx only
deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx
# Allow multiple commands (comma-separated)
deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx, \
/usr/bin/systemctl restart php8.3-fpm
# Use command aliases for cleaner sudoers files
Cmnd_Alias WEBSERVICES = /usr/bin/systemctl restart nginx, \
/usr/bin/systemctl restart php8.3-fpm, \
/usr/bin/systemctl reload nginx
deploy ALL=(ALL) NOPASSWD: WEBSERVICES
TIP
Always use the full path in sudoers rules — /usr/bin/systemctl not systemctl. Without a full path, a user could create a malicious script named systemctl in their PATH and escalate privileges.

Auditing Sudo Usage and Access

Every sudo command is logged to /var/log/auth.log on Ubuntu. Each entry records the username, the terminal, the effective user, and the exact command run. This audit trail is your first line of defense when investigating an incident.

# View all sudo activity (last 50 lines)
sudo tail -n 50 /var/log/auth.log | grep sudo
# Filter by a specific user
grep 'sudo' /var/log/auth.log | grep 'deploy'
# Show all current sudo privileges for a user
sudo -l -U deploy
# List all users in the sudo group
getent group sudo
# Revoke sudo access (remove from sudo group)
sudo deluser deploy sudo

For persistent audit logging, consider forwarding auth.log to a centralized log aggregator. Once a log file is deleted on a compromised server, the audit trail is gone — remote logging preserves it.

"The principle of least privilege means every user and process should have only the minimum permissions required to perform their function — nothing more, nothing less."

Managing Server Access with CloudStick Teams

Managing sudo rules manually across multiple servers is tedious and error-prone. CloudStick's Teams feature solves this at scale. You add team members to a server through the dashboard, assign them a role (Admin, Developer, or Viewer), and CloudStick provisions the appropriate SSH key and system user automatically — no manual adduser commands required.

When you remove a team member from a server in CloudStick, their SSH key is revoked and their system access is removed immediately across all servers in the workspace. For agencies managing dozens of client servers, this centralized access management eliminates the risk of orphaned user accounts from former contractors or employees.

Manual sudo management remains essential knowledge for edge cases and direct server access. But for day-to-day team operations, CloudStick's access control layer removes the risk of configuration drift across your server fleet.

Leave a comment
Full Name
Email Address
Message
On this page

We use cookies to improve your experience

CloudStick uses cookies to personalise content, analyse traffic and keep you signed in. Cookie Policy · Terms of Service

Manage cookies