Linux setfacl command

From Brian Nelson Ramblings
Jump to: navigation, search

Linux setfacl command

About setfacl

The setfacl command sets file access control lists.

Permissions Required To Modify ACLs

The file owner and processes capable of CAP_FOWNER are granted the right to modify ACLs of a file, which is analogous to the permissions required for accessing the file mode.

On current Linux systems, root is the only user with the CAP_FOWNER capability, so you must be the superuser to use setfacl if you are not the owner of the file.

Setfacl Syntax

setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
setfacl --restore=file

Description

The setfacl utility sets Access Control Lists (ACLs) of files and directories. On the command line, a sequence of commands is followed by a sequence of files (which in turn can be followed by another sequence of commands, and so on).

The options -m and -x expect an ACL on the command line. Multiple ACL entries are separated by commas (","). The options -M and -X read an ACL from a file or from standard input. The ACL entry format is described in the ACL Entries section, below.

The --set and --set-file options set the ACL of a file or a directory. The previous ACL is replaced. ACL entries for this operation must include permissions.

The -m (--modify) and -M (--modify-file) options modify the ACL of a file or directory. ACL entries for this operation must include permissions.

The -x (--remove) and -X (--remove-file) options remove ACL entries. It is not an error to remove an entry which does not exist. Only ACL entries without the perms field are accepted as parameters, unless the POSIXLY_CORRECT environment variable is defined.

When reading from files using the -M and -X options, setfacl accepts the output produced by getfacl. There is at most one ACL entry per line. After a pound sign ("#"), everything up to the end of the line is treated as a comment.

If setfacl is used on a file system which does not support ACLs, setfacl operates on the file mode permission bits. If the ACL does not fit completely in the permission bits, setfacl modifies the file mode permission bits to reflect the ACL as closely as possible, writes an error message to standard error, and returns with an exit status greater than 0.

ACL Entries

[d[efault]:] [u[ser]:]uid [:perms] Permissions of the user with user ID uid, or permissions of the file's owner if uid is empty.
[d[efault]:] g[roup]:gid [:perms] Permissions of the group with group ID gid, or permissions of the owning group if gid is empty.
[d[efault]:] m[ask][:] [:perms] Effective rights mask.
[d[efault]:] o[ther][:] [:perms] Permissions of others.

Setfacl Examples

setfacl -m u:lisa:r file

Grant user lisa read access to file file.

setfacl -R -m u:lisa:r /var/log/

Grant user lisa read access to the /var/log/ directory and all files/folders under /var/log/

setfacl -m m::rx file

Revoke write access from all groups and all named users (using the effective rights mask) for file file.

setfacl -x g:staff file

Remove the group entry for the group staff from file file's ACL.

getfacl file1 | setfacl --set-file=- file2

Copy the ACL of file1 to file2.

getfacl --access dir | setfacl -d -M- dir

Copy the access ACL into the default ACL.