Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 7Naam in het boek: Permissions Management

Control who can read, change and run files

Every file has an owner, a group and permissions. This chapter shows how to read them, change them, set good defaults, build shared team folders, and fix "Permission denied" problems.

  • 14 Lessen
  • 24 Commando's
  • 2 Labs

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

7.1

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
PartExampleMeaning
1st sign- / d / lFile / directory / symbolic link
2–4rw-Permissions of the owner (user)
5–7r--Permissions of the group
8–10r--Permissions of all others
. at the end.The file has an SELinux context (a + = an ACL)
owner, grouproot rootWho 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.

Commando's in deze les

7.2

Know what r, w and x do

The same letters mean different things for a file and for a directory.

FileDirectory
r (4)Read the contentList the names in it (ls)
w (2)Change the contentCreate, delete and rename files in it
x (1)Run it as a programEnter 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.

ls -ld /home/lisa
drwx------. 4 lisa lisa 112 Oct 25 09:20 /home/lisa
cat /home/lisa/notes.txt
cat: /home/lisa/notes.txt: Permission denied

7.3

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
  • -R changes 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.txt

7.4

Set permissions with numbers

Add the values: r = 4, w = 2, x = 1. Three digits: owner, group, others.

NumberLettersCommon use
644rw-r--r--Normal files
600rw-------Private files, SSH keys
755rwxr-xr-xPrograms and normal directories
750rwxr-x---Team directories (no access for others)
700rwx------Private directories
770rwxrwx---Shared team directories
chmod 644 index.html
chmod 600 ~/.ssh/id_ed25519
chmod 750 /srv/project

Commando's in deze les

7.5

Set permissions with letters

The letter form changes only what you name: who (u g o a), how (+ - =), what (r w x).

chmod u+x backup.sh
chmod g+w,o-rwx shared.txt
chmod a+r info.txt
chmod o= secret.txt
chmod -R g+rwX /srv/sales
  • + adds, - removes, = sets exactly.
  • Capital X adds x only to directories (and files that are already executable). Good with -R.

Examentip

Read the task carefully: "others may not read" → o-r; "only the owner" → chmod 600 or chmod go=.

Commando's in deze les

7.6

Set default permissions with umask

New files start at 666 and new directories at 777. The umask takes bits away.

umaskNew fileNew directoryWho
0022644755RHEL default for root and normal users
0027640750Hardened servers: others get nothing
0077600700Very 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
  1. A umask command in the shell is valid only for that shell.
  2. For one user, put it in ~/.bashrc.
    echo "umask 027" >> ~/.bashrc
  3. 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.

7.7

Use the special permissions

Three extra bits: SUID, SGID and sticky. They are the fourth (first) digit of the number.

BitValueOn a fileOn a directory
SUID u+s4The program runs as the owner (like passwd)—
SGID g+s2The program runs as the groupNew files get the group of the directory
sticky o+t1—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/sales

In 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.

7.8

Build a shared team directory

The classic exam task: a directory where a team works together, new files belong to the team group, and people cannot delete files of others.

  1. Create the group and add the members.
    groupadd sales
    usermod -aG sales anna; usermod -aG sales bob
  2. Create the directory and give it to the group.
    mkdir -p /srv/sales
    chown root:sales /srv/sales
  3. Give the group full rights, others nothing, plus SGID and sticky.
    chmod 3770 /srv/sales

Controleer het

A file made by anna gets the group sales, and bob cannot delete it.

ls -ld /srv/sales
drwxrws--T. 2 root sales 6 Oct 25 10:40 /srv/sales
su - anna -c 'touch /srv/sales/plan.txt; ls -l /srv/sales'
su - bob -c 'rm -f /srv/sales/plan.txt'
rm: cannot remove '/srv/sales/plan.txt': Operation not permitted

Notitie

The T is capital because others have no x. This is correct here.

7.9

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
  • -m adds or changes an entry, -x removes one, -b removes all.
  • -d sets a default ACL: new files in the directory get it.
  • ls -l shows 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.

7.10

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
AttributeMeaning
iImmutable: no change, delete, rename or link
aAppend 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.

7.11

Fix "Permission denied"

Check the layers one by one: path, owner and mode, ACL, attributes, SELinux.

  1. Check every directory in the path (x needed on each).
    namei -l /srv/sales/plan.txt
  2. Check owner, group, mode and your groups.
    ls -l /srv/sales/plan.txt; id
  3. Check ACLs and attributes.
    getfacl /srv/sales/plan.txt; lsattr /srv/sales/plan.txt
  4. 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).

7.12

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 file changes owner and group; chgrp group file changes 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 with getfacl; default ACLs (-d) apply to new files.
  • Attributes like immutable (chattr +i) protect files even from root.

7.13

Learn the key terms

The words of this chapter in simple English.

TermMeaning
OwnershipWhich user and which group a file belongs to.
Permission modeThe rwx rights for user, group and others.
Octal notationPermissions as numbers (640).
Symbolic notationPermissions as letters (u+x, g-w).
umaskThe permissions that new files and folders do NOT get.
SUIDA program runs with the rights of its owner (like passwd).
SGIDOn a folder: new files get the group of the folder.
Sticky bitIn a shared folder, users can delete only their own files (like /tmp).
ACLAccess Control List: extra permissions for named users and groups.
Default ACLAn ACL on a folder that new files inherit.
AttributeAn extra file flag like immutable (i) or append only (a).

7.14

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?
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).

Oefenen

Alle commando's in dit hoofdstuk