Chapter 1: File Permissions

Changing File Permissions

face Josiah Wang Tom Crossland

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/owner
  • g for group
  • o for other
  • a 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 permissions
  • w for write permissions
  • x 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

  1. Change the working directory to ~/module/shell/.
  2. Create a file called hello.txt, and write Hello from USERNAME inside it (replacing USERNAME with your own username).
  3. Use ls to check the permissions on the file. Can you see that you have read and write access to it?
  4. Use chmod to remove your write permissions to the file. Which user reference does this require? Which operator(s) can you use for this?
  5. Run ls again to check the permissions have changed.
  6. Now try and edit the file with nano. What error do you get?
  7. Use chmod to give yourself write permissions again, and verify that you can once again edit the file.