This page was translated by an AI (LLM) with a cursory human check and is awaiting full review.
Managing file access rights
Unix rights
Unix rights allow you to give different access rights in reading, writing and/or execution to the following categories:
- to the file owner (
user), - to users belonging to the same Unix group as the file owner (
group), - to all other users (
other).
Simply use the chmod command to modify Unix rights (for more information, see man chmod).
Most of the time, Unix rights will be sufficient. If you need to control access to your data more finely, for example to open access to a directory to only one other user (regardless of their Unix group), you will need to use anACL (Access Control List).
Access Control List (ACL)
It is possible to create an Access Control List (ACL) on a directory or file to open access in reading, writing and/or execution to any other user or group.
Conversely, an ACL can also be used to impose access restrictions on certain files or directories using masks specifying "maximum rights" that can be assigned (see section Dependencies between ACL and Unix rights (for advanced users)).
Viewing ACLs
The getfacl command (for more information, see man getfacl)
gives a detailed display of the ACLs set on a given file or
directory:
getfacl <file|directory>
- Default ACL set on a file
- Default ACL set on a directory
$ getfacl myfile
# file: myfile
# owner: user1
# group: group1
user::rw-
group::r--
other::---
$ getfacl mydirectory
# file: mydirectory
# owner: user1
# group: group1
user::rwx
group::r-x
other::---
The rights rwx have the same meanings as the classic Unix rights rwx.
The goal is to open access to a file or directory beyond the current owner and owner group by enriching the associated ACLs. We explain how to do this in the Managing ACLs section.
- Example of enriched ACL set on a file
- Example of enriched ACL set on a directory
$ getfacl myfile
# file: myfile
# owner: user1
# group: group1
user::rw-
user:user2:rw-
user:user3:r--
user:user4:r--
group::r--
group:group2:r--
group:group3:r--
mask::rw-
other::r--
$ getfacl mydirectory
# file: mydirectory
# owner: user1
# group: group1
user::rwx
user:user2:r-x
user:user3:r-x
user:user4:r-x
group::r-x
group:group2:rwx
group:group3:r-x
mask::rwx
other::r-x
The -l option of the ls command allows you to display the classic Unix rights but also to see if ACLs are set. In this case, a
sign + appears just after the Unix rights.
$ ls -ld repertoire_avec_acl repertoire_sans_acl
drwxr-x---+ 2 user1 group1 8192 2014-03-29 11:00 repertoire_avec_acl
drwxr-x--- 2 user1 group1 8192 2014-03-29 11:00 repertoire_sans_acl
$ ls -l fichier_avec_acl fichier_sans_acl
-rwx------+ 2 user1 group1 8192 2014-03-29 11:00 fichier_avec_acl
-rwx------ 2 user1 group1 8192 2014-03-29 11:00 fichier_sans_acl
Managing ACLs
If you wish to give access rights to a specific user and/or group, you can define an ACL (Access Control List)
via the setfacl command on the desired file or directory.
A presentation of this command is provided below. For more details, you can type the command man setfacl.
The general syntax is as follows:
setfacl <ACL_options> <file|directory>
where <ACL_options> can have one of the following forms:
-
to create or replace an existing ACL:
--set=<arguments> -
to modify an existing ACL:
-m <arguments> -
to delete an existing ACL:
-b
The list of possible <arguments> fields is as follows:
u(as user) WITHOUT mention of the user corresponds to the rights given to the owner of the file or directory:u::[r|-][w|-][x|-];noteFor a directory (or an executable), we advise you to set
u::rwxand for a fileu::rw-.dangerWithout these rights, the owner can no longer access the file or directory. They will then be blocked by the ACLs even if the Unix rights are well set.
g(as group) WITHOUT mention of group corresponds to the rights given to the owner group of the file:g::[r|-][w|-][x|-];o(as other) WITHOUT any mention corresponds to the rights given to users who are not mentioned in theuandgfields:o::[r|-][w|-][x|-];u(as user) WITH mention of the user corresponds to the rights given to a particular user (hereuserX) :u:userX:[r|-][w|-][x|-];g(as group) WITH mention of the group corresponds to the rights assigned to all users of the specified Unix group (heregroupX) :g:groupX:[r|-][w|-][x|-];m(as mask) defines the maximum (or effective) rights of the users concerned by theu:userX:...and/org:groupX:...fields.importantIt is advisable to:
- fill in this field yourself each time you modify an ACL to avoid a default value being defined that is not consistent with your needs;
- give the mask the highest rights (
m::rwx) to not restrict the rights given to a user and/or a group.
warningIf the mask field is empty (
m::---), the access rights opened thanks to theufields with mention of the user orgfields with mention of the group are inoperative. See the Dependencies between ACL and Unix rights (for advanced users) section for more details.
To set a new ACL with the setfacl --set=<argument> command, the first three fields u without mention of the user, g without mention of the group and o are mandatory.
For example, the following command gives all rights to the owner (u::rwx) on the directory mon_repertoire, allows members of the owner group to consult the contents of the directory in read-only mode (g::r-x), and closes access to other users (o::---) :
setfacl --set=u::rwx,g::r-x,o::--- mon_repertoire
Access to subdirectories and files contained in mon_repertoire remains controlled by Unix rights. See the Tips on using ACLs section for more details on this.
When you set ACL rights on a directory (for example $WORK/sub_dir/shared_dir/) or a file (for example $WORK/sub_dir/shared_file.h5), you must also set the ACL rights allowing you to traverse each of the directories that make up the access path to this directory or file (for this example $WORK/ and sub_dir/). Without this, access will be denied.
Using the setfacl command with these three fields only gives the same permissions as the chmod command and is therefore of little interest. To broaden access rights, you need to add the u fields with mention of the user, g fields with mention of the group and/or the m field.
Adding a user
For example, if user1 wants to give read and write access rights to the user user2 on their directory DIR_user1, they must use the following setfacl command:
[user1@hostname:~]$ cd $DIR_user1
[user1@hostname:~]$ setfacl --set=u::rwx,u:user2:rwx,g::r-x,o::---,m::rwx .
They can then check the ACLs set with the
getfacl command:
[user1@hostname:~]$ cd $DIR_user1
[user1@hostname:~]$ getfacl .
# file: .
# owner: user1
# group: group1
user::rwx
user:user2:rwx
group::r-x
mask::rwx
other::---
Reading the output:
- with the
u::rwxfield, the owneruser1has the rightsrwxon their directoryDIR_user1; - with the
g::r-xfield, users belonging to the owner group (group1) have the rightsr-x: they can therefore traverse the directory and read its contents but not write to it; - with the
o::---field, no other user has any rights on this directory; - with the
u:user2:rwxfield,user1adds the rightsrwxonly for the useruser2: they can therefore read and write in the directoryDIR_user1; - with the
m::rwxfield: the maximum rights allowed for users outside the owner group arerwx.
Adding a group
For example, if user1 wants to give specific access rights to
the group group3 on their directory DIR_user1, they must use the
following setfacl command:
[user1@hostname:~]$ cd $DIR_user1
[user1@hostname:~]$ setfacl --set=u::rwx,g::r-x,g:group3:r-x,o::---,m::rwx .
They can then check the ACLs set with the
getfacl command:
[user1@hostname:~]$ cd $DIR_user1
[user1@hostname:~]$ getfacl .
# file: .
# owner: user1
# group: group1
user::rwx
group::r-x
group:group3:r-x
mask::rwx
other::---
Reading the output:
- with the
u::rwxfield, the owneruser1has the rightsrwxon their directoryDIR_user1; - with the
g::r-xfield, users belonging to the owner group (group1) have the rights'' r-x'' : they can therefore traverse the directory and read its contents but not write to it; - with the
o::---field, no other user has any rights on this directory; - with the
g:group3:r-xfield,user1adds the rightsr-xfor users belonging to the groupgroup3: they can therefore traverse the directory and see its contents but not write to it; - with the
m::rwxfield: the maximum rights allowed for users outside the owner group arerwx.