Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Examendoelen in dit hoofdstuk
- De gewone ugo/rwx-rechten tonen, instellen en wijzigen
- Problemen met bestandsrechten vinden en oplossen
- Standaard bestandsrechten beheren
Read owner, group and permissions
ls -l shows who owns a file and what the owner (u), the group (g) and all others (o) may do.
ls -l /etc/passwd /etc/shadow
-rw-r--r--. 1 root root 2401 Oct 25 09:10 /etc/passwd
----------. 1 root root 1290 Oct 25 09:10 /etc/shadow
ls -ld /tmp
drwxrwxrwt. 18 root root 4096 Oct 25 10:00 /tmp| Part | Example | Meaning |
|---|---|---|
| 1st sign | - / d / l | File / directory / symbolic link |
| 2–4 | rw- | Permissions of the owner (user) |
| 5–7 | r-- | Permissions of the group |
| 8–10 | r-- | Permissions of all others |
. at the end | . | The file has an SELinux context (a + = an ACL) |
| owner, group | root root | Who owns the file, which group |
Linux checks in this order and stops at the first match: owner? → group member? → others.
Let op
If you are the owner, only the owner bits count — even if the group or others have more rights.
Know what r, w and x do
The same letters mean different things for a file and for a directory.
| File | Directory | |
|---|---|---|
r (4) | Read the content | List the names in it (ls) |
w (2) | Change the content | Create, delete and rename files in it |
x (1) | Run it as a program | Enter it (cd) and reach files in it |
Onthoud
To delete a file you need w on the directory, not on the file. To read a file in a directory you need x on every directory in the path.
Change the owner and group
chown changes the owner (and group); chgrp changes only the group. Only root can give a file to another user.
chown lisa report.txt
chown lisa:sales report.txt
chown :sales report.txt
chgrp sales report.txt
chown -R apache:apache /var/www/html/app-Rchanges a whole directory tree.- A normal user can change the group of an own file, but only to a group he is a member of.
Controleer het
The new owner and group.
ls -l report.txt
-rw-r--r--. 1 lisa sales 0 Oct 25 10:20 report.txtSet permissions with numbers
Add the values: r = 4, w = 2, x = 1. Three digits: owner, group, others.
| Number | Letters | Common use |
|---|---|---|
644 | rw-r--r-- | Normal files |
600 | rw------- | Private files, SSH keys |
755 | rwxr-xr-x | Programs and normal directories |
750 | rwxr-x--- | Team directories (no access for others) |
700 | rwx------ | Private directories |
770 | rwxrwx--- | Shared team directories |
Set permissions with letters
The letter form changes only what you name: who (u g o a), how (+ - =), what (r w x).
Set default permissions with umask
New files start at 666 and new directories at 777. The umask takes bits away.
| umask | New file | New directory | Who |
|---|---|---|---|
0022 | 644 | 755 | RHEL default for root and normal users |
0027 | 640 | 750 | Hardened servers: others get nothing |
0077 | 600 | 700 | Very private |
umask
0022
umask 027
touch a; mkdir b; ls -ld a b
-rw-r-----. 1 student student 0 Oct 25 10:30 a
drwxr-x---. 2 student student 6 Oct 25 10:30 b- A
umaskcommand in the shell is valid only for that shell. - For one user, put it in
~/.bashrc.echo "umask 027" >> ~/.bashrc - For all users, put it in a file in
/etc/profile.d/.echo "umask 027" > /etc/profile.d/umask.sh
Onthoud
The umask never gives x to new files. A file is executable only after chmod +x.
Use the special permissions
Three extra bits: SUID, SGID and sticky. They are the fourth (first) digit of the number.
| Bit | Value | On a file | On a directory |
|---|---|---|---|
SUID u+s | 4 | The program runs as the owner (like passwd) | — |
SGID g+s | 2 | The program runs as the group | New files get the group of the directory |
sticky o+t | 1 | — | Users may delete only their own files (like /tmp) |
ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 32656 Oct 21 12:00 /usr/bin/passwd
chmod g+s /srv/sales
chmod o+t /srv/sales
chmod 3770 /srv/salesIn ls -l you see s or t instead of x. A capital S or T means the bit is set but x is not there (often a mistake).
Let op
SUID programs are a favourite target of attackers. Find them now and then: find / -perm -4000 -type f 2>/dev/null.
Give one more user access with an ACL
Normal permissions know one owner and one group. An ACL (access control list) adds rights for more users or groups.
setfacl -m u:carol:rx /srv/sales
setfacl -m g:account:rwx /srv/sales
getfacl /srv/sales
setfacl -d -m g:account:rwX /srv/sales
setfacl -x u:carol /srv/sales
setfacl -b /srv/sales-madds or changes an entry,-xremoves one,-bremoves all.-dsets a default ACL: new files in the directory get it.ls -lshows a+after the permissions when a file has an ACL.
Let op
The ACL mask limits the rights of all named entries and the group. chmod g-w on a file with an ACL changes the mask.
Protect files with attributes
File attributes work below the permissions. Even root cannot change an immutable file until the attribute is removed.
chattr +i /etc/resolv.conf
lsattr /etc/resolv.conf
----i----------------- /etc/resolv.conf
rm /etc/resolv.conf
rm: cannot remove '/etc/resolv.conf': Operation not permitted
chattr -i /etc/resolv.conf
chattr +a /var/log/app/audit.log| Attribute | Meaning |
|---|---|
i | Immutable: no change, delete, rename or link |
a | Append only: data can only be added (good for logs) |
Notitie
XFS (the RHEL default) supports i, a and a few more; ext4 supports more attributes.
Fix "Permission denied"
Check the layers one by one: path, owner and mode, ACL, attributes, SELinux.
- Check every directory in the path (
xneeded on each).namei -l /srv/sales/plan.txt - Check owner, group, mode and your groups.
- Check ACLs and attributes.
- If everything looks right, check SELinux (see the SELinux chapter).
ausearch -m avc -ts recent
Op het werk
Do not "fix" a problem with chmod 777. It opens the file for everybody and often hides the real cause (SELinux, a wrong group, a missing x on a directory).
Remember the main points
The most important facts of this chapter, in short.
- Every file has an owner, a group and permissions for user, group and others (
ugo). - r read, w write, x execute. On a folder: r = list names, w = create and delete files, x = enter the folder.
- Numbers: r = 4, w = 2, x = 1, so
750= rwx r-x ---. Letters:chmod g+w,o-rwx file. chown user:group filechanges owner and group;chgrp group filechanges only the group.- umask removes permissions from new files: umask 022 gives files 644 and folders 755.
- Special permissions: SUID (4, run as the owner), SGID (2, on a folder new files get the folder's group), sticky bit (1, only the owner can delete own files).
- ACLs give extra users or groups access:
setfacl -m u:lisa:rx file, read them withgetfacl; default ACLs (-d) apply to new files. - Attributes like immutable (
chattr +i) protect files even from root.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| Ownership | Which user and which group a file belongs to. |
| Permission mode | The rwx rights for user, group and others. |
| Octal notation | Permissions as numbers (640). |
| Symbolic notation | Permissions as letters (u+x, g-w). |
| umask | The permissions that new files and folders do NOT get. |
| SUID | A program runs with the rights of its owner (like passwd). |
| SGID | On a folder: new files get the group of the folder. |
| Sticky bit | In a shared folder, users can delete only their own files (like /tmp). |
| ACL | Access Control List: extra permissions for named users and groups. |
| Default ACL | An ACL on a folder that new files inherit. |
| Attribute | An extra file flag like immutable (i) or append only (a). |
Check what you learned
Think about the answer first, then open it.
1Which number gives the owner all rights, the group read and execute, and others nothing?
750.
2What do you need on a folder to open a file in it?
The x (execute) permission on the folder.
3How do you make sure new files in /data/sales get the group sales?
chgrp sales /data/sales and chmod g+s /data/sales (SGID).
4How do you stop users from deleting each other's files in a shared folder?
Set the sticky bit: chmod +t FOLDER.
5How do you give user carol read access to one file without changing owner or group?
6Which permissions does a new file get with umask 027?
640 (rw- r-- ---).
7How do you see the ACLs of a file?
getfacl FILE (a + after the mode in ls -l means the file has ACLs).
8How do you make a file impossible to change, even for root, until you remove the flag?
chattr +i FILE (remove with chattr -i).