Permissions on Linux are applied to files by three main groups of three attributes:
r
indicates the ability to read the contents of the filew
indicates the ability to write to or modify the filex
indicates the ability to execute the file (i.e. to run it as a script or an application)
In addition, a leading single attribute indicates whether a file is a directory or a normal file. A typical file on Linux may show the following permissions when you use ls -l
:
$ ls -l myfile.txt
-r-------- 1 n1234 1999 67 Mar 31 12:35 myfile.txt
We can change our own permissions to set a file from read-only to be read/write (or vice-versa). To do this we use the chmod
command and pass the u+
option (for user or owner of the file) to add a permission.
Here we change a file from read-only, to read-write by adding the write permission:
$ chmod u+w myfile.txt
$ ls -l myfile.txt
-rw------- 1 n1234 1999 67 Mar 31 12:35 myfile.txt
$
Notice that the first triplet of permissions now shows rw-, indicating that we, as owner of the file can now read and write to it.
We can use the u-
option to remove a permission as well. The normal available permissions are, of course r
for read, w
for write and x
for execute as a programme.
Most files you create on the HPC facility will default to rw-
for yourself. But there may be times when you need to add/remove those permissions.
Allowing others in the group mygroup to read the contents of myfile.txt
we used the chmod
command (change mode) and pass the g+
option (for group) to add a permission:
$ chmod g+r myfile.txt
$ ls -l myfile.txt
-rw-r----- 1 n1234 mygroup 67 Mar 31 12:35 myfile.txt
$
Note that the second triplet is now r–. Now assume we also want others in mygroup to be able to write to the file as well:
$ chmod g+w myfile.txt
$ ls -l myfile.txt
-rw-rw---- 1 n1234 mygroup 67 Mar 31 12:35 myfile.txt
$
Note that the second triplet is now rw-.
We can also use the g-
option to remove a group permission:
$ chmod g-w myfile.txt
$ ls -l myfile.txt
-rw-r----- 1 n1234 mygroup 67 Mar 31 12:35 myfile.txt
$
The file is now read-only for everyone in mygroup again.
If we want to change the permissions for everyone else (outside of the current group), we can use the o+
flag (meaning others) to add a permission:
$ chmod o+r myfile.txt
$ ls -l myfile.txt
-rw-r--r-- 1 n1234 mygroup 67 Mar 31 12:35 myfile.txt
$
Note how in the example above we have allowed read permission for the third field (r–), which means all other users outside our group can read the file.
The o-
flag can also be used to remove a permission from everyone else.
Tip - Shortcuts to set permissions
We can add or remove permissions for all categories of user (u
- user, g
- group, and o
- other) simultaneously by omitting the u
, g
or o
flag. For example remove write permissions for everyone on the file file.txt
by using -w
with no prefix:
$ chmod -w file.txt
Permissions on Linux are applied to directories by the same three main groups of three attributes as used for files. But, their meaning is subtly different:
r
indicates the ability to list the contents of the directory (i.e. via ls
)w
indicates the ability to create new files in the directoryx
indicates the ability to enter or access the directory as part of a path, e.g. with cd
(note that accessing a directory and listing directory contents are two different permissions!)Note that directory permissions are strictly related to the directory itself - they do not imply any permissions on the files contained within that directory; file permissions are set on the files themselves.
This gives some potentially odd scenarios, including (but not restricted to):
ls
, but not being able to cd
into it!cd
to a directory, but not being able to list the contents using ls
!
Assume a directory containing two folders; mydir
and private
:
$ ls -l
drwx------ 2 n1234 mygroup 4096 Mar 21 12:32 mydir
drwx------ 2 n1234 mygroup 4096 Jun 21 2024 private
$
In the example above:
mydir
has permissions rwx for the owner, so they can list contents (r), create new files (w), as well as access the directory in the path (x)mydir
has permissions - - - for the group, so group members can do nothing with that directory, or any sub-directories of it.mydir
has permissions - - - for all others group, so everyone can do nothing with that directory, or any sub-directories of it.How do I let others in my groupcd
into my directory?
The directory and all directories above it must have +x
set for the group:
$ chmod g+x mydir
drwx--x--- 2 n1234 mygroup 4096 Mar 31 13:24 mydir
$
How do I let others in my group list the files in my directory usingls
?
The directory must have +r
set for the group. You probably also want the group to be able to cd
to it, so add the +x
permission as well:
$ chmod g+x mydir
$ chmod g+r mydir
drwxr-x--- 2 n1234 mygroup 4096 Mar 31 13:24 mydir
$
How do I let others in my group create new files or subfolders in my directory?
Add the w
permission, which allows the creation of new files (and directories):
$ chmod g+x mydir
$ chmod g+r mydir
$ chmod g+w mydir
drwxrwx--- 2 n1234 mygroup 4096 Mar 31 13:24 mydir
$
Note that in the above example, all members of mygroup can access, list the contents of, and create new files in mydir
, with the exact same permissions as the owner, n1234. This is often a common request for shared, project working areas.