Home » 2014 » October » 17 » SETFACL command in Linux to set file access control list

10:18 PM
SETFACL command in Linux to set file access control list

SETFACL - set file access control lists or set special permissions to a file or directory.

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

       setfacl --restore=file


DESCRIPTION
       This  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, ...).

       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.


Get the usage or help for the command:

shanky@shankysportal:/home/shanky/test:> setfacl --help
setfacl 2.2.41 -- set file access control lists
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
  -m, --modify=acl             modify the current ACL(s) of file(s)
  -M, --modify-file=file      read ACL entries to modify from file
  -x, --remove=acl             remove entries from the ACL(s) of file(s)
  -X, --remove-file=file      read ACL entries to remove from file
  -b, --remove-all               remove all extended ACL entries
  -k, --remove-default       remove the default ACL
      --set=acl                       set the ACL of file(s), replacing the current ACL
      --set-file=file                read ACL entries to set from file
      --mask                         do recalculate the effective rights mask
  -n, --no-mask                 don't recalculate the effective rights mask
  -d, --default                    operations apply to the default ACL
  -R, --recursive               recurse into subdirectories
  -L, --logical                    logical walk, follow symbolic links
  -P, --physical                 physical walk, do not follow symbolic links
      --restore=file             restore ACLs (inverse of `getfacl -R')
      --test                           test mode (ACLs are not modified)
      --version                    print version and exit
      --help                         this help text

 


Examples:

shanky@localhost:/home/shanky/test:> ll testFile
-rw-rw-r-- 1 shanky somegroup 174 2014-05-12 12:58 testFile

To get file access control list of a file or directory, use getfacl:

shanky@localhost:/home/shanky/test:> getfacl testFile
# file: testFile
# owner: shanky
# group: somegroup
user::rw-
group::rw-
other::r--

The below command will add read permission to user sonu on the file testFile

shanky@localhost:/home/shanky/test:> setfacl -m u:sonu:r testFile
shanky@localhost:/home/shanky/test:>

shanky@localhost:/home/shanky/test:> getfacl testFile
# file: testFile
# owner: shanky
# group: somegroup
user::rw-
user:sonu:r--
group::rw-
mask::rw-
other::r--

shanky@localhost:/home/shanky/test:> ll testFile
-rw-rw-r--+ 1 shanky somegroup 174 2014-05-12 12:58 testFile

Note:- Notice the extra "+" symbol in the long listing of the file testFile


Removing a named group entry from a file's ACL

The below command will remove the group name somegroup from the ACL of the file testFile

shanky@localhost:/home/shanky/test:> setfacl -x g:somegroup testFile

Copying the ACL of one file to another:

Below command will copy the ACL setting of file1 to the file2

 shanky@shankysportal:/home/shanky/test:> getfacl testFile
# file: testFile
# owner: shanky
# group: somegroup
user::rw-
user:sonu:r--
group::rw-
mask::rw-
other::r--

shanky@shankysportal:/home/shanky/test:> getfacl testFile | setfacl --set-file=- newfile
shanky@shankysportal:/home/shanky/test:>
shanky@shankysportal:/home/shanky/test:> getfacl newfile
# file: newfile
# owner: shanky
# group: othergroup
user::rw-
user:sonu:r--
group::rw-
mask::rw-
other::r--

 You may notice that file newfile is having the same setting as the ACL of testFile

 
 

Category: Open System-Linux | Views: 1801 | Added by: shanky | Tags: setfacl command with examples, setfacl, setfacl examples, setfacl command in linux | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-12-03][Open System-Linux]
GREP command in Linux to search for a pattern
[2015-01-18][Open System-Linux]
The JAR archiving tool in Linux
[2014-03-12][Open System-Linux]
uptime command in Linux
[2015-07-12][Open System-Linux]
EGREP or extended grep in Linux to search patterns
[2014-03-25][Open System-Linux]
Create a new user in Linux system: useradd

Total comments: 0
ComForm">
avatar