هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
ls -l deploy.sh
-rw-r--r--. 1 alice alice 812 Nov 17 09:10 deploy.sh
chmod u+x deploy.sh
chown -R nginx:nginx /var/www/shop
find /var/www/shop -type d -exec chmod 755 {} +
find /var/www/shop -type f -exec chmod 644 {} +
chmod 600 ~/.ssh/id_ed25519Never fix a problem with chmod 777 — it lets every user and every hacked service change the files.
Step by step
- Read the current rights. Three groups (owner, group, others), each with read, write, execute:
stat -c '%A %a %U:%G %n' /var/www/shop/index.php
-rw-r----- 640 root:nginx /var/www/shop/index.php| Number | Letters | Typical use |
|---|---|---|
644 | rw-r--r-- | Normal files: owner writes, all read. |
640 | rw-r----- | Config with secrets: owner writes, group reads, others nothing. |
600 | rw------- | Private keys, .env, password files. |
755 | rwxr-xr-x | Folders and programs. |
750 | rwxr-x--- | Folders only the owner and the group may enter. |
2770 | rwxrws--- | Shared team folder (setgid: new files get the folder's group). |
On a folder, x means "may enter it" and r means "may list it".
- Change the mode — numbers for a full set, letters for one change:
The capital X sets execute only on folders (and files that already have it) — handy with -R.
- Change the owner and group:
chown alice report.csv
chown alice:devs report.csv
chown -R app:app /srv/app
chgrp nginx /etc/nginx/certs/shop.key- Check the whole path. To open
/srv/app/conf/app.enva user needsxon every folder on the way:
namei -l /srv/app/conf/app.env
f: /srv/app/conf/app.env
dr-xr-xr-x root root /
drwxr-xr-x root root srv
drwxr-x--- app app app
drwxr-x--- app app conf
-rw-r----- app app app.env- A shared folder for a team (everyone in
devscan work on all files):
groupadd devs
usermod -aG devs alice
mkdir -p /srv/projects
chown root:devs /srv/projects
chmod 2770 /srv/projects
setfacl -d -m g:devs:rwX /srv/projects- One extra user without changing the group — use an ACL:
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
Permission denied although the file is 644 | A parent folder has no x for that user. | namei -l PATH; add x on the folder (chmod o+x or the right group). |
Script: Permission denied | No execute bit, or the file system is mounted noexec. | chmod u+x; or run bash script.sh; check findmnt -no OPTIONS /tmp. |
| Rights look right, still denied (RHEL) | SELinux label is wrong (for example files moved from home). | ls -Z; restorecon -Rv /var/www; check ausearch -m AVC -ts recent. |
A + at the end of the mode (rw-r--r--+) | An ACL adds or limits rights. | getfacl FILE; remove with setfacl -b FILE. |
| New files in the team folder have the wrong group | No setgid bit on the folder. | chmod g+s DIR (or 2770). |
| SSH refuses your key | ~/.ssh or the key is too open. | chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_*. |
| User added to a group, still denied | Group membership is read at login. | Log out and in again (or newgrp devs); check with id. |
Go further
- Command pages: chmod, chown, umask, setfacl, getfacl, stat.
- Next how-tos: Create a user with sudo rights, Find files by name, size, date, Find files with risky permissions, Find and fix SELinux denials, Share a folder with a group using ACLs, Protect a file with chattr.
- Learn it: RHCSA — chmod with numbers, RHCSA — Shared folders, RHCSA — ACLs, RHCSA — Fix access problems, TLCL — Permissions.
- SELinux labels: RHCSA — restorecon.
- Practise: Lab: build shared team folders, Lab: defaults, ACLs and fixing access problems.
1What does the x right mean on a folder?
The user may enter it (use paths through it). Without x on every parent folder, a file can not be opened.
2Why is chmod 2770 good for a team folder?
The owner and group can work, others have no access, and setgid (2) gives new files the folder's group.