You need to grant access to a folder to someone, but you don’t want to change their primary group or grant them access with a… chmod 777This is precisely the case where an ACL becomes useful. With setfacl, you add a rule for a specific user or group, while retaining the file’s classic owner and permissions.
I still advise you to read the ACL before modifying it. A correct rule may appear ineffective if a parent folder blocks access or if the ACL mask limits the effective rights.
Check permissions before adding an ACL
Orders setfacl And getfacl are provided by the package aclFirst, check for their presence:
command -v setfacl
command -v getfacl
On Debian or Ubuntu, install them with:
sudo apt update
sudo apt install acl
On Fedora, Rocky Linux or AlmaLinux:
sudo dnf install acl
On Arch Linux:
sudo pacman -S acl
I’m starting here with a file /srv/reports to which the user alice must have access. Start by noting the owner, group, and standard permissions:
ls -ld /srv/reports
namei -l /srv/reports
getfacl -p /srv/reports
namei -l displays each component of the path. This is important because the right x must allow the passage of all parent files. getfacl -p then displays the complete ACL without removing the initial slash from the path.
Granting precise access with setfacl
To grant to alice Before reading and navigating the file, test the modification without applying it:
sudo setfacl --test -m u:alice:rx /srv/reports
If the result matches your expectations, apply the rule:
sudo setfacl -m u:alice:rx /srv/reports
getfacl -p /srv/reports
In u:alice:rx, u targets a user named, alice is the account in question and rx allows reading the folder’s contents and navigating through them. Use the same logic for a group:
sudo setfacl -m g:audit:rx /srv/reports
This ACL does not automatically grant access to files already present. For a specific file, grant the necessary permissions on that file:
sudo setfacl -m u:alice:r-- /srv/rapports/bilan.pdf
getfacl -p /srv/rapports/bilan.pdf
If you need to process an entire tree structure, save it first, then use rX. THE X The uppercase letter adds execution only to folders and files that are already executable:
sudo getfacl -R -p /srv/rapports > acl-rapports-backup.txt
sudo setfacl -R -m u:alice:rX /srv/reports
Do not initiate a recursive modification on /srv /var/www For ease of use. Target the file that is actually relevant. The same principle applies to recursive chmod under Linuxwhich can quickly break the rights of an application.
Understanding the mask and effective rights
maskThis mask limits the rights of the owning group, named users, and named groups.
getfacl -p /srv/reports
For example, you can see:
user:alice:rwx
mask::r-x
# effective:r-x
In this case, alice poster rwxBut the mask removes the writing. Its effective rights are therefore r-xBy default, setfacl recalculates the mask from the relevant inputs. Avoid the option -n as long as you do not have a specific reason to prevent this recalculation.
A ls -l usually displays a sign + after permissions when an extended ACL is present. Also be careful with subsequent modifications. chmod The bits in the group correspond to the ACL mask and can therefore change the effective permissions. Always review the ACL after combining the two tools.
Inherit the rights to the new files
An access ACL governs the current folder. To have new files and subfolders inherit a rule, add a Default ACL on the directory:
sudo setfacl -m d:u:alice:rwX /srv/reports
getfacl -p /srv/reports
The lines prefixed by default: These changes will apply to objects created subsequently. They do not retroactively correct existing files. Also check the…umask from the service that creates the files, then create a test file with the same account or service.
In a shared workspace, first ask yourself if a traditional Linux group would be simpler to maintain. The guide on the managing groups under Linux This covers this case. An ACL is particularly useful for adding a targeted exception without multiplying groups or modifying the rights of all other users.
Test access and know how to go back
Don’t just settle for the release of getfaclTest with the account in question:
sudo -u alice test -x /srv/rapports && echo acces-dossier-ok
sudo -u alice test -r /srv/rapports/bilan.pdf && echo lecture-fichier-ok
To remove only the entrance of alice :
sudo setfacl -x u:alice /srv/reports
sudo setfacl -x u:alice /srv/rapports/bilan.pdf
To remove all extended ACLs from the folder while keeping the standard owner, group, and other entries:
sudo setfacl -b /srv/reports
To remove only the default ACL:
sudo setfacl -k /srv/reports
Finally, the file created with getfacl -R allows for a complete restoration of the directory tree:
sudo setfacl --restore=acl-rapports-backup.txt
Keep this backup safe: it may contain the tree’s paths, owners, groups, and special flags. On NFS, CIFS, or a particular file system, the available ACLs may depend on the server and mount options. If setfacl returns Operation not supportedFirst, identify the file system with findmnt -T /srv/reports instead of circumventing the problem with overly broad rights.
The manual pages of setfacl, getfacl And acl They detail the entries, the mask, and inheritance. For a folder where multiple users can write but must not delete each other’s files, also look at how the… sticky bit under Linux : it addresses a different problem than ACLs.