cs-tech-primer

Computer Science tech primer for the University of Manitoba.

View on GitHub

UNIX permissions

An introduction to permissions, and some intricacies of them.

Learning outcomes

After doing this unit, students should be able to…

Read, write, execute

We’ve talked about how “everything is a file”. So, we can apply the idea of permissions to anything on the filesystem. In this case it means that all items can be given permission to Read, write, and execute. It can be any mix between all, and none of them.

Common permissions:

Then, we have three types of ‘users’ we can apply these permissions to: the owner (usually us), the group, and everybody.

Permissions on directories?

The x bit means that anyone can cd change directory into it. The w bit means it can be ls‘d. The r bit means new files can be created within it.

Groups?

Quick digression here. There are groups in UNIX permissions. Simple systems will minimally have: ‘users’ group, and ‘daemon’ group. These are often named different things, but have the same purpose. ‘Users’ are real people that use the computer. ‘Daemons’ are tasks in the background that make things go.

There is, of course, lots of variety here. Some systems will make a group with the same name as the user - so you have a group that is just you. No a terrible idea for people who don’t fully understand groups… though, not accidentally giving access to everyone in the group!

ls -l will show you the owner and group of a file.

So what makes it hard

3 different types of permissions, 3 different types of users. It’s easy to get bogged down in “who gets what how”.

The tools and lingo

The permissions put on a file are called the “mode” of the file.

We want to change the mode… chmod… of the file to change the permissions.

The ‘easy’ way - symbolic mode

A good way for beginners to change permissions is by using “symbolic mode”

More lingo… remember there three types of users:

Then, what can we do?

Now, we can add + permissions, or - remove permissions. Use “who”, then if the permission should be added or removed, then which permissions should be added or removed.

That’s a lot. Examples:

Remove execute from the user: chmod u-x theFile.txt

Allow everyone to read: chmod o+r theFile.txt

Allow everyone to read and execute: chmod o+rx theFile.txt

Allow everyone to execute using a: chmod a+x theFile.txt

There’s a lot more you can do, check out the manual page!

The ‘hard way - numeric mode

It’s not actually hard. This is the normal way that it’s done, generally.

Let’s change the topic to binary, and use flags…

Change rwx to all be positional binary flags:

Now, let’s represent these in octal… base 8. Which, for the uses here is identical to using decimal (base 10) or hexadecimal (base 16) since we are not exceeding 7.

Actually pretty easy for one user… but we have 3 user types…

We organize them with ugo: user, group, other. And, represent each group’s permission in octal (this is when it matters).

Examples!

Putting it together: chmod 711 aFile.txt

The sticky bit

There’s actually one more thing called the sticky bit. It lets users execute a file with the permissions of the owner.

chmod 1777 aProgram is literally the worst thing you could possibly, ever do.

But, now you know about it.

Default permissions

When we make a file… what are the permissions?

We could

$ touch new_file.txt
$ ls -l
total 0
-rw-------. 1 robg csstaff 0 Oct 28 11:54 new_file.txt

So… 600, read and write, just me. But what set this?

$ umask
67

This number is also in octal.

Files

Normal files start with 666, then this mask is subtracted, ish. It is a bitwise and of the compliment of the mask.

calculated = 666 & ~mask

Example:

~67 = 111 001 000

Octal Binary
----- -----------
666:  110 110 110
~67:  111 001 000

&  : 110 000 000

Which is 600 - what we see above!

Directories

Same as above, but from 777.

Octal Binary
----- -----------
777:  111 111 111
~67:  111 001 000

&  :  111 000 000