Users and groups

INPUT · Slides

Become root (`su`)

01 / 08

The wall you could not get over

Remember the three walls in the last chapter you simply could not get over?

chown root memo.txtchown: memo.txt: Operation not permitted

Different wording from Permission denied. A refusal no amount of fiddling with chmod could fix.

What you wantedHow you were refused
change the ownerOperation not permitted
hand it to a group you are not inOperation not permitted
change the permissions of someone else's fileOperation not permitted

What they share is "you have no standing".

So who does? That is today's subject, root.

02 / 08

What root is

Root is a special user, and the specialness comes down to one thing.

> It slips past every permission check.

Whatever the nine characters say makes no difference. A 000 file (nobody can do anything) is readable, and the owner of someone else's file can be changed.

How to tell is fixed too. Root's number (uid) is always 0.

~ $ id -u100          <- you(root would be 0)

It is decided by whether the number is 0, not by whether the name is root. Which is why a script checking "am I the administrator?" looks at whether id -u gives 0. Remember writing that in the practice last chapter?

The name can be changed; the meaning of the number 0 cannot. So going by the number is the sure way.

03 / 08

Switch with su

The command for switching is su (switch user).

~ $ suPassword:

It asks for a password, so type it. In this course it is fulfledge.

While you type, nothing appears on the screen. Not even a *, so you wonder whether it is working, but it is going in. It is built that way to stop people looking over your shoulder.

Press return and you switch.

/home/learner $ whoamiroot

If whoami says root, you are through.

Get the password wrong and you see su: Authentication failure with nothing else changed. Just type it again; no need to panic.

04 / 08

The prompt will not tell you

Many textbooks say "$ is an ordinary user, # is root".

~ $     <- ordinary user~ #     <- root

A convention worth knowing. But this course's Linux does not switch to #. It stays $ after you switch.

~ $ suPassword:/home/learner $

The only thing that changed is that ~ (home) became /home/learner (the plain path). Root's home is /root, so where you are is no longer home.

In other words, do not rely on the prompt. There is one way to be sure.

whoami

Even in the real world that is the surest. A prompt can be set to anything, so a # does not prove you are root.

05 / 08

Come back with exit

The way back is exit.

/home/learner $ exit~ $ whoamilearner

Knowing the mechanism makes it clearer. su starts a new shell on top, and the old shell waits underneath.

[ learner's shell ]  <- before su[ root's shell      ]  <- after su (stacked on top)[ learner's shell ]  <- exit removes the top one

So exit does not "stop being root"; it closes the shell on top. Two sus need two exits.

And one important habit. Spend as little time as root as you can.

The reason is simple. Nothing stops root. Type rm -rf / and it really does remove everything. As an ordinary user the permissions protect you; as root that protection is gone.

When you are done, exit at once. Get it into your fingers.

06 / 08

Make something as root and it belongs to root

One pit that is easy to miss.

/home/learner $ touch newfile.txt/home/learner $ exit~ $ ls -l newfile.txt-rw-r--r-- 1 root root 0 newfile.txt

The owner is root, because you made it as root.

What is the trouble? You are back to learner, so you cannot rewrite that file. It became someone else's (root's).

This really is a common accident: work as root, then try to touch it as an ordinary user and get Permission denied. The cause is that you made it as root a moment ago.

To mend it, become root again and type chown learner newfile.txt.

When you work as root, check who owns what you made. That habit saves a lot of trouble later.

07 / 08

sudo is not in this environment

In the real world you use sudo more than su.

sudo chown root memo.txt     # run just this one line as the administrator

The difference is the range.

RangePassword
suroot until you leaveroot's password
sudothat one commandyour own password

sudo is safer. You are root for one command, and nobody has to be told root's password. What each person did also stays in the record.

But it is not in this course's Linux.

~ $ sudo ls-sh: sudo: not found

It was left out to keep things small. So here you do the same things with su. The thinking is the same.

When you graduate to your own computer or a server you rent, remember that sudo takes the lead role.

08 / 08

Now have a go

Your home holds two files.

  • secret.txt — it says the secret contents
  • mine.txt — it says this belongs to me

Every question goes the same way.

1. Type su
2. When Password: appears, type fulfledge (it will not show)
3. Do what you came for
4. Come back with exit

Do not forget the fourth. Move on to the next question having forgotten, and everything you make ends up belonging to root.

Even if you do forget, this course puts you back automatically between questions, so relax. But nobody puts you back on a real terminal. Build the habit here.