Access Control Lists
When owner/group/others can't express what you need: ACL entries for extra users and groups, getfacl and setfacl, the + in ls -l, default ACLs that new files inherit, the mask and #effective, revoking access, backing up ACLs, and when to use ACLs versus groups.
The limit of owner, group and others
Normal permissions have room for one owner and one group. For most files that's plenty, and you should always try groups first. But some requests simply don't fit:
- An external auditor must read one report, not the folder full of payroll files, and must not join the finance group.
- A folder belongs to the ops team, but the support team must write into it too.
- A backup or monitoring user needs read access to one application's logs, owned by another service.
You could create a new group for every combination of people, but that quickly gets out of hand. Access Control Lists (ACLs) let you attach extra entries to a single file or folder: "and also user meera: read", "and also group support: read and write".
Normal permissions vs an ACL
ACLs are supported by ext4, XFS, Btrfs and most filesystems you'll meet. The tools come from the acl package (apt install acl if getfacl is missing).
Reading an ACL: getfacl
getfacl /srv/finance/q3-report.txtgetfacl: Removing leading '/' from absolute path names
# file: srv/finance/q3-report.txt
# owner: root
# group: finance
user::rw-
group::r--
other::---With no extra entries, getfacl is just another view of the normal permissions:
| Line | Same as |
|---|---|
user::rw- | the owner's bits |
group::r-- | the owning group's bits |
other::--- | others' bits |
The first line is only a notice: it prints paths without the leading / so the output can be restored anywhere. getfacl -p keeps absolute paths and skips the notice.
Granting access: setfacl -m
setfacl -m u:meera:r /srv/finance/q3-report.txt
getfacl /srv/finance/q3-report.txt
ls -l /srv/finance| Entry | Meaning |
|---|---|
u:meera:r | user meera: read |
g:support:rwx | group support: read, write, execute |
m::rx | the mask (below) |
o::- | others: nothing (same as chmod o=) |
Several at once: setfacl -m u:meera:r,g:audit:r file. Recursively: setfacl -R -m ....
# file: srv/finance/q3-report.txt
# owner: root
# group: finance
user::rw-
user:meera:r--
group::r--
mask::r--
other::---
-rw-r----- 1 root finance 22 Nov 24 16:02 payroll.txt
-rw-r-----+ 1 root finance 22 Nov 24 16:02 q3-report.txtTwo new lines: Meera's entry and a mask. And ls -l now shows + after the permissions. That + is the only hint ls gives, so learn to notice it.
Don't forget the directories
ACLs don't change the rule from the File Permissions lesson: you need x on every directory along the path.
cat: /srv/finance/q3-report.txt: Permission deniedMeera, right after the command above. /srv/finance is rwxr-x---, so she's "others" on the folder and can't get in.
Give her x on the folder, which lets her reach files she knows the name of, but not r, so she can't list the folder's other files:
setfacl -m u:meera:x /srv/finance
su - meera -c 'cat /srv/finance/q3-report.txt; cat /srv/finance/payroll.txt; ls /srv/finance'Q3 revenue: 48.2 lakh
cat: /srv/finance/payroll.txt: Permission denied
ls: cannot open directory '/srv/finance': Permission deniedOne file readable, nothing else. That precision is what ACLs are for.
Access for one user
Let Meera read the Q3 report, and nothing else in /srv/finance.
The finance reports belong to group finance. Adding Meera to that group would let her read payroll too. An ACL entry gives her access to exactly one file, and leaves the normal permissions alone.
1. Look at the current permissions
ls -l /srv/finance
getfacl /srv/finance/q3-report.txt
su - meera -c 'cat /srv/finance/q3-report.txt'total 8
-rw-r----- 1 root finance 22 Nov 24 16:02 payroll.txt
-rw-r----- 1 root finance 22 Nov 24 16:02 q3-report.txt
getfacl: Removing leading '/' from absolute path names
# file: srv/finance/q3-report.txt
# owner: root
# group: finance
user::rw-
group::r--
other::---
cat: /srv/finance/q3-report.txt: Permission deniedWithout extra entries, getfacl just shows the normal permissions in a different format: user:: (owner), group:: (owning group), other::. The first line is only a note that it shows paths without the leading /.
2. Add an entry for Meera
setfacl -m u:meera:r /srv/finance/q3-report.txt
getfacl /srv/finance/q3-report.txt
ls -l /srv/finance| Part | Meaning |
|---|---|
-m | modify: add or change entries |
u:meera:r | user meera gets r |
# file: srv/finance/q3-report.txt
# owner: root
# group: finance
user::rw-
user:meera:r--
group::r--
mask::r--
other::---
-rw-r----- 1 root finance 22 Nov 24 16:02 payroll.txt
-rw-r-----+ 1 root finance 22 Nov 24 16:02 q3-report.txtThe new user:meera:r-- line, plus a mask line (explained in step 3). In ls -l, the + marks the file that has an ACL.
3. Still denied?
su - meera -c 'cat /srv/finance/q3-report.txt'cat: /srv/finance/q3-report.txt: Permission deniedThe file allows her now, but the folder is rwxr-x---: Meera is "others" there, with no x, so she can't reach anything inside. Give her x (enter) on the folder, but not r, so she can't list the other files:
setfacl -m u:meera:x /srv/finance
su - meera -c 'cat /srv/finance/q3-report.txt; cat /srv/finance/payroll.txt; ls /srv/finance'Q3 revenue: 48.2 lakh
cat: /srv/finance/payroll.txt: Permission denied
ls: cannot open directory '/srv/finance': Permission deniedExactly what was asked: she can read the report, can't read payroll, and can't even see which other files exist.
Using ACLs, let meera read /srv/finance/q3-report.txt. She must not be able to read payroll.txt or list the folder, and must not be added to any group.
Expected result: su - meera -c 'cat /srv/finance/q3-report.txt' prints the report; payroll.txt and ls /srv/finance are refused.
Hint: the commands, explained
setfacl -m u:meera:r /srv/finance/q3-report.txt
setfacl -m u:meera:x /srv/financeWhat each part does
setfacl -m u:meera:r /srv/finance/q3-report.txt: modify the file's ACL and add an entry for usermeerawith read permission only.setfacl -m u:meera:x /srv/finance: give meera execute on the folder, so she can reach files inside it. Nor, so she can't list what else is there.- Both commands print nothing when they work. Check with
getfacl /srv/finance/q3-report.txt: you'll see a new lineuser:meera:r--.
How Linux checks an ACL
The order matters, and the first match wins:
A consequence: a named user entry can also take away access. setfacl -m u:anita:- file blocks Anita even if her group could read the file.
Sharing a folder between two teams
setfacl -m g:support:rwx /srv/tickets
getfacl /srv/tickets# file: srv/tickets
# owner: ops
# group: ops
user::rwx
group::r-x
group:support:rwx
mask::rwx
other::r-xThe folder still belongs to ops:ops, and the support team can now create files in it. But the files they create get their normal default permissions (rw-r--r--, owned by the creator), so teammates can't edit each other's tickets. The folder's own ACL doesn't carry over to new files. Default ACLs do.
A second group on a folder
Let the support group create files in /srv/tickets, which belongs to ops.
/srv/tickets belongs to user and group ops. The support team must also create files there. You can't give the folder two groups, and making it writable by everyone is far too much. A group ACL entry adds support alongside ops.
1. Support can't write
ls -ld /srv/tickets
su - anita -c 'echo note > /srv/tickets/102.txt'drwxr-xr-x 2 ops ops 4096 Nov 24 16:02 /srv/tickets
-bash: line 1: /srv/tickets/102.txt: Permission denied2. Add the support group
setfacl -m g:support:rwx /srv/tickets
getfacl /srv/ticketsg:support:rwx = group support gets rwx.
# file: srv/tickets
# owner: ops
# group: ops
user::rwx
group::r-x
group:support:rwx
mask::rwx
other::r-xOps still owns the folder, the ops group still has r-x, and support now has rwx.
3. Try again
su - anita -c 'echo note > /srv/tickets/102.txt'
ls -l /srv/tickets
su - vikram -c 'echo more >> /srv/tickets/102.txt'total 8
-rw-r--r-- 1 ops ops 25 Nov 24 16:02 101.txt
-rw-r--r-- 1 anita anita 5 Nov 24 16:02 102.txt
-bash: line 1: /srv/tickets/102.txt: Permission deniedAnita can create tickets. But the file she created is rw-r--r--, owned by anita, so Vikram, her teammate, can't add to it. The folder's ACL doesn't apply to files created inside it. That's the next step.
Give the support group rwx on /srv/tickets with an ACL. Don't change the folder's owner, group or normal permissions.
Expected result: anita and vikram can create files in /srv/tickets, the folder still belongs to ops:ops, and ls -ld shows a +.
Hint: the commands, explained
setfacl -m g:support:rwx /srv/ticketsWhat each part does
setfacl -m g:support:rwx /srv/tickets: add an ACL entry for the groupsupportwith read, write and execute on the folder.- The folder's owner and group (ops:ops) don't change; support is added alongside them.
ls -ld /srv/ticketsnow shows a+at the end of the permissions.
Default ACLs: inheritance for new files
A default ACL sits on a directory as a template. It's never checked for the directory itself; it's copied onto every new file and subfolder created inside it (and subfolders also get it as their own default, so it carries down the tree).
setfacl -d -m g:support:rw /srv/tickets
su - anita -c 'echo note > /srv/tickets/103.txt'
getfacl /srv/tickets/103.txt-d = change the default ACL.
# file: srv/tickets/103.txt
# owner: anita
# group: anita
user::rw-
group::r-x #effective:r--
group:support:rw-
mask::rw-
other::r--The new ticket came with group:support:rw-, so the whole team can edit it. When a directory has a default ACL, the default ACL is used instead of the umask for new files.
Default ACLs only affect new files. For what's already there, apply the normal entry recursively too:
setfacl -R -m g:support:rwX /srv/tickets # existing files and folders
setfacl -R -d -m g:support:rwX /srv/tickets # defaults on every folderCapital X works as in chmod: execute only on directories.
Default ACLs
Make every new ticket editable by the whole support team.
A default ACL is set on a directory and isn't used for the directory itself: it's a template copied onto every new file and subfolder created inside. Set a default entry for support, and every new ticket comes with group:support:rw- already on it.
1. Set the default
setfacl -d -m g:support:rw /srv/tickets
getfacl /srv/tickets-d = apply to the default ACL instead of the folder's own.
# file: srv/tickets
# owner: ops
# group: ops
user::rwx
group::r-x
group:support:rwx
mask::rwx
other::r-x
default:user::rwx
default:group::r-x
default:group:support:rw-
default:mask::rwx
default:other::r-xThe default: lines are the template. setfacl filled in the other default entries from the folder's current permissions.
2. Create a ticket and check it
su - anita -c 'echo note > /srv/tickets/103.txt'
getfacl /srv/tickets/103.txt
su - vikram -c 'echo more >> /srv/tickets/103.txt' && echo ok# file: srv/tickets/103.txt
# owner: anita
# group: anita
user::rw-
group::r-x #effective:r--
group:support:rw-
mask::rw-
other::r--
okThe new file arrived with group:support:rw-, and Vikram can edit it.
3. The mask
Notice group::r-x #effective:r--. The mask is the upper limit for every entry except the owner and others. Here it's rw- (new files never get x), so the inherited r-x is cut down to r--. getfacl prints #effective: whenever the mask takes something away.
The mask also matters when someone runs chmod on a file with an ACL: changing the group bits changes the mask, not the owning group:
# file: srv/tickets/103.txt
# owner: anita
# group: anita
user::rw-
group::r-x #effective:r--
group:support:rw- #effective:r--
mask::r--
other::r--That's after chmod g-w /srv/tickets/103.txt: the mask lost w, so support's rw- is now effectively read-only. If an ACL "stops working" after a chmod, check the mask, and restore it with setfacl -m m::rw file.
Add a default ACL to /srv/tickets that gives the support group rw on every new file.
Expected result: a new file created by anita in /srv/tickets can be edited by vikram, and getfacl on it shows group:support:rw-.
Hint: the commands, explained
setfacl -d -m g:support:rw /srv/tickets(g:support:rwx works too: new files never get x, and new subfolders would then let support in.)
What each part does
-dchanges the folder's default ACL: a template copied onto every new file created inside it.-m g:support:rw: new files will give the support group read and write.- Existing files are not changed. Test it by creating a file as anita, then
getfaclon that file: it showsgroup:support:rw-.
The mask
The mask is the maximum permission any named user, named group or the owning group can get. It exists so that chmod keeps working sensibly on files with ACLs: on such a file, chmod's group digit changes the mask, not the owning group's entry.
# file: srv/tickets/103.txt
# owner: anita
# group: anita
user::rw-
group::r-x #effective:r--
group:support:rw- #effective:r--
mask::r--
other::r--After chmod g-w /srv/tickets/103.txt: the mask became r--, and support's rw- is now effectively read-only. getfacl marks every entry the mask cuts down with #effective:.
setfacl recalculates the mask whenever you add an entry, so you rarely set it directly. If an ACL "suddenly stopped working", look for #effective: and fix it with setfacl -m m::rw file.
Removing entries
setfacl -x u:meera /srv/finance/q3-report.txt /srv/finance # remove one entry
setfacl -x g:support /srv/tickets # a group entry
setfacl -k /srv/tickets # remove the default ACL
setfacl -b /srv/finance # remove ALL extra entriesNote: -x takes just the name, without permissions.
drwxr-x--- 2 root finance 4096 Nov 24 16:02 /srv/financeAfter setfacl -b, the + is gone: only normal permissions remain.
Backing up and copying ACLs
getfacl -R /srv/tickets > /root/tickets-acl.txt # save
cd / && setfacl --restore=/root/tickets-acl.txt # restore
getfacl file1 | setfacl --set-file=- file2 # copy one file's ACL to anotherMany tools silently drop ACLs. Keep them with cp -a, rsync -aA (capital A = ACLs) and tar --acls.
Revoke and back up
Remove Meera's access when the audit ends, and save a backup of the tickets ACLs.
ACLs are easy to forget, because ls -l only shows a small +. When access should end, remove the entries explicitly. Before big permission changes, back up ACLs so you can restore them.
1. Remove Meera's entries
setfacl -x u:meera /srv/finance/q3-report.txt /srv/finance
getfacl -p /srv/finance/q3-report.txt
su - meera -c 'cat /srv/finance/q3-report.txt'| Option | Meaning |
|---|---|
-x u:meera | remove the entry for user meera (no permissions after the name) |
-p | print absolute paths (and skip the "Removing leading '/'" note) |
# file: /srv/finance/q3-report.txt
# owner: root
# group: finance
user::rw-
group::r--
mask::r--
other::---
cat: /srv/finance/q3-report.txt: Permission deniedMeera's line is gone and she's refused. An empty mask line can remain; setfacl -b removes every extra entry at once, which takes away the + too:
setfacl -b /srv/finance
ls -ld /srv/finance → drwxr-x--- 2 root finance ... /srv/finance2. Back up and restore ACLs
getfacl -R /srv/tickets > /root/tickets-acl.txt
head -5 /root/tickets-acl.txt-R = the whole tree. To restore later: setfacl --restore=/root/tickets-acl.txt (run from /, since paths are saved without the leading /).
Plain cp and many archive tools drop ACLs. Use cp -a, rsync -aA or tar --acls to keep them, and back them up with getfacl -R before migrations.
- Remove all of meera's ACL entries under
/srv/finance(the file and the folder). - Save a backup of the ACLs of
/srv/ticketsand everything in it to/root/tickets-acl.txtwithgetfacl -R.
Expected result: meera can't read the report, getfacl -R /srv/finance doesn't mention her, and the backup file contains group:support.
Hint: the commands, explained
setfacl -x u:meera /srv/finance/q3-report.txt /srv/finance
getfacl -R /srv/tickets > /root/tickets-acl.txtWhat each part does
setfacl -x u:meera FILE FOLDER: remove (x) meera's entries from both the file and the folder. With-xyou give only the name, not permissions.getfacl -R /srv/tickets: print the ACL of the folder and everything inside it (Recursive).> /root/tickets-acl.txt: save that output to a file instead of the screen. You can restore it later withsetfacl --restore=FILE.
ACLs or groups?
| Use a group when... | Use an ACL when... |
|---|---|
| a team shares many files long-term | one person needs one file |
| membership changes over time | a second team needs a folder another team owns |
you want simple audits (getent group) | you can't change the owner or group (an app expects them) |
Groups are easier to see and audit: ls -l shows them directly. ACLs are hidden behind a +, so they're easy to forget. A good habit is to document every ACL you set, and include getfacl -R in access reviews.
Granting the file but not the folder. The ACL is right but the user has no x on a parent directory. Check with namei -l /path/to/file.
chmod after setting ACLs. chmod 640 on a file with ACL entries lowers the mask to r--, silently cutting every named entry to read-only.
Expecting a folder's ACL to apply to new files. Only default entries (setfacl -d) are inherited.
Copying without -a/-A. The copy loses its ACLs, and access mysteriously breaks after a migration.
"What is an ACL and when would you use one?" Extra permission entries for specific users or groups beyond owner/group/others. For example, one auditor reading one file, or two teams writing to one folder.
"How do you see whether a file has an ACL?" A + after the permissions in ls -l; getfacl file shows the entries.
"What's a default ACL?" A template on a directory that new files and subdirectories inherit, set with setfacl -d -m.
"What's the ACL mask?" The maximum permission for named users, named groups and the owning group. chmod on the group bits of a file with an ACL changes the mask.
Summary
- ACLs add named users and groups to a file:
user:NAME:rwx,group:NAME:rwx.ls -lshows+. getfaclreads;setfacl -mgrants,-xrevokes,-bremoves all,-Rrecursive.- Directories still need
xalong the path. setfacl -d -msets default entries that new files inherit (used instead of the umask).- The mask caps named entries and the owning group;
chmod g...changes it. Watch for#effective:. getfacl -R > backup,setfacl --restore=backup; copy withcp -a/rsync -aA. Prefer groups; use ACLs for the exceptions.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.