Users, groups and permissions
Unit 2 of 5 in Operating systems and the command line.
You will be able to read a Linux permission string and explain who can read, write or run a file and what the setuid bit changes.
Lesson
Three permissions for three kinds of people
Every file has three permissions: read, write and execute. For a directory, read means you may list what is inside, write means you may create and remove files in it, and execute means you may enter it and reach the files inside.
Three kinds of people can have different permissions: the file's owner, other users who are in the file's group, and everyone else. Files get an owner and a group when they are created, and chown and chgrp change them. That gives nine permission bits in all, which is what the string you see in a long directory listing shows.
Which set of permissions applies to you
The system picks one set, not all of them. If the process's effective user ID equals the file's owner, the owner permissions apply. Otherwise, if the file's group matches the process's group or one of its supplementary groups, the group permissions apply. If neither holds, the permissions for everyone else apply.
This order has a practical lesson: being the owner uses the owner bits even if the group bits are more generous. Read the matching set, not the best-looking one.
Who is root, and what the setuid bit does
On traditional Unix systems a privileged process is one whose effective user ID is 0, called superuser or root, and privileged processes bypass all kernel permission checks. Linux later split that power into units called capabilities that can be turned on and off separately, which lets a program have one needed privilege instead of all of them.
Besides read, write and execute there are special bits. The setuid bit makes a program run with the effective user ID of the file's owner instead of the person who started it. That is useful for a few tools and risky for anything else, so a defender checks which programs have it.
Every process has real and effective user and group IDs. The real IDs say who owns the process; the effective IDs are what the kernel uses to decide most permissions.
Default permissions and the umask
New files do not start with every permission. A setting called the umask is applied when a file or directory is created, and the permissions in the umask are turned off from what the program asked for. A stricter umask means new files start more private. Checking the umask for the account that runs a service is a quick hardening step.
Key points
- Read, write and execute apply to owner, group and everyone else; directories use them differently.
- The system uses the first matching set: owner, then group, then everyone else.
- Root bypasses permission checks; capabilities split that power into smaller pieces.
- The setuid bit runs a program as its owner; the umask turns permissions off for new files.
Sources
- GNU Coreutils manual: Structure of File Mode Bits
- Linux manual page: path_resolution(7), Permissions
- Linux manual page: capabilities(7)
- Linux manual page: umask(2)
- Linux manual page: credentials(7), process identifiers
Practice in the labs
Make users, read permission strings and see how the umask and setuid bit behave, all in a safe sandbox.
- Manage Linux users, groups, and passwords (and understand /etc/passwd and /etc/shadow): You create users and groups and decode each line of the account files, so owner and group stop being abstract.
- Understand the umask and default file permissions: A short lab that shows exactly how the umask changes new files.
- Linux file permissions, ownership, and the SUID bit: You read and set permissions and see the setuid bit as a mechanism, which is how you will audit for it later.
Knowledge check
- You own a file with permissions that give the owner read only and the group read and write. You are in the group. What can you do? Answer: Read only, because the owner set applies to you as the owner. The system uses the owner permissions when your effective user ID matches the file's owner, even if the group set is more generous (path_resolution(7)).
- What does the setuid bit on a program do? Answer: Runs the program with the effective user ID of the file's owner. The GNU Coreutils manual describes setuid as setting the process's effective user ID to that of the file on execution, which is why it needs auditing.
- What is a capability in Linux? Answer: A unit of privilege traditionally held by root, which can be enabled and disabled independently. capabilities(7) says Linux divides the privileges traditionally associated with superuser into distinct units that can be independently enabled and disabled.
- What does a stricter umask do? Answer: Turns off more permissions on newly created files and directories. umask(2) says permissions in the umask are turned off from the mode requested when a file or directory is created. It does not touch existing files.
Back to the syllabus or see all classes.