What is setfacl in linux

setfacl (1) — Linux Man Pages

setfacl: set file access control lists

Command to display setfacl manual in Linux: $ man 1 setfacl

setfacl — set file access control lists

SYNOPSIS

setfacl [-bkndRLPvh] [ <-m|-x>acl_spec] [ <-M|-X>acl_file] file .

DESCRIPTION

The -m and -x options expect an ACL on the command line. Multiple ACL entries are separated by comma characters (`,’). The -M and -X options read an ACL from a file or from standard input. The ACL entry format is described in Section ACL ENTRIES.

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 POSIXLY_CORRECT is defined.

When reading from files using the -M and -X options, setfacl accepts the output getfacl produces. 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.

PERMISSIONS


OPTIONS


ACL ENTRIES

[d[efault]:] [u[ser]:] uid [: perms ] Permissions of a named user. Permissions of the file owner if uid is empty. [d[efault]:] g[roup]: gid [: perms ] Permissions of a named group. 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.

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. Character literals may be specified with a backslash followed by the 3-digit octal digits corresponding to the ASCII code for the character (e.g., \101 for ‘A’). If the name contains a literal backslash followed by 3 digits, the backslash must be escaped (i.e., \\ ).

The perms field is a combination of characters that indicate the read (r) , write (w) , execute (x) permissions. Dash characters in the perms field (-) are ignored. The character X stands for the execute permission if the file is a directory or already has execute permission for some user. Alternatively, the perms field can define the permissions numerically, as a bit-wise combination of read (4) , write (2) , and execute (1) . Zero perms fields or perms fields that only consist of dashes indicate no permissions.

AUTOMATICALLY CREATED ENTRIES

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 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).

Читайте также:  Windows server нет связи с сервером

EXAMPLES

Granting an additional user read access setfacl -m u:lisa:r file

Revoking write access from all groups and all named users (using the effective rights mask) setfacl -m m::rx file

Removing a named group entry from a file’s ACL setfacl -x g:staff file

Copying the ACL of one file to another getfacl file1 | setfacl —set-file=- file2

Copying the access ACL into the Default ACL getfacl —access dir | setfacl -d -M- dir

CONFORMANCE TO POSIX 1003.1e DRAFT STANDARD 17


AUTHOR

Please send your bug reports, suggested features and comments to the above address.

Источник

How to Manage Linux ACL using Setfacl and Getfacl

The Linux command setfacl allows users to set extensive Access Control Lists on files and directories. Normally, using chmod command, you will be able to set permissions for the owner/group/others. But, in case you may need to provide file permissions for some other users too, that can’t be done using chmod. Setfacl will assist you to get rid of such troubles.

For example, we cannot set up different permission sets for different users on same directory or file. Thus, Access Control Lists (ACLs) were implemented. You can view the current ACL set on files and directories using getfacl command.

In order to use setfacl on a file/directory, the residing filesystem should have acl support enabled. If the filesystem doesn’t support acl, you will get “operation not supported” error. In that case, you need to add acl support to the filesystem in /etc/fstab as follows and then remount the filesystem.

Check if Kernel has ACL Support

Run the following command to check for ACL Support for file system and POSIX_ACL=Y option (if there is N instead of Y, then it means Kernel doesn’t support ACL and needs to be recompiled).

Checking if the filesystem supports ACL

You can try this with:

On some systems looking at fstab will not show you if the filesystem supports acl, but rather display just «defaults» as the mount options:

In that case, you can use the following command to check if the filesystem is mounted with acl support:

If your filesystem has not been mounted with the ‘acl’ option, you can re-mount it giving the needed option:

Check for Required Packages

To use Linux ACLs make sure that you have required packages installed. Below are the required packages that need to be installed using yum or apt-get.

For RedHat based systems:

For Debian based:

Now we can go through various usages of “setfacl” command. Firstly, we should create a folder called “test_folder” as root user.

1. Providing ACL for an individual User

Suppose, you want to give full access to the user “test” (it can be any user at all) on the directory “test_folder”. This can be done using setfacl as follows.

2. Providing ACL for all users of a group

If you want to provide write access permission for all the users of the group “testg” to the folder “test_folder”, you can do it the following way.

3. Revoking acl of a user/group

If you want to revoke the permissions that we’ve given for the user test and the group testg, you can use setfacl command as follows.

4. Copying ACL of one file/directory to another

Suppose, you want to have the same ACL set of test_folder on test_folder1 too, you can set it by copying the ACL as follows.

Conclusion

In this tutorial, we have seen the basic usage of getfacl and setfacl tools for Access Control Lists to set and revoke some permissions to test_folder. We also learned how to check for Kernel and filesystem acl support and how to install the required packages. If you have any thoughts or comments about linux acl, please write it down in the comments section below.

Источник

Learn the Linux Command ‘setfacl’

Control access to your files and folder

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, etc.).

  • The options -m, and -x expect an ACL on the command line. Multiple ACL entries are separated by comma characters (`,’). The options -M, and -X read an ACL from a file or from standard input. The ACL entry format is described in Section ACL ENTRIES.
  • 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. Only ACL entries without the perms field are accepted as parameters unless POSIXLY_CORRECT is defined.
Читайте также:  Проблемные обновления windows 10 2020 года

When reading from files using the -M, and -X options, setfacl accepts the output getfacl produces. 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 that 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.

Using Setfacl

First, try setting up a basic list for a folder in your home directory. Since you’re trying this out for the first time, make a new one to play with. This way, you won’t risk locking yourself out of important documents.

Next, use setfacl to grant another user on your system access to the folder. Setfacl uses the -m flag to modify the access control lists for a file. This usually means adding a user or group.

The u before the first colon tells the command you’re talking about a username. The rwx following the second one grants read, write, and execute permissions.

The same works for groups by specifying g instead of u before the first colon.

It works the same as the previous example, except that it grants permissions to the entire group.

The -x flag works the opposite of -m. It will revoke access to the specified user or group.

The command revokes write access to the directory to the user.

You can also directly modify the permissions for all current users. This includes the owner of the file and any additional users that were granted permissions. This is something of a shortcut, using the existing command but omitting the username.

This grants all existing users read and execute permissions but removes write access.

The same works with the -x flag and removing permissions.

This is essentially the same as the previous example, inverted.

setfacl also lets you copy the access control lists from one file to another. Make sure that your user has full access to the test folder, and create two new files to work with.

Now, modify the permissions of the first file.

Now, you can pipe the results of getfacl, the command for retrieving the current ACL info, into setfacl.

The second fill will have the same permissions as the first.

Finally, you can remove all extended ACL entries created by the command. This will leave the file with the user and group permissions that it originally had. To do this, use the -b flag.

Those are the most common uses of setfacl. If you want to dive deeper, the next portion of this guide is a complete technical breakdown of the command.

Technical Breakdown of Setfacl

setfacl [-bkndRLPvh] [ <-m|-x>acl_spec] [ <-M|-X>acl_file] file .

setfacl —restore=file

Permissions

The file owner and processes capable of CAP_FOWNER are granted the right to modify ACLs of a file. This 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.)

Options

-b, —remove-all

  • Remove all extended ACL entries. The base ACL entries of the owner, group, and others are retained.

-k, —remove-default

  • Remove the Default ACL. If no Default ACL exists, no warnings are issued.
  • Do not recalculate the effective rights mask. The default behavior of setfacl is to recalculate the ACL mask entry unless a mask entry was explicitly given. The mask entry is set to the union of all permissions of the owning group, and all named user and group entries. (These are exactly the entries affected by the mask entry).
  • Do recalculate the effective rights mask, even if an ACL mask entry was explicitly given. (See the -n option.)

-d, —default

  • All operations apply to the Default ACL. Regular ACL entries in the input set are promoted to Default ACL entries. Default ACL entries in the input set are discarded. (A warning is issued if that happens).
Читайте также:  Проверка загруженных драйверов windows

—restore=file

  • Restore a permission backup created by `getfacl -R’ or similar. All permissions of a complete directory subtree are restored using this mechanism. If the input contains owner comments or group comments, and setfacl is run by root, the owner and owning group of all files are restored as well. This option cannot be mixed with other options except `—test’.
  • Test mode. Instead of changing the ACLs of any files, the resulting ACLs are listed.

-R, —recursive

  • Apply operations to all files and directories recursively. This option cannot be mixed with `—restore’.

-L, —logical

  • Logical walk, follow symbolic links. The default behavior is to follow symbolic link arguments and to skip symbolic links encountered in subdirectories. This option cannot be mixed with `—restore’.

-P, —physical

  • Physical walk, skip all symbolic links. This also skips symbolic link arguments. This option cannot be mixed with `—restore’.

—version

  • Print the version of setfacl and exit.
  • Print help explaining the command line options.

End of command line options. All remaining parameters are interpreted as file names, even if they start with a dash.

If the file name parameter is a single dash, setfacl reads a list of files from standard input.

ACL Entries

The setfacl utility recognizes the following ACL entry formats:

  • Permissions of a named user. Permissions of the file owner if uid is empty.
  • Permissions of a named group. Permissions of the owning group if gid is empty.

[d[efault]:] m[ask][:] [:perms]

[d[efault]:] o[ther][:] [:perms]

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), execute only if the file is a directory or already has execute permission for some user(X). Alternatively, the perms field can be 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 in order 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 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 -noption description).

Examples

  • Granting an additional user read access
  • setfacl -m u:lisa:r file
  • Revoking write access from all groups and all named users (using the effective rights mask)
  • setfacl -m m::rx file
  • Removing a named group entry from a file’s ACL
  • setfacl -x g:staff file
  • Copying the ACL of one file to another
  • getfacl file1 | setfacl —set-file=- file2
  • Copying the access ACL into the Default ACL
  • getfacl -a dir | setfacl -d -M- dir

Conformance to Posix 1003.1e Draft Standard 17

If the environment variable POSIXLY_CORRECT is defined, the default behavior of setfacl changes as follows: All non-standard options are disabled. The «default:» prefix is disabled. The -x and -X options also accept permission fields (and ignore them).

Источник

Оцените статью