Shell and DoC Systems, Lab 2
Remote Working, gitlab, and LabTS
Chapter 1: File Permissions
Changing File Permissions
You can change file permissions using the chmod
command (for “change mode”).
This will have the following syntax:
username@MACHINE:~$ chmod [mode] filename.txt
Where [mode]
is either a numeric code, or a symbolic description of the mode change.
Here we’ll use the symbolic version, as it’s easier to remember – feel free to look up the numeric codes if you’d like!
The sybolic description has three parts: reference, operator, and the mode. For example:
g+rw
Here we are adding read and write permissions for members of the file’s group.
The reference indicates which set of users we want to change the permissions for:
u
for user/ownerg
for groupo
for othera
for all (i.e. all of the above)
The operator indicates what kind of change you would like to make to the referenced users:
+
for adding permissions-
for removing permissions=
for overwriting the current permissions
And finally the mode is some combination of:
r
for read permissionsw
for write permissionsx
for execution permissions
So, let’s say we want to add write permissions for everyone for a given file. The command would look something like:
username@MACHINE:~$ chmod a+w filename.txt
Now the permissions of this file should look something like:
-rw-rw-rw-
Let’s say we wanted to remove write permissions for the “other” set of users (the o
reference above). We could run:
username@MACHINE:~$ chmod o-w filename.txt
We could instead have used chmod
to set the permissions explicitly:
username@MACHINE:~$ chmod o=r filename.txt
So there you go, that’s how we can change file permissions using chmod
.
Exercise 1
- Change the working directory to
~/module/shell/
. - Create a file called
hello.txt
, and writeHello from USERNAME
inside it (replacingUSERNAME
with your own username). - Use
ls
to check the permissions on the file. Can you see that you have read and write access to it? - Use
chmod
to remove your write permissions to the file. Which user reference does this require? Which operator(s) can you use for this? - Run
ls
again to check the permissions have changed. - Now try and edit the file with
nano
. What error do you get? - Use
chmod
to give yourself write permissions again, and verify that you can once again edit the file.