Learn / Linux / Users & Permissions
Lesson 22 of 74

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.

20 min read Report a problem Hands-on lab included 6-question quiz

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

Normal: 3 slotsowner · group · others
user:: rw-root
group:: r--finance
other:: ---everyone else
setfacl -m
ACL: as many as you needthe same 3, plus named entries
user:: rw-root
user:meera: r--the auditor
group:: r--finance
group:support: rw-a second team
mask:: rw-cap for the named entries
other:: ---everyone else

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

bash
getfacl /srv/finance/q3-report.txt
text
getfacl: 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:

LineSame 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

bash
setfacl -m u:meera:r /srv/finance/q3-report.txt
getfacl /srv/finance/q3-report.txt
ls -l /srv/finance
EntryMeaning
u:meera:ruser meera: read
g:support:rwxgroup support: read, write, execute
m::rxthe 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 ....

text
# 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.txt

Two 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.

text
cat: /srv/finance/q3-report.txt: Permission denied

Meera, 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:

bash
setfacl -m u:meera:x /srv/finance
su - meera -c 'cat /srv/finance/q3-report.txt; cat /srv/finance/payroll.txt; ls /srv/finance'
text
Q3 revenue: 48.2 lakh
cat: /srv/finance/payroll.txt: Permission denied
ls: cannot open directory '/srv/finance': Permission denied

One file readable, nothing else. That precision is what ACLs are for.

Try it · Task 1 of 4

Access for one user

Let Meera read the Q3 report, and nothing else in /srv/finance.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -l /srv/finance
getfacl /srv/finance/q3-report.txt
su - meera -c 'cat /srv/finance/q3-report.txt'
text
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 denied

Without 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

bash
setfacl -m u:meera:r /srv/finance/q3-report.txt
getfacl /srv/finance/q3-report.txt
ls -l /srv/finance
PartMeaning
-mmodify: add or change entries
u:meera:ruser meera gets r
text
# 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.txt

The 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?

bash
su - meera -c 'cat /srv/finance/q3-report.txt'
text
cat: /srv/finance/q3-report.txt: Permission denied

The 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:

bash
setfacl -m u:meera:x /srv/finance
su - meera -c 'cat /srv/finance/q3-report.txt; cat /srv/finance/payroll.txt; ls /srv/finance'
text
Q3 revenue: 48.2 lakh
cat: /srv/finance/payroll.txt: Permission denied
ls: cannot open directory '/srv/finance': Permission denied

Exactly what was asked: she can read the report, can't read payroll, and can't even see which other files exist.

2 · Your task

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
text
setfacl -m u:meera:r /srv/finance/q3-report.txt
setfacl -m u:meera:x /srv/finance

What each part does

  • setfacl -m u:meera:r /srv/finance/q3-report.txt: modify the file's ACL and add an entry for user meera with read permission only.
  • setfacl -m u:meera:x /srv/finance: give meera execute on the folder, so she can reach files inside it. No r, 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 line user:meera:r--.
First start the lab machine in the terminal panel on the right.

How Linux checks an ACL

The order matters, and the first match wins:

1 · Are you the owner?use user:: and stop
no
2 · Is there a user:YOU: entry?use it (limited by the mask) and stop
no
3 · Are you in the owning group or any named group?granted if one of them allows it (limited by the mask), otherwise denied
in none of them
4 · Use other::

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

bash
setfacl -m g:support:rwx /srv/tickets
getfacl /srv/tickets
text
# file: srv/tickets
# owner: ops
# group: ops
user::rwx
group::r-x
group:support:rwx
mask::rwx
other::r-x

The 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.

Try it · Task 2 of 4

A second group on a folder

Let the support group create files in /srv/tickets, which belongs to ops.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

/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

bash
ls -ld /srv/tickets
su - anita -c 'echo note > /srv/tickets/102.txt'
text
drwxr-xr-x 2 ops ops 4096 Nov 24 16:02 /srv/tickets
-bash: line 1: /srv/tickets/102.txt: Permission denied

2. Add the support group

bash
setfacl -m g:support:rwx /srv/tickets
getfacl /srv/tickets

g:support:rwx = group support gets rwx.

text
# file: srv/tickets
# owner: ops
# group: ops
user::rwx
group::r-x
group:support:rwx
mask::rwx
other::r-x

Ops still owns the folder, the ops group still has r-x, and support now has rwx.

3. Try again

bash
su - anita -c 'echo note > /srv/tickets/102.txt'
ls -l /srv/tickets
su - vikram -c 'echo more >> /srv/tickets/102.txt'
text
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 denied

Anita 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.

2 · Your task

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
text
setfacl -m g:support:rwx /srv/tickets

What each part does

  • setfacl -m g:support:rwx /srv/tickets: add an ACL entry for the group support with 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/tickets now shows a + at the end of the permissions.
First start the lab machine in the terminal panel on the right.

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).

bash
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.

text
# 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:

text
setfacl -R -m g:support:rwX /srv/tickets        # existing files and folders
setfacl -R -d -m g:support:rwX /srv/tickets     # defaults on every folder

Capital X works as in chmod: execute only on directories.

Try it · Task 3 of 4

Default ACLs

Make every new ticket editable by the whole support team.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
setfacl -d -m g:support:rw /srv/tickets
getfacl /srv/tickets

-d = apply to the default ACL instead of the folder's own.

text
# 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-x

The 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

bash
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
text
# file: srv/tickets/103.txt
# owner: anita
# group: anita
user::rw-
group::r-x	#effective:r--
group:support:rw-
mask::rw-
other::r--
ok

The 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:

text
# 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.

2 · Your task

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
text
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

  • -d changes 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 getfacl on that file: it shows group:support:rw-.
First start the lab machine in the terminal panel on the right.

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.

text
# 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

bash
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 entries

Note: -x takes just the name, without permissions.

text
drwxr-x--- 2 root finance 4096 Nov 24 16:02 /srv/finance

After setfacl -b, the + is gone: only normal permissions remain.

Backing up and copying ACLs

bash
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 another

Many tools silently drop ACLs. Keep them with cp -a, rsync -aA (capital A = ACLs) and tar --acls.

Try it · Task 4 of 4

Revoke and back up

Remove Meera's access when the audit ends, and save a backup of the tickets ACLs.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
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'
OptionMeaning
-x u:meeraremove the entry for user meera (no permissions after the name)
-pprint absolute paths (and skip the "Removing leading '/'" note)
text
# file: /srv/finance/q3-report.txt
# owner: root
# group: finance
user::rw-
group::r--
mask::r--
other::---
cat: /srv/finance/q3-report.txt: Permission denied

Meera'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:

text
setfacl -b /srv/finance
ls -ld /srv/finance       →  drwxr-x--- 2 root finance ... /srv/finance

2. Back up and restore ACLs

bash
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.

2 · Your task
  1. Remove all of meera's ACL entries under /srv/finance (the file and the folder).
  2. Save a backup of the ACLs of /srv/tickets and everything in it to /root/tickets-acl.txt with getfacl -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
text
setfacl -x u:meera /srv/finance/q3-report.txt /srv/finance
getfacl -R /srv/tickets > /root/tickets-acl.txt

What each part does

  • setfacl -x u:meera FILE FOLDER: remove (x) meera's entries from both the file and the folder. With -x you 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 with setfacl --restore=FILE.
First start the lab machine in the terminal panel on the right.

ACLs or groups?

Use a group when...Use an ACL when...
a team shares many files long-termone person needs one file
membership changes over timea 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 -l shows +.
  • getfacl reads; setfacl -m grants, -x revokes, -b removes all, -R recursive.
  • Directories still need x along the path.
  • setfacl -d -m sets 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 with cp -a / rsync -aA. Prefer groups; use ACLs for the exceptions.
Check your understanding

Quick quiz

Feedback