متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 13Security · Linux

How to change file permissions and owner

"Permission denied" is one of the most common errors at work: a web server can not read its files, a script will not run, a team can not write to a shared folder. The fix is almost always the owner (chown) or the mode (chmod) — but the wrong fix (chmod -R 777) opens the server to everyone. This how-to shows the right commands, safe defaults for files and folders, shared team folders, and the hidden causes (the parent folder, ACLs, SELinux).

  • 5 الدروس
  • 23 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

13.1

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_ed25519

Never fix a problem with chmod 777 — it lets every user and every hacked service change the files.

13.2

Step by step

  1. 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
NumberLettersTypical use
644rw-r--r--Normal files: owner writes, all read.
640rw-r-----Config with secrets: owner writes, group reads, others nothing.
600rw-------Private keys, .env, password files.
755rwxr-xr-xFolders and programs.
750rwxr-x---Folders only the owner and the group may enter.
2770rwxrws---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".

  1. Change the mode — numbers for a full set, letters for one change:
chmod 640 app.env
chmod g+w report.csv
chmod o-rwx private/
chmod -R u=rwX,g=rX,o= /srv/app

The capital X sets execute only on folders (and files that already have it) — handy with -R.

  1. 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
  1. Check the whole path. To open /srv/app/conf/app.env a user needs x on 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
  1. A shared folder for a team (everyone in devs can 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
  1. One extra user without changing the group — use an ACL:
setfacl -m u:bob:rX /srv/projects/report
getfacl /srv/projects/report

13.3

Check that it worked

Test as the user that had the problem, not as root:

sudo -u nginx cat /var/www/shop/index.php > /dev/null && echo OK
sudo -u alice touch /srv/projects/test && ls -l /srv/projects/test

The new file in the shared folder must show the group devs.

13.4

Common problems

ProblemCauseFix
Permission denied although the file is 644A 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 deniedNo 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 groupNo 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 deniedGroup membership is read at login.Log out and in again (or newgrp devs); check with id.

13.5

Go further

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.

كل الأوامر في هذا الفصل