1 of 4

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.

  • useradd
  • usermod
  • userdel
  • id
  • getent
  • /etc/passwd
  • /etc/shadow
  • /etc/skel

45 min5 exercises

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.

FileHolds
/etc/passwdOne line per account: name:x:UID:GID:comment:home:shell
/etc/shadowThe password hash and aging fields, readable by root only
/etc/groupGroup names, GIDs and the members of each group
/etc/skelTemplate files copied into every new home directory
/etc/login.defs, /etc/default/useraddDefaults 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

CommandEffect
usermod -s /bin/sh userChange the login shell
usermod -c 'Name' userChange the comment
usermod -e 2027-01-31 userAccount expires on that date
usermod -L user / -U userLock / unlock the password
usermod -l new oldRename the login (the home directory does not move; add -d /home/new -m)
userdel userDelete the account; keeps the home directory
userdel -r userDelete 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.

  1. Warm-up

    Decode one account line

    Print the /etc/passwd line for alex and name each of its seven fields. Then show the same account through getent, 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 placeholder x, UID, primary GID, comment, home, shell. getent passwd alex prints the same line and also finds users from LDAP or IdM.

    Hint

    grep '^alex:' /etc/passwd and getent passwd alex.

    Solution
    grep '^alex:' /etc/passwd
    getent passwd alex
  2. Practice

    Create a normal user

    Create the user lena with UID 1500, comment Lena Berg, and the default home and shell. Set her password to Redhat123! without a prompt. Show her id and confirm her home directory exists and holds the files from /etc/skel.

    Check your result

    id lena shows uid=1500(lena) gid=1500(lena). ls -A /home/lena shows .bash_logout, .bash_profile and .bashrc.

    Hint

    useradd -u 1500 -c 'Lena Berg' lena, then echo '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
  3. Practice

    A service account that cannot log in

    Create a system account appsvc with 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 appsvc shows a UID below 1000. sudo su - appsvc prints This account is currently not available. and returns.

    Hint

    -r creates a system account, -s /sbin/nologin removes the login shell, -d and -m choose and create the home.

    Solution
    sudo useradd -r -s /sbin/nologin -d /srv/appsvc -m appsvc
    id appsvc
    sudo su - appsvc
  4. Practice

    Change, lock, then remove

    For lena: change her shell to /bin/zsh if installed, otherwise to /bin/sh; set the account to expire on 2027-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/shadow starts with !. After userdel -r, id lena fails with no such user and /home/lena is gone.

    Hint

    usermod -s, -e, -L, -U. userdel -r removes 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
  5. Challenge

    Onboard three users from a list

    Create users amy, ben and cho in one command sequence from this list, each with a comment equal to their capitalised name and the shell /bin/bash, all with the password Welcome#1. Do not type each useradd by hand. Verify all three exist and list their home directories.

    Check your result

    getent passwd amy ben cho prints three lines and ls -d /home/amy /home/ben /home/cho lists three directories.

    Hint

    A for loop 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