ACL: setfacl, getfacl
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
setfacl -m u:name:rx filegives a user permissions,-m g:group:rwa group;getfacl fileshows them all.- A
+inls -lmeans the file has an ACL; in that case the group column is actually the mask. setfacl -m d:g:group:rwx dir— a default ACL: inherited automatically by new files in the directory.setfacl -x u:nameremoves one entry,setfacl -bremoves all;-Rfor a directory.- XFS and ext4 support ACLs by default;
cp -aandtar --aclskeep them.
The problem
The /srv/share directory belongs to the wheel group (previous lesson). Now:
aliis not a wheel member but must read the reports in the directory;- the
auditgroup must read too; - others must see nothing.
Standard permissions cannot do this — a file has only one group. The solution is ACLs.
Setting an ACL
[ali@srv-web01 ~]$ sudo setfacl -m u:ali:rx /srv/share [ali@srv-web01 ~]$ sudo setfacl -m g:audit:rx /srv/share [ali@srv-web01 ~]$ ls -ld /srv/share drwxrws---+ 2 root wheel 32 Dec 1 12:50 /srv/share [ali@srv-web01 ~]$ getfacl /srv/share # file: srv/share # owner: root # group: wheel # flags: -s- user::rwx user:ali:r-x group::rwx group:audit:r-x mask::rwx other::---
Reading the output:
| Line | Meaning |
|---|---|
user::rwx | The owner's (root) permissions |
user:ali:r-x | An ACL entry for the named user ali |
group::rwx | The file's own group (wheel) |
group:audit:r-x | The named group audit |
mask::rwx | The maximum permission for named entries and the group |
other::--- | Others |
flags: -s- | The SGID bit |
A + appeared at the end in ls -l — the file has an ACL.
The mask: a hidden limit
The mask is the maximum permission that can be given to named users and groups. On a file with an ACL, the group column of ls -l actually shows the mask. If you run chmod g-w, the mask changes and everyone's effective permission drops:
[ali@srv-web01 ~]$ sudo setfacl -m u:leyla:rwx /srv/share [ali@srv-web01 ~]$ sudo chmod g=rx /srv/share [ali@srv-web01 ~]$ getfacl /srv/share | grep -E "leyla|mask" user:leyla:rwx #effective:r-x mask::r-x
#effective:r-x — leyla was given rwx, but because the mask is r-x the real permission is r-x. Remember the rule: be careful with chmod on files that have ACLs. To restore the mask: setfacl -m m::rwx /srv/share.
Default ACL: for new files
A normal ACL applies only to the existing file. For it to pass automatically to files created later in the directory, a default (d:) ACL is needed:
[ali@srv-web01 ~]$ sudo setfacl -m d:g:wheel:rwx,d:u:ali:rx,d:o::- /srv/share [ali@srv-web01 ~]$ sudo -u ops1 touch /srv/share/yeni.txt [ali@srv-web01 ~]$ getfacl /srv/share/yeni.txt # file: srv/share/yeni.txt # owner: ops1 # group: wheel user::rw- user:ali:r-x #effective:r-- group::rwx #effective:rw- group:wheel:rwx #effective:rw- mask::rw- other::---
Now wheel members can edit each other's files (regardless of umask), and ali only reads. x is not effective on files — a new file gets no execute permission by default, which is correct.
This is the cleanest solution to the previous lesson's "the team should edit each other's files" problem.
Management commands
| Command | What it does |
|---|---|
setfacl -m u:name:rw file | Add or change |
setfacl -m g:group:r file | For a group |
setfacl -m d:u:name:rx dir | Default ACL |
setfacl -x u:name file | Remove one entry |
setfacl -k dir | Remove only the default ACL |
setfacl -b file | Remove the whole ACL |
setfacl -R -m g:audit:rX dir | For all existing files (X — x only for directories) |
getfacl -R dir > acl.txt | Backup |
setfacl --restore=acl.txt | Restore |
Careful with backups: plain cp and tar can lose ACLs. Use cp -a and tar --acls --xattrs.
Lab: read access for an auditor
Finish the "A shared directory: SGID and ACL" task in the Red Hat simulator:
- Give ali read and traverse access only on
/srv/share. - Check with
getfacl.
[ali@srv-web01 ~]$ sudo setfacl -m u:ali:rx /srv/share [ali@srv-web01 ~]$ getfacl /srv/share | grep ali user:ali:r-x [ali@srv-web01 ~]$ ls -ld /srv/share drwxrws---+ 2 root wheel 6 Dec 1 12:50 /srv/share
- (On your own VM) set a default ACL for wheel, create files as two different users and try to write to each other's file — it should work.
- (On your own VM) run
chmod g=r /srv/shareand see how the#effectivecolumn changes ingetfacl. Then restore withsetfacl -m m::rwx.
Check: in the simulator both conditions of the task are green; ls -ld shows a +.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1Which command gives the user ali read-only access to a file?
2What does a + at the end of the permissions in ls -l mean?
3getfacl shows user:leyla:rwx #effective:r-x. Why?
4What is needed for an ACL to pass automatically to files created in a directory in the future?
5Which command is right for copying files together with their ACLs?
Comments
Sign in to comment.