🚀 TL;DR: Quick Commands to Add a User to Sudoers

Need to give a user sudo privileges? Just copy and paste one of these commands based on your Linux distribution.

✅ For Ubuntu/Debian

sudo usermod -aG sudo username

✅ For CentOS/RHEL

sudo usermod -aG wheel username

✅ Manually Edit the Sudoers File (Universal)

sudo visudo

Add this line at the end:

username ALL=(ALL:ALL) ALL

💡 Done! The user can now run commands with sudo.
🔧 To check: Log in as the user and run sudo whoami. If it prints root, it's working!

🐢And now the long version

** What is a Superuser in Linux?**

Linux has a multi-user architecture where different users have different levels of permissions. The superuser (root) has unrestricted access to all system files and commands.

  • 🛠 Superuser (Root) → Has full control over the system (can install software, modify system files, change user permissions).
  • 👥 Regular Users → Have limited access, usually restricted to their own files and directories.

By default, only the superuser (root) can make critical system changes. Regular users cannot modify system files or install software unless explicitly given permission.


** Why Not Always Use Root?**

Using the root user for daily tasks is dangerous because:

  1. Security Risks → If a hacker gains access to a root account, they can completely control the system.
  2. Accidental Mistakes → Running a destructive command (rm -rf /) as root can wipe the entire OS.
  3. Process Isolation → Applications running as root have unrestricted access, which increases the risk of malware or misconfiguration.

🔒 Solution?Use sudo instead of logging in as root!


** What is sudo and Why is it Safer?**

sudo (Superuser DO) allows a regular user to run commands with superuser privileges, but only when necessary.

Why is sudo better than root?

  • 🛡 Access Control → Admins can define who can use sudo and for which commands.
  • Temporary Elevation → Users only get superuser access for one command at a time (reduces risk).
  • 📜 Audit Logging → Every sudo command is logged, so admins can track activity.

Example:
Instead of logging in as root (su -), you can run one command with sudo:

sudo apt update  # Updates system packages
sudo systemctl restart nginx  # Restarts a service

This way, you stay a regular user and only elevate privileges when required.

Let’s apply everything we’ve learned....

1️⃣ Log in as a Root User or a Sudo User

Ensure you have root access or an existing sudo user.

su -  # Switch to root
# OR
sudo -i  # Open a root session

2️⃣ Add the User to the sudo Group

On most Linux distributions, users in the sudo group have administrative privileges.

usermod -aG sudo username

Replace username with the desired user's name.


3️⃣ Verify the User Has Sudo Access

Switch to the user and test their sudo privileges:

su - username
sudo whoami

If successful, it will return root.


4️⃣ Optional: Edit the Sudoers File

For advanced permissions, edit the sudoers file safely with visudo:

sudo visudo

Add a specific rule, e.g., to allow a user to run all commands:

username ALL=(ALL:ALL) ALL

Save and exit (Ctrl+O, Enter, Ctrl+X).


Now your user can run administrative commands securely!

Disclaimer: At MojaLab, we aim to provide accurate and useful content, but hey, we’re human (well, mostly—some of this content is generated with the help of an AI). We carefully review and refine our articles to ensure quality, but if you spot an error, have questions, or think something could be improved, let us know—we’d love to hear from you. Always test tutorials and tips in a safe environment before applying them in production. Happy learning! 🚀