ACL stands for Access Control List, which is a set of rules that define who can access or modify a file or a directory in Linux. Unlike the regular permissions, which only allow you to specify the access rights for the owner, the group, and the others, the ACLs allow you to specify the access rights for any user or group, as well as the default permissions for new files and directories.
What is the getfacl command?
The getfacl command is used to display the ACLs of a file or a directory. The syntax of the getfacl command is:
getfacl [options] file
The output of the getfacl command shows the following information:
- The file name
- The owner of the file
- The group of the file
- The regular permissions of the file (same as the output of ls -l)
- The ACL entries of the file, which consist of the following fields:
- The type of the entry, which can be one of the following:
- u for user
- g for group
- o for others
- m for mask
- d for default
- The name or ID of the user or group, or empty for others or mask
- The permissions of the entry, which can be one of the following:
- r for read
- w for write
- x for execute
- for no permission
- The type of the entry, which can be one of the following:
For example, the output of getfacl file1
may look like this:
# file: file1
# owner: root
# group: root
user::rw-
user:alice:rwx
group::r--
mask::rwx
other::r--
This means that the file1 has the following ACLs:
- The owner (root) has read and write permissions
- The user alice has read, write, and execute permissions
- The group (root) has read permission
- The mask, which determines the maximum permissions for the user and group entries, has read, write, and execute permissions
- The others have read permission
What is the setfacl command?
The setfacl command is used to set or modify the ACLs of a file or a directory. The syntax of the setfacl command is:
setfacl [options] -m entry file
The -m option specifies the entry to be added or modified, which has the same format as the output of the getfacl command. For example, to grant the user bob read and write permissions on file1, you can use the following command:
setfacl -m u:bob:rw file1
To remove an entry from the ACL of a file or a directory, you can use the -x option, followed by the entry to be removed. For example, to revoke the permissions of the user alice on file1, you can use the following command:
setfacl -x u:alice file1
To remove all the ACL entries from a file or a directory, you can use the -b option, which restores the regular permissions. For example, to remove all the ACLs from file1, you can use the following command:
setfacl -b file1
To set the default ACLs for a directory, which will be inherited by the new files and subdirectories created inside it, you can use the -d option, followed by the entry to be added or modified. For example, to set the default permissions for the group staff to read and write on the directory dir1, you can use the following command:
setfacl -d -m g:staff:rw dir1
To apply the ACLs recursively to all the files and subdirectories in a directory, you can use the -R option. For example, to grant the user alice read and execute permissions on all the files and subdirectories in dir1, you can use the following command:
setfacl -R -m u:alice:rx dir1
For more options and details, you can check the setfacl manual page by typing man setfacl
in your terminal.
Additional Information
You can also use the getfacl command to copy the ACLs of one file or directory to another, by using the --set-file option. For example, to copy the ACLs of file1 to file2, you can use the following command:
getfacl file1 | setfacl --set-file=- file2
The - symbol indicates that the input is taken from the standard input, which is the output of the getfacl command.
You can also use the setfacl command to restore the ACLs from a backup file, by using the --restore option. For example, if you have saved the ACLs of file1 in a file named backup.acl, you can restore them by using the following command:
setfacl --restore=backup.acl
The backup file must have the same format as the output of the getfacl command.
I