Linux Users, Groups and sudo: useradd, usermod, passwd and visudo

Linux 9 min readUpdated Nov 18, 2026
2 views0 comments
A group of people working around a table with laptops and notebooks
Photo: Startup Stock Photos / StockSnap (CC0, public domain). source

Every process on a Linux system runs as a user, and every permission check starts with “who is asking?”. That makes user and group management one of the first real administration skills. This guide shows how Linux stores accounts, how to create and change users with useradd and usermod, how groups work, and how to give someone administrator rights safely with sudo and visudo. Examples use the Red Hat family (RHEL 9, AlmaLinux 9, Rocky Linux 9); differences for Debian and Ubuntu are noted where they matter.

Where Linux keeps accounts

Accounts are plain text files, so you can read them with ordinary tools. The three that matter:

File Holds Readable by
/etc/passwd One line per user: name, UID, primary group ID, comment, home directory, login shell Everyone
/etc/shadow Password hashes and password-aging settings Root only
/etc/group One line per group: name, GID, member list Everyone

A line in /etc/passwd has seven fields separated by colons:

$ getent passwd alice
alice:x:1001:1001:Alice Smith:/home/alice:/bin/bash
  • alice: the login name.
  • x: the password is stored in /etc/shadow.
  • 1001: the user ID (UID). 1001 again is the primary group ID (GID).
  • Alice Smith: a free-text comment.
  • /home/alice: the home directory. /bin/bash: the login shell.

Prefer getent passwd name over reading the file directly: it also finds accounts that come from a directory service such as LDAP or Active Directory.

User IDs follow a convention. UID 0 is root. On RHEL-family systems UIDs 1 to 999 are for system and service accounts, and normal people start at 1000. Debian and Ubuntu use the same split.

Who am I, and who else is here?

whoami              # your login name
id                  # your UID, GID and all groups
id alice            # the same for another user
groups              # just the group names
who                 # who is logged in now
w                   # who is logged in and what they run
last -n 5           # the last five logins
lastlog -u alice    # when alice last logged in
$ id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),10(wheel)

Creating users with useradd

sudo useradd -m -s /bin/bash -c "Alice Smith" alice
sudo passwd alice
  • -m creates the home directory and copies the starter files from /etc/skel. RHEL does this by default, but Debian’s raw useradd does not, so keep -m in your habit.
  • -s sets the login shell and -c the comment.
  • By default Red Hat systems also create a private group with the same name as the user (the “user private group” scheme), which is why alice has a group called alice.
  • passwd alice asks you to type a new password twice. A new account stays locked for password logins until you set one.

Useful extras:

sudo useradd -m -G wheel,developers alice          # add to extra groups at creation
sudo useradd -m -u 1500 bob                        # choose a specific UID
sudo useradd -m -e 2026-12-31 contractor           # account expires on a date
sudo useradd -r -s /sbin/nologin -d /var/lib/myapp myapp   # a service account (no login)

A service account (-r for system account, shell /sbin/nologin) is a user that exists only to own files and run one program. Never let a service run as root when it can run as its own account.

Debian and Ubuntu also offer a friendlier adduser command. It is not installed by default on RHEL systems, so scripts for the Red Hat family should use useradd.

Changing and removing users with usermod and userdel

sudo usermod -c "Alice S." alice            # change the comment
sudo usermod -s /bin/zsh alice              # change the shell
sudo usermod -L alice                       # lock the password
sudo usermod -U alice                       # unlock it again
sudo usermod -e 2026-12-31 alice            # set an expiry date
sudo usermod -aG developers alice           # add to a group (note the -a)
sudo userdel alice                          # delete the account, keep the files
sudo userdel -r alice                       # delete the account and the home directory

The most expensive typo in user management: usermod -G developers alice without -a replaces all of alice’s supplementary groups with just developers. If she was in wheel, she just lost her administrator rights. Always write -aG (append to Groups).

Before userdel -r, check whether the user owns files elsewhere or runs processes. sudo find / -xdev -user alice 2>/dev/null | head lists what she owns. A locked account (usermod -L) is often safer than deleting while you decide what to keep.

Passwords and password aging

The chage command shows and sets password policy per account:

$ sudo chage -l alice
Last password change                                    : Nov 17, 2026
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

sudo chage -M 90 -W 7 alice     # must change every 90 days, warn 7 days before
sudo chage -d 0 alice           # force a new password at the next login

passwd -l alice locks the password and passwd -u alice unlocks it. Locking the password does not stop an SSH key login, so to block an account completely, also expire it (usermod -e 1 alice) or lock it with both.

Working with groups

Groups let you give the same access to several users at once. Every user has one primary group (the group new files get) and any number of supplementary groups.

sudo groupadd developers             # create a group
sudo usermod -aG developers alice    # add a member
sudo gpasswd -d alice developers     # remove a member
sudo groupdel developers             # delete the group
getent group developers              # show the members
id alice                             # confirm

Group changes apply at the next login. If you add yourself to a group and it does not seem to work, log out and back in. newgrp developers starts a new shell with that group active right away, which is handy for a quick test.

Groups become useful together with file permissions. A shared folder owned by the developers group with mode 2775 lets the whole team work in it. See our file permissions guide for that recipe.

Becoming root safely: su and sudo

su switches to another user and needs that user’s password. sudo runs one command as another user (root by default) and asks for your own password. Modern practice prefers sudo: you keep a log of who did what, you never share the root password, and you can limit what each person may run.

sudo dnf update                 # one command as root
sudo -u postgres psql           # one command as another user
sudo -i                         # an interactive root shell with root's environment
sudo -l                         # list what you are allowed to run
su - alice                      # switch user (the dash gives alice's full environment)

Who gets sudo? The wheel group

On the Red Hat family, members of the group wheel may use sudo. On Debian and Ubuntu the group is called sudo. Giving someone administrator access is one command:

sudo usermod -aG wheel alice     # RHEL, AlmaLinux, Rocky, Fedora
sudo usermod -aG sudo alice      # Debian, Ubuntu

This works because /etc/sudoers contains the line %wheel ALL=(ALL) ALL, which reads: “members of group wheel, on any host, may run commands as any user, any command”.

Editing sudo rules with visudo

Never edit the sudoers file with a normal editor. A syntax error there can lock every administrator out of sudo. visudo opens it and checks the syntax before saving. Better still, leave the main file alone and add small files in /etc/sudoers.d/:

sudo visudo -f /etc/sudoers.d/deploy

Example rules (the format is who where = (as whom) what):

# bob may restart only the web server, with his own password
bob ALL=(root) /usr/bin/systemctl restart httpd

# the group "dbadmins" may run commands as the postgres user
%dbadmins ALL=(postgres) ALL

# a backup account may run one script without a password prompt
backup ALL=(root) NOPASSWD: /usr/local/bin/run-backup.sh

Good habits:

  • Use full paths to commands, and allow the exact command, not ALL, whenever you can.
  • NOPASSWD is convenient for automation, but anyone who controls that account or script controls root. Keep it narrow.
  • Files in /etc/sudoers.d/ should be mode 0440 and owned by root. Check any file with sudo visudo -c -f /etc/sudoers.d/deploy.
  • Do not give sudo to a command that can open a shell (editors, less, vi, find, tar with some options). The user can escape to a root shell from them.

Where sudo activity is logged

Each sudo use is logged with the user, the working directory and the command. On RHEL-family systems read it with:

sudo journalctl _COMM=sudo --since today
sudo grep sudo /var/log/secure | tail

On Debian and Ubuntu the file is /var/log/auth.log. Failed attempts appear here too, which makes it a good first place to look when a server shows unexpected activity.

Securing the accounts

  • Do not log in as root over SSH. Use a normal user with sudo, and turn on key-based login. Our SSH key login guide shows how, including how to avoid locking yourself out.
  • Give each person their own account. Shared accounts make logs useless.
  • Remove or lock accounts when people leave. List accounts that can log in with grep -v nologin /etc/passwd and look for names you do not recognise.
  • Set a password policy (chage) for accounts that use passwords, and prefer SSH keys for administrators.

Quick reference

Task Command
Create a user with a home directory sudo useradd -m -s /bin/bash alice
Set or change a password sudo passwd alice
Add a user to a group (safely) sudo usermod -aG groupname alice
Give administrator rights (RHEL) sudo usermod -aG wheel alice
Lock or unlock an account sudo usermod -L alice / -U alice
Delete a user and the home directory sudo userdel -r alice
See groups and IDs id alice
Edit sudo rules safely sudo visudo -f /etc/sudoers.d/name
See your sudo rights sudo -l

For the related command lists, see the Linux commands guide for Red Hat systems.

Frequently asked questions

What is the difference between su and sudo?

su switches to another account and needs that account’s password. sudo runs one command with higher rights, uses your own password, can be limited to specific commands, and writes a log entry. Most administrators use sudo.

Why did usermod remove my user from the wheel group?

You most likely ran usermod -G without -a. Without append, the list you give replaces all supplementary groups. Re-add the group with usermod -aG wheel name and always use -aG.

How do I give a user sudo access on AlmaLinux or RHEL?

Add the user to the wheel group with sudo usermod -aG wheel username, then have them log out and back in. On Ubuntu the group is called sudo.

How do I check which users can log in?

Run grep -v -E 'nologin|false' /etc/passwd. Accounts whose shell is /sbin/nologin or /bin/false cannot log in interactively.

Is it safe to delete a user with userdel -r?

It permanently removes the home directory. Back up anything you need first, and check for files the user owns outside the home directory, because those are not removed and will show a bare numeric owner afterwards.

Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top