Linux users, groups and sudo explained (adduser, usermod, visudo)
Linux identifies every account by a user ID and group IDs stored in /etc/passwd and /etc/group, and sudo lets chosen users run commands as root according to rules in /etc/sudoers.
- Updated Nov 27, 2026
- Markdown
Inspect accounts, add a user to a group, grant limited sudo with visudo, and create a no-login service account for an app.
- An Ubuntu or Debian server you can reach over SSH
- A user with sudo access
Every file, process and login on Linux belongs to a user and a group, and sudo is how a normal user does an admin task. Getting this wrong has two failure modes: people who cannot do their job because a group is missing, and accounts with far more power than they need. This lesson goes deeper than create a sudo user and shows how the pieces fit.
How it works#
An account is a line in /etc/passwd. Each line has seven colon-separated fields:
deploy:x:1001:1001:Deploy User:/home/deploy:/bin/bashThese are: name, a placeholder for the password (x means the hash is in /etc/shadow), UID, primary GID, a comment, the home directory and the login shell. /etc/group lists groups the same way:
sudo:x:27:alice,deploy
docker:x:999:deployThe fields are the group name, a password placeholder, the GID and a comma-separated member list. Permissions are checked against your UID and the GIDs of your groups, which is the basis of file permissions. UID 0 is root. On Debian and Ubuntu, normal users start at 1000, and system accounts use lower numbers.
sudo runs a command as another user, root by default, if the rules in /etc/sudoers and /etc/sudoers.d/ allow it. On Ubuntu, members of the sudo group may run anything.
Step 1: Inspect accounts#
id
id deploy
groups
getent passwd deploy
getent group sudo
cut -d: -f1 /etc/passwdid prints your UID, GID and all groups. groups lists only group names. getent queries the account databases, including network ones, and is the right tool for scripts. cut -d: -f1 lists all account names. Never read hashes: /etc/shadow is root-only, and you should not need it.
Step 2: Create users and groups#
sudo adduser alice
sudo groupadd appteam
sudo usermod -aG appteam aliceadduser is the friendly Debian wrapper. It asks for a password and creates the home directory. useradd is the low-level tool and does not create a home unless you pass -m, so prefer adduser for people. groupadd makes a group.
usermod -aG appteam alice adds Alice to a supplementary group. The -a is the one that matters. Without it, -G replaces her whole group list, and if you do that to an admin you can remove them from sudo.
Group changes apply at login. After adding yourself to a group, log out and back in, or start a new SSH session, then check with id. As a quick test in the current shell, run newgrp appteam.
To remove a user from a group, use sudo gpasswd -d alice appteam. To delete an account but keep its files, sudo deluser alice. Adding --remove-home deletes the home folder, which is destructive, so back it up first.
Step 3: Grant sudo#
The simple route on Ubuntu:
sudo usermod -aG sudo alice
sudo -l -U alicesudo -l lists what the current user may run, and -U alice shows it for another user. For a narrower rule, use a drop-in file and always edit with visudo:
sudo visudo -f /etc/sudoers.d/deployPut this in the file:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp, /usr/bin/systemctl status myappThis lets deploy restart one service as root without a password, and nothing else. Use full paths. visudo checks the syntax on save and refuses a broken file. Files in /etc/sudoers.d/ must be mode 0440 and must not contain a dot or end with ~ in the file name, or they are ignored. visudo -f sets the mode correctly. Check the whole configuration with sudo visudo -c.
Be careful with NOPASSWD: ALL and with commands that can spawn a shell, such as editors, less or find. They turn a narrow rule into full root. Keep a second root-capable session open while you test.
Step 4: sudo -i, sudo -s and su#
sudo -i
sudo -s
sudo -u postgres psqlsudo -i runs a login shell as root, with root’s home directory and profile. sudo -s runs a shell as root but keeps more of your environment and your current directory. sudo -u postgres psql runs one command as another user, which is the safer habit: act as root only for the single command that needs it. Exit with exit. Avoid leaving a root shell open, since every typo then has root power. See Linux environment variables and the shell profile for why the two shells behave differently.
Step 5: Service accounts#
A service should not run as root or as a person. Create a dedicated account with no login:
sudo adduser --system --group --home /var/lib/myapp --shell /usr/sbin/nologin myapp--system picks a low UID and creates no password. --group creates a matching group. --home sets the data folder and --shell /usr/sbin/nologin blocks interactive logins. Then give the service its folders and run the unit under that user:
sudo chown -R myapp:myapp /var/lib/myapp[Service]
User=myapp
Group=myappIf the app is compromised, the attacker gets only what myapp can touch. The unit file details are in run a service with systemd.
Check it worked#
id alice
sudo -l -U alice
getent passwd myapp
sudo -u myapp idAfter logging out and in, id alice lists the new group. getent passwd myapp should show the nologin shell and a UID below 1000. sudo -u myapp id proves you can act as that account.
Edge cases and gotchas#
- Group changes need a new login. A running shell keeps its old groups. Reconnect or use
newgrp. - docker group equals root. Anyone in
dockercan mount/into a container, so treat membership as root access. - A broken sudoers locks you out. Edit only with
visudo. Keep a root console or second session available. - Do not delete the only sudo user. Confirm another admin works before
deluser. - UIDs matter across servers and containers. Files on a bind mount are owned by number, so UID 1000 on the host may differ from the container.
- Root login over SSH. Prefer a sudo user and disable direct root login, see disable SSH password login.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
alice is not in the sudoers file. This incident will be reported. | The user is not in sudo or sudoers | sudo usermod -aG sudo alice from an admin account |
usermod: group 'docker' does not exist | The group is not created yet | Install Docker or run sudo groupadd docker |
id does not show the new group | Old session | Log out and in, or run newgrp name |
sudo: unable to resolve host server1: Name or service not known | Hostname missing from /etc/hosts | Add the hostname to 127.0.1.1 in /etc/hosts |
>>> /etc/sudoers.d/deploy: syntax error near line 1 <<< | Typo in the rule | Choose e to edit again in visudo, and fix the line |
sudo: /etc/sudoers.d/deploy is world writable | Wrong mode on the drop-in | sudo chmod 0440 /etc/sudoers.d/deploy |
adduser: The user 'alice' already exists. | Name is in use | Pick another name or use usermod on the existing user |
This account is currently not available. | The account has the nologin shell | Expected for service accounts; use sudo -u name command |
Quick reference#
id; groups # who am I, which groups
getent passwd NAME # one account line
sudo adduser NAME # new person
sudo usermod -aG GROUP NAME # append to a group
sudo gpasswd -d NAME GROUP # remove from a group
sudo -l -U NAME # what sudo allows
sudo visudo -f /etc/sudoers.d/NAME # safe sudo rule
sudo -u NAME command # run as another user
sudo adduser --system --group --shell /usr/sbin/nologin NAMERelated lessons#
- Create a sudo user: the short version for a fresh server.
- Linux file permissions explained: how UID and GID decide access.
- Log in with SSH keys: give each user their own key.
- Fix permission denied on a Docker volume: what UID mismatch looks like.
For a service account in a real deploy, see deploy a Go service with Docker.
Next#
Accounts have their own environments. Continue with environment variables and the shell profile.
Check what you learned
Frequently asked questions
How do I add a user to a group in Linux?
Run sudo usermod -aG groupname username. The -a flag appends, and without it usermod replaces all the user's supplementary groups. The user must log out and in for it to apply.
How do I give a user sudo access on Ubuntu?
Add them to the sudo group with sudo usermod -aG sudo username. For finer control, create a file in /etc/sudoers.d/ with visudo -f.
What is the difference between sudo -i and sudo -s?
sudo -i starts a login shell as root, with root's environment and home directory. sudo -s starts a shell as root but keeps most of your current environment.
What is a system user in Linux?
A system user is an account for a service, not a person. Created with adduser --system, it gets a low UID, no password and usually no login shell.
Sources
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.