Users and groups

INPUT · Slides

Read the registers

01 / 08

Users are written in a file

You can become root. So, are you and root the only people on this Linux?

There is a way to find out. The list of users is not somewhere invisible; it is written in an ordinary text file.

~ $ wc -l < /etc/passwd10

Ten of them. You only know two, learner and root, so who are the other eight?

This lesson teaches not one new command. All you use is grep (chapter 3) and cut, sort and awk (chapter 5). It is the lesson where you find that pointing the tools you already have at the register lets you inspect a server.

02 / 08

The seven fields of /etc/passwd

You saw your own line last chapter. Read it again, counting the separators.

learner:x:100:101:learner:/home/learner:/bin/sh

Split into seven by :.

PositionWhat it isOn this line
1namelearner
2passwordx
3user number100
4group number101
5descriptionlearner
6home/home/learner
7the shell that starts/bin/sh

A table separated by : — exactly the shape the cut -d: of chapter 5 fits.

/etc/passwd is a table made to be read with command line tools. Which is why the separator is one fixed character and each person fits on one line.

03 / 08

The x in the second field means "it is over there"

Look at the second field. It is named passwd, and what is in it is a single x.

Everybody has x. Let us check.

~ $ cut -d: -f2 /etc/passwd | sort -ux

Drop the duplicates with sort -u and one line is left. So the real passwords are not here.

They used to be, encrypted. But /etc/passwd is readable by anybody (it was -rw-r--r--), so people took it away and cracked it by brute force.

So the contents moved to /etc/shadow, leaving only a mark saying "it is somewhere else". That is the file you read as root last lesson.

Separate what may be shown from what must be hidden into different files and guard it with permissions. Chapter 6's knowledge is at work here.

04 / 08

Some users are not people

Let us see the other eight.

~ $ cut -d: -f1 /etc/passwdrootdaemonbinsyssyncmailwww-dataoperatornobodylearner

www-data, mail, daemonnot people's names. They are users for programs.

Why would a program need a name? To narrow its permissions.

A web server takes traffic from outside, so it runs the greatest risk of being taken over. Were it running as root, whoever took it over could do anything.

So you make a user called www-data that can do almost nothing and run it under that standing, so that the damage stops there if it is taken over.

The "owner" of chapter 6 is being used here as a mechanism of defence.

05 / 08

/bin/false keeps people from logging in

Users for programs have another trick. Look at the shell in the seventh field.

~ $ cut -d: -f7 /etc/passwd | sort -u/bin/false/bin/sh/bin/sync

There is an odd one, /bin/false, a command that does nothing and ends in failure.

The seventh field was "the shell to run when you log in". With /bin/false there, an attempt to log in ends immediately, which means you cannot get in.

learner -> /bin/sh    can log inwww-data -> /bin/false  cannot get in

It runs as a program and cannot get in as a person. A neat arrangement.

Count them and /bin/false appears seven times. Seven of the ten cannot get in: everybody except you, root and sync.

06 / 08

The numbers have territories

Line up the numbers in the third field.

~ $ cut -d: -f3 /etc/passwd | sort -n012348333710065534

Clustered at the low end, with only 100 and 65534 away on their own. The territories are fixed.

NumberWhose
0root alone
1 to 99the system (for programs)
100 and uppeople
65534nobody (the role that holds no permissions)

The one that matters most is 0. Being root is decided by the number 0, not by the name.

So "delete the name root and it is safe" is not true, and conversely a user with number 0 under another name is also root.

That is useful for inspection. Two or more lines with number 0 means someone may have made a back door. Count them in the next question.

07 / 08

The register of groups is /etc/group

Groups have a register too, split into four.

~ $ grep learner /etc/grouplearner:x:101:
PositionWhat it isOn this line
1group namelearner
2passwordx (unused)
3group number101
4who is in it(empty)

The empty fourth field is curious. You are in the learner group and your name is not written there.

There is a reason. Your main group is written in the fourth field of /etc/passwd, which is the 101 in learner:x:100:101:....

What lines up in the fourth field of /etc/group is only people added later. Add someone to a group next lesson and a name appears here.

Incidentally /etc/group runs to 27 lines. There is a group per device, like disk and audio.

08 / 08

Now have a go

Today's questions are all about pointing the tools you already know at the registers.

What you wantThe toolLearned in
find a linegrepchapter 3
pull out a columncut -d:chapter 5
sort and drop duplicatessort -uchapter 5
pick lines by a conditionawk -F:chapter 5
countwc -lchapter 2

There is nothing new to learn. And still you come away able to inspect.

Towards the end there is a question that counts the users with number 0. That is an inspection people really do on real servers, and you already hold every tool it takes.