In this article we will show you how to create a user, then give to that new user a read-only permissions (for example if you need a log viewer user)
- create user
#useradd username
- give to that user a password
#passwd username
- use ‘setfacl’ to give the permission
#setfacl -R -m u:username:r-x /dir/sub_dir1/.../
setfacl syntax
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
Description of SETFACL
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.
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.
-n, –no-mask 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).
–mask 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).
–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, setfacl attempts to restore the owner and owning group. If the input contains flags comments (which define the setuid, setgid, and sticky bits), setfacl sets those three bits accordingly; otherwise, it clears them. This option cannot be mixed with other options except “–test”.
–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 to directories. The default behavior is to follow symbolic link arguments, and skip symbolic links encountered in subdirectories. Only effective in combination with -R. This option cannot be mixed with “–restore”.
-P, –physical “Physical walk”: do not follow symbolic links to directories. This also skips symbolic link arguments. Only effective in combination with -R. This option cannot be mixed with “–restore”.
-v, –version Print the version of setfacl, and exit.
-h, –help Print a help message explaining the command line options.
— A double-dash marks the end of command line options; all remaining parameters are interpreted as file names. This option is especially useful for file names that start with a dash.
– If the file name parameter is a single dash, setfacl reads a list of files from standard input.
ACL Entries
setfacl recognizes the following ACL entry formats (spaces in the following formats are optional, but have been included for legibility):
[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.
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 can be an octal digit (“0”-“7”).
Related Articles: Linux setfacl command | Setting filesystem ACL