Linux Basics Lesson 36 of 38
Users and groups
8 minutes
Learning goals
- Read with id who you are and which groups an account belongs to.
- Create an account with a home folder and a shell with sudo useradd -m -s.
- Add an account to a group with usermod -aG, and know why -G without -a is dangerous.
id: who you are
id shows your user ID, your primary group and every group you belong to. With a name, it shows somebody else:
id
uid=1000(lerner) gid=1000(lerner) groups=1000(lerner),4(adm),27(sudo)
id gast
uid=1001(gast) gid=1001(gast) groups=1001(gast)
lerner is in adm, which may read logs, and in sudo, which may use sudo. gast is only in its own group. Where the groups are stored is in /etc/group. cat and grep are not loaded on this page and answer with command not found, and getent group adm shows the line you need.
useradd: a new account
Creating accounts needs root, so sudo goes in front. On this training machine it asks for no password:
sudo useradd -m -s /bin/bash -G adm anna
Purpose: Creates a user account. usermod changes it, and id shows it.
Syntax: useradd [-m] [-s SHELL] [-G GROUPS] NAME | usermod -aG GROUP NAME | id [NAME]
Options:
Example:
sudo useradd -m -s /bin/bash -G adm anna
id anna
uid=1002(anna) gid=1002(anna) groups=1002(anna),4(adm)
getent passwd anna
anna:x:1002:1002::/home/anna:/bin/bash-m creates the home folder /home/anna. Without it, Debian's useradd creates none. -s /bin/bash sets the shell, otherwise it is /bin/sh. -G adm adds the group. getent passwd shows the new line in /etc/passwd.
The account has no password yet. sudo passwd anna sets one and asks for it twice, without showing what you type. Until then nobody can log in with a password.
On Debian and Ubuntu there is also adduser, a friendlier front end that asks questions and creates the home folder by itself. Here it gives a hint, it comes with LPIC-1. On RHEL and Rocky Linux, useradd creates the home folder without -m as well, because the defaults are different there.
usermod -aG: into another group
Later Anna needs sudo too. usermod -aG adds a group and keeps the others. The example creates her account first, because every example on this page starts on a fresh machine:
sudo useradd -m -s /bin/bash -G adm anna
sudo usermod -aG sudo anna
id anna
uid=1002(anna) gid=1002(anna) groups=1002(anna),4(adm),27(sudo)
The new group counts from Anna's next login on. A session that is already open keeps its old groups. id anna shows what is stored, not what her running shell knows. Logging out and in again, or newgrp sudo in her session, makes it count.
Practice
Terminal task 1/2
Create an account for Mia, user name mia, with a home folder, and in the group adm so that she can read the logs.
Task 1/2: Create an account for Mia, user name mia, with a home folder, and in the group adm so that she can read the logs.
Create an account for Mia, user name mia, with a home folder, and in the group adm so that she can read the logs.
Task complete
Solution
sudo useradd -m -s /bin/bash -G adm mia Why: id mia now shows the group adm, and /home/mia exists. Without -m, Debian would have created no home folder, and mia would land in / after logging in.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/2
Which groups is the account gast in? Show it.
Task 2/2: Which groups is the account gast in? Show it.
Which groups is the account gast in? Show it.
Task complete
Solution
id gast Why: Only its own group gast, 1001. So gast may neither read the logs nor use sudo.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Find the error
This line is supposed to add Anna to the group docker, and she keeps her other groups. Tap the part that is wrong.
Parts of the command
Check works once you have tapped a part.
Fill the gap
sudo usermod -G adm anna adds the group and keeps Anna's other groups.
sudo usermod -…G adm anna adds the group and keeps Anna's other groups.
Check works once you have typed an answer.
Predict the output
Command
id -GnBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
- ~/handover.txt
Check works once you have chosen an answer.
Next up: what kind of machine this is, and your own shortcuts.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: sudo useradd -m -s /bin/bash -G adm anna
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: done
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
id [<name>]user ID, primary group and all groups -
sudo useradd -m -s /bin/bash <name>new account with home folder and bash -
sudo usermod -aG <group> <name>add a group, keep the others -
sudo passwd <name>set a password -
getent passwd <name>the line of the account in /etc/passwd
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 67 KB)