Users and groups

INPUT · Slides

Make a group and put people in it

01 / 08

Two people want to touch one file

You made taro last lesson, and found something out: taro cannot write in your room.

That is safe and good, and sometimes awkward.

When two of you want to keep adding to the same document, you want a place both you and taro can read and write.

There are three ways you might do it.

  • give the slot for everyone else a w (chmod 777) → open to everybody
  • make one of you the owner → the other cannot write
  • put the two of you in one group and give the group a w ← this one

The third is what groups are for. You can open it to just the people who need it.

Chapter 6 taught you that the middle three of the nine characters are the group. Today is about putting those three to work.

02 / 08

Make a box with addgroup

Making a group is one line, as root of course.

~ $ suPassword:~ $ addgroup team~ $ grep team /etc/groupteam:x:1002:

Of the four fields, the fourth is empty. A box has been made with nobody in it.

The number comes from 1000 upwards, the same idea as users.

The thing to watch is that making a group changes nothing on its own. You have prepared a box, and you still need to

  • put someone in it
  • set a file's group to that box
  • give the group's three characters a w

before anything is shared. Today does the first half (putting people in), and the next lesson does the second (the file side).

03 / 08

There are two ways in

You put someone in a group either when you make them, or afterwards.

To decide at the time, use -G.

~ $ adduser -D -G team hanako~ $ id hanakouid=1001(hanako) gid=1002(team)  groups=1002(team)

hanako belongs to team from the start. No group named hanako was made, because team became her main group.

So how do you put the existing taro in afterwards? Here it gets slightly awkward.

Real Linux has usermod -aG team taro. But it is not in this environment, which is a slimmed-down BusyBox.

What now? Nothing to worry about. The register is a text file.

04 / 08

Edit the register and they are in

Write a name in the fourth field of /etc/group and that person is in that group.

~ $ sed -i \    '/^team:.*:$/s/$/taro/' \    /etc/group~ $ grep team /etc/groupteam:x:1002:taro

The sed of chapter 5. Let us read it.

PartMeaning
-irewrite the file in place
/^team:.*:$/only lines starting team: and ending with :
s/$/taro/add taro at the end of the line

^ and $ for the start and end of a line are the regular expressions of chapter 3. Adding at the end of the line means writing after the fourth field.

There is a reason for picking only lines ending in :: only the ones with nobody in them yet. That way a second run does not give you tarotaro.

Adding a second person needs a comma, as in s/$/,hanako/. The rule is that the fourth field lists people separated by commas, like taro,hanako.

And id taro already shows team, because it simply reads the register again. It takes effect the moment you write it.

05 / 08

Main groups and groups added later

This is today's big pit. Compare hanako and taro.

~ $ grep team /etc/groupteam:x:1002:taro

Only taro is written there. But hanako is in team too. In the group without her name in the register — how?

Because they are written in different places.

KindWhere it is written
main groupthe fourth field of /etc/passwd (by number)
added laterthe fourth field of /etc/group (by name)

hanako was made with -G team, so the first; taro was added with sed, so the second.

Which means /etc/group alone does not tell you everyone in a group. You have to look at both.

Tedious, so there are tools. id and groups answer from both at once, so that you do not have to cross-reference.

06 / 08

Check with id and groups

Let us line up the tools for checking.

~ $ groups tarotaro team~ $ id tarouid=1000(taro) gid=1000(taro)  groups=1000(taro),1002(team)
FormWhat you get
groups namejust the names of the groups they are in
id nameall the numbers and names
id -gn namejust the name of the main group

Look at how id lays it out. gid= is the main group and groups= is all of them. taro's main is taro, with team added.

hanako comes out like this.

gid=1002(team) groups=1002(team)

Her main group is team itself. Two people "in team" who got there differently.

Either way sharing works. The difference shows up in the group of newly made files, which comes from the main group. The next lesson shows it in action.

07 / 08

The rule for removing

You remove a group with delgroup. But you can be refused.

~ $ delgroup shopdelgroup: 'kenji' still has  'shop' as their primary group!

You cannot remove it while someone has it as their main group.

Why? Because if you could, kenji would become a person belonging to a group that does not exist. Look at kenji's files with ls -l and the group would be a bare number — the "no name to look up" state you saw last lesson.

To stop you making that broken state, it refuses first.

A group whose only members were added with sed, on the other hand, can go. Those people have a main group elsewhere to fall back on.

delgroup team    -> goes through (taro's main is taro)

Type id taro afterwards and team has gone. Remove a group and membership of it goes with it.

08 / 08

Now have a go

Four things to type today.

What to doWhat to type
make a boxaddgroup team
put someone in as you make themadduser -D -G team name
put someone in afterwardssed -i into the fourth field of /etc/group
checkid name, groups name

Making needs root, checking needs nobody, because /etc/group is readable by anybody.

So today's questions often go "make it as root, exit, then check". Less time as root is better.

The sed line is a little long. Copy it calmly. Everything in it comes from chapters 3 and 5.