Users, Groups and sudoLesson 1 of 4
Creating and managing local user accounts
Know what an account is on disk, then create, change, lock and remove users with useradd, usermod and userdel.
useraddusermoduserdelidgetent/etc/passwd/etc/shadow/etc/skel
After this lesson you can
- Read the seven fields of /etc/passwd and explain where the password and the group memberships live.
- Create users with a chosen UID, home, shell, comment and primary group, and create a service account that cannot log in.
- Modify, lock, unlock and delete accounts, and say what userdel does with the home directory.
The idea
A local account is three small database lines and a directory. Understanding where they live makes every user task predictable.
| File | Holds |
|---|---|
/etc/passwd | One line per account: name:x:UID:GID:comment:home:shell |
/etc/shadow | The password hash and aging fields, readable by root only |
/etc/group | Group names, GIDs and the members of each group |
/etc/skel | Template files copied into every new home directory |
/etc/login.defs, /etc/default/useradd | Defaults for UID ranges, home creation, aging and shell |
User IDs follow a convention. UID 0 is root. System accounts for services use low UIDs (on RHEL, below 1000). Real people start at 1000. A service account runs a daemon, has no password, and gets /sbin/nologin as its shell so nobody can open a session as it.
Every user has one primary group (the gid in passwd, and by default a private group with the same name as the user) and any number of supplementary groups. Files you create belong to your primary group.
Try it
getent passwd alex
alex:x:1000:1000:alex:/home/alex:/bin/bash
sudo useradd -u 1500 -c 'Lena Berg' lena
echo 'Redhat123!' | sudo passwd --stdin lena
id lena
Changing password for user lena.
passwd: all authentication tokens updated successfully.
uid=1500(lena) gid=1500(lena) groups=1500(lena)
On RHEL useradd creates the home directory by default (CREATE_HOME yes in /etc/login.defs) and a private group. The new home is filled from /etc/skel:
ls -A /home/lena
.bash_logout .bash_profile .bashrc
Service accounts
sudo useradd -r -s /sbin/nologin -d /srv/appsvc -m appsvc
id appsvc
sudo su - appsvc
uid=987(appsvc) gid=985(appsvc) groups=985(appsvc)
This account is currently not available.
Change, lock and remove
| Command | Effect |
|---|---|
usermod -s /bin/sh user | Change the login shell |
usermod -c 'Name' user | Change the comment |
usermod -e 2027-01-31 user | Account expires on that date |
usermod -L user / -U user | Lock / unlock the password |
usermod -l new old | Rename the login (the home directory does not move; add -d /home/new -m) |
userdel user | Delete the account; keeps the home directory |
userdel -r user | Delete the account and its home directory and mail spool |
sudo usermod -L lena
sudo grep '^lena:' /etc/shadow | cut -c1-20
lena:!$6$qN4k9...
Going further
useradd -D prints the defaults and can change them, such as the default shell. /etc/skel is yours to edit: files you add there appear in every future home directory. chsh -s lets a user change their own shell, but only to one listed in /etc/shells. last, lastlog and who show who logged in, and passwd -S user prints a one-line status.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
Decode one account line
Print the
/etc/passwdline foralexand name each of its seven fields. Then show the same account throughgetent, and say why that is the better habit.Check your result
The line looks like
alex:x:1000:1000:alex:/home/alex:/bin/bash: name, password placeholderx, UID, primary GID, comment, home, shell.getent passwd alexprints the same line and also finds users from LDAP or IdM.Hint
grep '^alex:' /etc/passwdandgetent passwd alex.Solution
grep '^alex:' /etc/passwd getent passwd alex - Practice
Create a normal user
Create the user
lenawith UID1500, commentLena Berg, and the default home and shell. Set her password toRedhat123!without a prompt. Show heridand confirm her home directory exists and holds the files from/etc/skel.Check your result
id lenashowsuid=1500(lena) gid=1500(lena).ls -A /home/lenashows.bash_logout,.bash_profileand.bashrc.Hint
useradd -u 1500 -c 'Lena Berg' lena, thenecho 'Redhat123!' | sudo passwd --stdin lena.Solution
sudo useradd -u 1500 -c 'Lena Berg' lena echo 'Redhat123!' | sudo passwd --stdin lena id lena ls -A /home/lena - Practice
A service account that cannot log in
Create a system account
appsvcwith no login shell, its home at/srv/appsvc(created), and a UID from the system range. Prove that logging in as it fails.Check your result
id appsvcshows a UID below 1000.sudo su - appsvcprintsThis account is currently not available.and returns.Hint
-rcreates a system account,-s /sbin/nologinremoves the login shell,-dand-mchoose and create the home.Solution
sudo useradd -r -s /sbin/nologin -d /srv/appsvc -m appsvc id appsvc sudo su - appsvc - Practice
Change, lock, then remove
For
lena: change her shell to/bin/zshif installed, otherwise to/bin/sh; set the account to expire on2027-01-31; lock it and show the lock is visible in/etc/shadow; unlock it; finally delete the account and her home directory.Check your result
After locking, the password field in
sudo grep lena /etc/shadowstarts with!. Afteruserdel -r,id lenafails withno such userand/home/lenais gone.Hint
usermod -s,-e,-L,-U.userdel -rremoves the home directory and mail spool.Solution
sudo usermod -s /bin/sh lena sudo usermod -e 2027-01-31 lena sudo usermod -L lena sudo grep '^lena:' /etc/shadow sudo usermod -U lena sudo userdel -r lena id lena ls /home - Challenge
Onboard three users from a list
Create users
amy,benandchoin one command sequence from this list, each with a comment equal to their capitalised name and the shell/bin/bash, all with the passwordWelcome#1. Do not type eachuseraddby hand. Verify all three exist and list their home directories.Check your result
getent passwd amy ben choprints three lines andls -d /home/amy /home/ben /home/cholists three directories.Hint
A
forloop over the names.${name^}capitalises the first letter in bash.Solution
for name in amy ben cho; do sudo useradd -c "${name^}" -s /bin/bash "$name" echo 'Welcome#1' | sudo passwd --stdin "$name" done getent passwd amy ben cho ls -d /home/amy /home/ben /home/cho