
Creating a sudo user allows a non-root user to perform administrative tasks with elevated privileges. Instead of logging in as root, the user authenticates with their password, and the system securely logs each command they run. This approach improves system security by limiting root access and providing better accountability. Most Linux distributions follow a similar process for adding sudo users, with slight group names or package requirements variations.
This article explains how to create a sudo user on major Linux distributions, verify their privileges, and apply optional security settings to control sudo behavior.
Before you begin, you need to:
In this section, you will create a new user and grant them sudo privileges. While the exact steps may vary slightly between distributions, most follow a similar process. Start by choosing the section that matches your Linux distribution.
On Red Hat-based systems, users gain sudo access by being added to the wheel group, which is predefined in the instance sudoers configuration. This group based approach helps centralize administrative permissions and improve security by controlling who can execute privileged commands.
Create a new user.
In the above command, the -m option ensures a home directory is created at /home/example_user. Replace example_user with your desired username.
Set a password for the user.
You'll be prompted to enter and confirm a secure password. The system stores this encrypted password in /etc/shadow, which is readable only by privileged users.
Add the user to the wheel group.
The above command grants sudo access by adding the user to the wheel group.
-a option appends the user to the group without removing them from any existing groups.-G flag specifies the group name.Open the /etc/sudoers file with visudo to verify that the wheel group has sudo privileges.
Unlike a regular text editor, visudo performs syntax validation before saving changes. This helps prevent configuration errors that could lock you out of the system.
Locate the following line and make sure it is uncommented.
The above directive allows users in the wheel group to run any command with sudo. Its components mean:
%wheel: Applies to the wheel group (% indicates a group).First ALL: Applies to all hosts.(ALL): Allows running commands as any user.Last ALL: Allows running any command.Always use visudo to edit the sudoers file. Editing it directly with a standard text editor can result in syntax errors that break sudo functionality.
Switch to the newly created user account.
The hyphen (-) ensures that the user’s full environment is loaded, including updated PATH variables.
Run the whoami command with sudo to verify administrative access.
Output:
The output root confirms that the configuration is correct and the user has sudo privileges.
After setting up a sudo user, verify that the account has administrative privileges. This section walks you through several tests to confirm proper sudo functionality.
Run these commands to ensure the user can perform administrative tasks.
View the root user's home directory.
This command displays hidden and regular files in /root, which only root or users with sudo privileges can access.
Update the server package lists.
On Debian-based systems (Ubuntu, Debian):
On Red Hat-based systems (RHEL, CentOS, Fedora):
On Arch Linux:
Package management commands require root privileges because they modify system files and packages. If these commands work without errors, your sudo setup is functioning.
Confirm that your sudo user can access sensitive files restricted to root.
The /etc/shadow file contains encrypted user passwords and is critical for system security. It's only readable by root or users with sudo privileges, so viewing it confirms your sudo privileges are working.
sudo maintains detailed logs of all activity, which is crucial for security auditing and accountability.
On Debian-based systems, run:
On Red Hat systems, run:
These logs record when users run sudo, who ran it, and which commands they executed. This audit trail is a key security advantage of using sudo instead of directly operating as the root user.
In this section, you will configure how and when sudo prompts for a password, allowing you to fine-tune the balance between security and convenience.
By default, sudo caches your credentials for 15 minutes. Follow the steps below to customize this timeout to suit your workflow.
Open the sudoers file using the visudo utility.
Add or modify the following line.
This example sets the timeout to 30 minutes. This value determines how long sudo remembers your authentication after a successful password entry.
Special values for timestamp_timeout:
0: Require the password for every sudo command.-1: Never prompt again during the session.5, 15, 30): Define the authentication timeout in minutes. Behind the scenes, sudo creates timestamp files in /var/run/sudo/ or /var/lib/sudo/ to track when users last authenticated.
For automation tasks or controlled environments, you can allow specific users or groups to execute sudo commands without being prompted for a password.
Open the sudoers file.
Add a rule for the user.
Or configure it for an entire group:
Enabling password-less sudo reduces security by eliminating authentication prompts. Use it only in secure environments with fully trusted users.
Following the principle of least privilege, restrict which commands users can execute with sudo.
Edit the sudoers file.
Define explicit command permissions.
This configuration allows the user to run only the specified commands with sudo. Use absolute paths, as sudo does not resolve commands through $PATH.
To locate a command's full path, run:
For example:
Output:
In multi-user environments, command aliases can simplify the privilege definitions.
Open the sudoers file.
Define command groups and assign them.
Command aliases let you group related commands under a single name, making the sudoers file more organized and maintainable as it grows.
You have successfully created and configured a sudo user across major Linux distributions. You granted administrative privileges by assigning the user to the appropriate group (wheel or sudo) and verified access through protected commands. You also reviewed ways to enforce secure privilege escalation by customizing the sudoers file, such as restricting commands, enabling password-less access, and setting timeout and retry limits. With these configurations, your system now has properly managed sudo access, enhancing usability and security.
0 Comments
Be the first to comment and share your perspective with the community.