terminal.cab

Linux Basics Lesson 36 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Users and groups

8 minutes

Learning goals

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:

  • -m also create the home folder
  • -s /bin/bash the login shell, on Debian otherwise /bin/sh
  • -aG with usermod, add groups and keep the others

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

in the glossary: sudo

-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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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 -Gn

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
  • ~/handover.txt
What does id -Gn print?

Check works once you have chosen an answer.

Next up: what kind of machine this is, and your own shortcuts.

Quiz

  1. Question 1 of 3On Debian, someone ran sudo useradd anna without options. What is missing?

    Check works once you have chosen an answer.

  2. Question 2 of 3What is adduser on Debian and Ubuntu?

    Check works once you have chosen an answer.

  3. Question 3 of 3Which command shows the groups of the account mia?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 37, The system and your shortcuts (8 min)

What you can do now: sudo useradd -m -s /bin/bash -G adm anna

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: done
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 37, The system and your shortcuts (8 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.