Permissions: rwx, chmod, chown, umask
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Three sets (u, g, o) × three rights (r=4, w=2, x=1):
chmod 640,chmod g+w,o=. chown user:group,chgrp group,-Rfor a directory; only root changes the owner.umaskshows the current value; a permanent change goes in~/.bashrcfor one user,/etc/profile.d/*.shfor everyone.- In
ls -la trailing.shows an SELinux context and+an ACL. - On "Permission denied", check three layers: permissions (
ls -l), ACL (getfacl), SELinux (ls -Z,ausearch).
A quick review
| Code | Looks like | Typical use |
|---|---|---|
755 | rwxr-xr-x | Directories, scripts |
644 | rw-r--r-- | Regular files, web content |
750 | rwxr-x--- | A directory only the group can enter |
640 | rw-r----- | Configuration the group reads |
600 | rw------- | Keys, secret configuration |
700 | rwx------ | Home directories on RHEL |
Symbolic form: u, g, o, a + +, -, = + r, w, x.
[ali@srv-web01 ~]$ sudo mkdir -p /srv/hesabat [ali@srv-web01 ~]$ sudo chown ops1:webdev /srv/hesabat [ali@srv-web01 ~]$ sudo chmod 770 /srv/hesabat [ali@srv-web01 ~]$ ls -ld /srv/hesabat drwxrwx---. 2 ops1 webdev 6 Dec 1 12:02 /srv/hesabat
The right way for a whole directory — separate permissions for directories and files:
[ali@srv-web01 ~]$ sudo find /srv/hesabat -type d -exec chmod 770 {} + [ali@srv-web01 ~]$ sudo find /srv/hesabat -type f -exec chmod 660 {} + [ali@srv-web01 ~]$ sudo chmod -R u=rwX,g=rwX,o= /srv/hesabat
The capital X in the last command means "give x only to directories and to files that are already executable". One command gets both directories and files right.
umask
[ali@srv-web01 ~]$ umask 0022 [ali@srv-web01 ~]$ umask -S u=rwx,g=rx,o=rx [ali@srv-web01 ~]$ grep -n "umask" /etc/login.defs | head -n 2 91:UMASK 022
For a user working in a team, umask 007 (file 660, directory 770) — "others see nothing":
[ali@srv-web01 ~]$ echo "umask 007" | sudo tee -a /home/ops1/.bashrc [ali@srv-web01 ~]$ sudo -iu ops1 bash -c "umask; touch /tmp/sinaq; ls -l /tmp/sinaq" 0007 -rw-rw----. 1 ops1 ops1 0 Dec 1 12:10 /tmp/sinaq
For all users: create the file /etc/profile.d/umask.sh. The UMASK in /etc/login.defs is used only when the home directory is created.
Three layers: permissions, ACL, SELinux
On Ubuntu "Permission denied" is almost always about permissions. On RHEL the check goes through three layers, and all three must allow it:
| Layer | How to see it | How to fix it | Lesson |
|---|---|---|---|
| Standard permissions | ls -l | chmod, chown | this lesson |
| ACL | + in ls -l, getfacl | setfacl | 3.5 |
| SELinux | ls -Z, ausearch -m AVC | restorecon, semanage, setsebool | unit 4 |
[ali@srv-web01 ~]$ ls -l /var/www/html/index.html -rw-r--r--. 1 root root 31 Dec 1 12:15 /var/www/html/index.html [ali@srv-web01 ~]$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost/ 403 [ali@srv-web01 ~]$ ls -Z /var/www/html/index.html unconfined_u:object_r:user_home_t:s0 /var/www/html/index.html
The permissions are fine (644), but the site returns 403. The cause is the SELinux context — user_home_t is forbidden for the web server. We will solve this scenario in unit 4. The lesson here: on RHEL chmod 777 is never the fix — and often it does not even help.
Lab: a team directory
In the Red Hat simulator, on srv-web01:
- Create the directory
/srv/hesabatowned by ops1 with group wheel (if the simulator has no webdev group). - Set the mode to
770and check withls -ld. - Create a file in the directory and find the trailing dot in
ls -l. - Look at the SELinux context with
ls -Z /srv/hesabat.
[ali@srv-web01 ~]$ sudo mkdir -p /srv/hesabat [ali@srv-web01 ~]$ sudo chown ops1:wheel /srv/hesabat [ali@srv-web01 ~]$ sudo chmod 770 /srv/hesabat [ali@srv-web01 ~]$ ls -ld /srv/hesabat drwxrwx---. 2 ops1 wheel 6 Dec 1 12:02 /srv/hesabat [ali@srv-web01 ~]$ ls -Zd /srv/hesabat unconfined_u:object_r:var_t:s0 /srv/hesabat
Check: ls -ld → drwxrwx---. ops1 wheel.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What does the capital X do in chmod -R u=rwX,g=rwX,o= dir?
2Where is a permanent umask for all users set?
3In ls -l output a + appears at the end of the permissions. What does it mean?
4The permissions are 644, but httpd returns 403. What is the next step?
5What is the default mode of a newly created home directory on RHEL?
Comments
Sign in to comment.