On Unix-like operating systems, the setfacl command sets file access control lists.
This page covers the Linux version of setfacl.
Syntax
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file …
setfacl –restore=file
Description
The setfacl utility sets ACLs (Access Control Lists) 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).
- Syntax
- Description
- ACL entries
- Automatically Created Entries
- Examples
- Related commands
- Linux commands help
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.
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.
Options
ACL entries
setfacl recognizes the following ACL entry formats (spaces in the following formats are optional, but are included for legibility):
Whitespace between delimiter characters and non-delimiter characters is ignored.
Proper ACL entries including permissions are used in modify and set operations (options -m, -M, –set and –set-file). Entries without the perms field are used for deletion of entries (options -x and -X).
For uid and gid, you can specify either a name or a number.
The perms field is a combination of characters that indicate the permissions: read (“r”), write (“w”), execute (“x”), or “execute only if the file is a directory or already has execute permission for some user” (capital “X”). Alternatively, the perms field is an octal digit (“0”-“7”).
Automatically Created Entries
Initially, files and directories contain only the three base ACL entries for the owner, the group, and others. There are some rules that need to be satisfied for an ACL to be valid:
- The three base entries cannot be removed. There must be exactly one entry of each of these base entry types.
- Whenever an ACL contains named user entries or named group objects, it must also contain an effective rights mask.
- Whenever an ACL contains any Default ACL entries, the three Default ACL base entries (default owner, default group, and default others) must also exist.
- Whenever a Default ACL contains named user entries or named group objects, it must also contain a default effective rights mask.
To help the user ensure these rules, setfacl creates entries from existing entries under the following conditions:
- If an ACL contains named user or named group entries, and no mask entry exists, a mask entry containing the same permissions as the group entry is created. Unless the -n option is given, the permissions of the mask entry are further adjusted to include the union of all permissions affected by the mask entry. (See the -n option description).
- If a Default ACL entry is created, and the Default ACL contains no owner, owning group, or others entry, a copy of the ACL owner, owning group, or others entry is added to the Default ACL.
- If a Default ACL contains named user entries or named group entries, and no mask entry exists, a mask entry containing the same permissions as the Default ACL’s group entry is added. Unless the -n option is given, the permissions of the mask entry are further adjusted to include the union of all permissions affected by the mask entry. (See the -n option description, above).
Examples
setfacl -m u:lisa:r file
Grant user lisa read access to file file.
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.
Related commands
chmod — Change the permissions of files or directories.getfacl — Display file access control lists.umask — Get or set the file mode creation mask.