Yes, you can secure files in Linux using Access Control Lists (ACLs), which provide a more granular permission system than the traditional user/group/other model. This article explains what ACLs are, how to use them, and when they are the right tool for the job.
What Are ACLs and Why Do You Need Them?
Standard Linux permissions (the rwx bits) are limited: each file has one owner, one group, and a set of permissions for “others.” This works for simple cases, but fails when you need to grant specific access to multiple users or groups without creating a new group for every combination. For example, you might want to give user Alice read access, user Bob read-write access, and the group "developers" execute access—all on the same file. Standard permissions can’t do that. ACLs extend the permission model to support multiple named users and groups, each with their own set of permissions.
ACLs are a POSIX standard, and Linux implements them through the acl package and the setfacl and getfacl commands. They are supported by most filesystems, including ext4, XFS, Btrfs, and ZFS, but not by all (e.g., FAT and exFAT do not support them).
How ACLs Work: The Basics
An ACL is a list of entries attached to a file or directory. Each entry specifies a user or group and the permissions granted (read, write, execute). The kernel checks the ACL when a process tries to access the file. The effective permissions for a user are the union of the permissions granted by the file’s owner, the named user entries, and the group entries that apply to that user’s groups.
There are two types of ACLs:
- Access ACLs – control access to the file itself.
- Default ACLs – apply only to directories; they set the initial ACL for files and subdirectories created inside the directory.
When you set a default ACL on a directory, new files inherit those permissions, which is useful for shared project directories.
Setting and Viewing ACLs with setfacl and getfacl
The primary tools are setfacl (set ACL) and getfacl (get ACL). They are part of the acl package, which may need to be installed on some distributions (e.g., sudo apt install acl on Debian/Ubuntu).
To view the ACL of a file:
getfacl /path/to/fileTo add a read-write ACL for user alice:
setfacl -m u:alice:rw /path/to/fileTo add a read-only ACL for group developers:
setfacl -m g:developers:r /path/to/fileTo remove a specific entry:
setfacl -x u:alice /path/to/fileTo remove all ACLs (reset to standard permissions):
setfacl -b /path/to/fileFor directories, you can set a default ACL:
setfacl -m d:u:alice:rwx /path/to/directoryThe -m flag modifies the ACL, -x removes an entry, and -b removes all entries. The d: prefix indicates a default ACL.
Choosing Between ACLs and Standard Permissions
ACLs are not a replacement for standard permissions; they are an extension. In most cases, standard permissions are sufficient. Use ACLs when:
- You need to grant access to multiple specific users or groups with different permission levels on the same file.
- You want to set up a shared directory where new files automatically get specific permissions for certain users or groups.
- You cannot or do not want to create a new group for every combination of users.
However, ACLs add complexity. They are harder to see at a glance (you need getfacl), and some tools (like rsync without the -A flag) may not preserve them. If you only need a simple “give this user access,” standard group permissions are often simpler.
Common Pitfalls and Misconceptions
One common misconception is that ACLs override standard permissions. In reality, the kernel checks the ACL first, and if an ACL entry matches the requesting user or a group they belong to, that permission is used; otherwise, the standard “other” permissions apply. This can lead to confusion when you set an ACL but the file still appears to deny access because the ACL entry is not what you think.
Another pitfall is the mask entry. When you set an ACL, a mask entry appears. The mask limits the effective permissions of named users, named groups, and the owning group. For example, if the mask is r--, then even if you set u:alice:rwx, Alice’s effective permissions will be r--. The mask is automatically adjusted when you modify an ACL, but you can set it explicitly with setfacl -m m::rx. Always check the mask with getfacl if access is denied unexpectedly.
Also, ACLs do not apply to NFS exports unless the server and client are configured to support them. Similarly, some backup tools may not preserve ACLs unless you use the appropriate flags.
Comparing ACLs with Other Access Control Methods
ACLs are one of several access control mechanisms in Linux. Others include:
- Standard Unix permissions – simple, but limited to one user and one group.
- POSIX capabilities – grant specific privileges to processes, not to files.
- SELinux / AppArmor – mandatory access control (MAC) systems that enforce system-wide policies, often used in conjunction with ACLs.
- chattr (file attributes) – set flags like immutable, which prevent even root from modifying a file until the flag is removed.
ACLs are more granular than standard permissions but less powerful than MAC systems. They are a good middle ground for many scenarios.
Performance and Overhead
ACLs have minimal performance overhead. The kernel stores ACLs as extended attributes, and checking them is slightly more complex than checking standard permissions, but for typical workloads the difference is negligible. The main cost is administrative: you need to manage ACLs on a per-file basis, which can become unwieldy in large systems. Use them judiciously.
Frequently Asked Questions
How do I copy ACLs from one file to another?
Use getfacl file1 | setfacl --set-file=- file2. This pipes the ACL of file1 into setfacl for file2, replacing its ACL.
Do ACLs work on all filesystems?
No. ACLs require filesystem support. Common Linux filesystems like ext4, XFS, Btrfs, and ZFS support them. FAT, exFAT, and some network filesystems do not. Check your filesystem’s documentation.
Can I use ACLs to give a user access to a directory but not its subdirectories?
Yes. Set an ACL on the directory itself, but do not set a default ACL. Without a default ACL, new files and subdirectories will not inherit the ACL. However, the user will still need execute permission on the directory to traverse it.
What is the difference between an ACL and a group?
A group is a collection of users. You can assign permissions to a group, but only one group can be the owner of a file. ACLs allow you to specify multiple named groups and users directly on a file, without changing the file’s group ownership.
How do I remove all ACLs from a directory tree?
Use find /path -exec setfacl -b {} to remove all ACLs from files and directories recursively. Be careful, as this will also remove default ACLs.
Are ACLs preserved when I copy files with cp or rsync?
By default, cp does not preserve ACLs unless you use the -p flag (or --preserve=mode). rsync preserves ACLs with the -A flag. Always test to ensure your backups include ACLs if you need them.