On this pageWhat sudo actually checks
A fresh server usually hands you one account: root. Working as root all day is fast until a typo runs with unlimited rights. The fix takes five minutes: create your own account, give it administrator rights through sudo, and learn the handful of commands that switch between identities. Examples below use Debian 13 and Ubuntu 26.04 LTS, with the RHEL-family difference noted where it matters.
What sudo actually checks
Every account has a numeric user ID. Root is UID 0, people get IDs from 1000 up, and services such as www-data or postgres use low IDs with a login shell of /usr/sbin/nologin. When you type sudo, the program reads /etc/sudoers and every file it includes, finds a rule that matches your user or one of your groups, asks for your password and then runs the command as root.
On Debian and Ubuntu that rule ships ready-made for one group:
%sudo ALL=(ALL:ALL) ALL
| Family | Admin group | Typical state after install |
|---|---|---|
| Debian | sudo |
depends on the installer choice (see below) |
| Ubuntu | sudo |
first user is already a member, root has no password |
| AlmaLinux, Rocky Linux, Fedora | wheel |
first user is a member if marked as administrator |
Create the account
adduser is the friendly Debian and Ubuntu tool: it creates the home directory, copies the skeleton files and asks for a password. useradd exists everywhere but needs flags to do the same job.
# Debian and Ubuntu
adduser alice
# Any distribution
useradd -m -s /bin/bash alice
passwd alice
If you log in with SSH keys, give the new account a copy of root's key before you need it. install creates the directory and the file with the right owner and mode in one step:
install -d -m 700 -o alice -g alice /home/alice/.ssh
install -m 600 -o alice -g alice /root/.ssh/authorized_keys /home/alice/.ssh/authorized_keys
Grant sudo rights
Through the admin group
This is the right choice for a person who administers the whole machine:
usermod -aG sudo alice # Debian and Ubuntu
usermod -aG wheel alice # RHEL family
id alice # confirm the group is listed
Never drop the -a. Without it, usermod -G replaces every supplementary group the user had with the one you named.
Through a file in /etc/sudoers.d
A drop-in file keeps your rule separate from the distribution's /etc/sudoers, so package upgrades never touch it. Always edit with visudo, which refuses to save a file with a syntax error. A broken sudoers file can lock every user out of sudo.
visudo -f /etc/sudoers.d/alice
alice ALL=(ALL:ALL) ALL
Two rules about file names: sudo skips files in /etc/sudoers.d whose names contain a dot or end in ~, so alice.conf is silently ignored. visudo creates the file with mode 0440; keep it that way.
Narrow rights for automation
A deploy script rarely needs a root shell. Give it exactly the commands it runs, with full paths:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service, /usr/bin/systemctl status myapp.service
Arguments written in a rule are part of the match, so this account can restart one unit and nothing else. Check what a rule really grants and whether the whole configuration still parses:
sudo -l -U deploy
sudo visudo -c
When Debian has no sudo at all
The Debian installer asks for a root password. If you set one, the first user is not added to the sudo group, and a minimal install may not even include the sudo package. Leave it empty and the installer disables root and gives the first user sudo rights instead. To fix the first case after the fact:
su -
apt install sudo
usermod -aG sudo alice
exit
Group membership is read at login. Until alice logs out and back in, sudo keeps answering "alice is not in the sudoers file". Open a new SSH session, or run newgrp sudo for a quick test in the current one.
Switch users from the command line
Each switching command differs in whose password it wants and which environment you end up with. A login shell (the - or -i forms) loads the target user's profile, home directory and PATH, which avoids most "command not found" surprises.
| Command | Password asked | You get | Use it for |
|---|---|---|---|
su - |
root's | root login shell | systems where root has a password |
su - alice |
alice's | alice's login shell | switching between two people's accounts |
sudo -i |
yours | root login shell | admin work on Ubuntu, where root has none |
sudo -s |
yours | root shell, your environment | quick root tasks in the current directory |
sudo -iu alice |
yours | alice's login shell | acting as another user without their password |
sudo -u postgres psql |
yours | one command as that user | service accounts |
Service accounts with a nologin shell refuse su, by design. Name the program you want instead:
sudo -u www-data bash # a shell as the web server user
sudo -u www-data php -v # or a single command
whoami prints the effective user and id adds the groups. exit takes you back to the previous identity. systemd 256 and later also include run0, which gets root rights through polkit rather than a setuid binary; sudo remains the tool most guides assume.
Switching users on the desktop
On a Linux desktop you can keep one session open and start another. In GNOME, open the system menu at the top right, choose the power button and pick Switch User, which appears once a second account exists. KDE Plasma offers Switch User in the application launcher's session actions. Linux Mint and other LightDM desktops can do it from a terminal with dm-tool switch-to-greeter.
What changes with sudo-rs on Ubuntu 26.04
Ubuntu 26.04 LTS makes sudo-rs, a Rust rewrite, the default sudo. It reads the same /etc/sudoers and /etc/sudoers.d files, and everyday rules like the ones above behave the same. The original implementation is still available as sudo.ws, and the sudo-ldap package is gone in favor of LDAP through PAM. If an old, exotic sudoers option stops working after an upgrade, test the rule with sudo.ws before rewriting it.
Lock down root once your account works
Prove the new account first. Keep the root session open, start a second SSH session as alice, and run sudo -i. Only then disable root logins:
echo 'PermitRootLogin no' | sudo tee /etc/ssh/sshd_config.d/10-no-root.conf
sudo sshd -t && sudo systemctl reload ssh # the unit is sshd on the RHEL family
sudo passwd -l root # lock the root password, sudo keeps working
A new CloudSat Linux server starts with root SSH access, using the key you chose at deploy time or the password from the console (find or reset the root password), so this is a good first job after logging in. If you lock yourself out anyway, the console's recovery tools give you a keyboard on the server again.
FAQ
Why does sudo still refuse me after I joined the group?
Your current session was created before the change, so it carries the old group list. Log out and back in, then check with id. If the group appears and sudo still refuses, look for a typo with sudo visudo -c from another admin account or a root session.
Is NOPASSWD a bad idea?
For people, usually yes: the password prompt is a last chance to notice a wrong command, and it protects you if someone reaches your logged-in session. For automation accounts, a password-free rule limited to exact commands is a reasonable trade.
How do I take sudo away or remove a user?
gpasswd -d alice sudo removes the group membership, and deleting the file in /etc/sudoers.d removes a drop-in rule. To delete the account itself, run deluser --remove-home alice on Debian and Ubuntu, or userdel -r alice elsewhere, after checking with ps -u alice that nothing still runs under that name.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




