How to Work with Linux Permissions

How to Work with Linux Permissions

with No Comments

Reading a file’s permissions

To see a file’s permissions, invoke the following in the Terminal:

ls -l "name of file"

Note: If the file is not in your home directory, you will need to provide the path to the file.

For example, if I create a file with TextEdit, it will generally have the permissions of:

-rw-r--r--

The character at the very beginning tells what type of file it is. In this case, “-” means it is a regular file. Alternatively, “d” would indicate a directory, and “l” a symbolic link.

The next nine characters show the permissions for the User, Group, and Others respectively. In this case, User currently has permissions of rw-, Group has r--, and Others has r--.

The possible characters for each of these is r, w, and x. This represents read, write, and execute. Note that they are also written in that order. When one of these is not permitted the symbol is a hyphen (“-“).

In the case of the TextEdit file above, the following are permitted:

User: read, write
Group: read
Others: read

Changing a file’s permissions

Symbolic Mode

If you invoke the following in the command line, it adds execute permission to all:

chmod a+x "name of file"

Now it appears like this (if you invoke ls -l “name of file” in the command line):

-rwxr-xr-x

User: read, write, execute
Group: read, execute
Others: read, execute

If you invoke the following in the command line, it adds write permission to all:

chmod a+w "name of file"

The command ls -l “name of file” returns this:

-rwxrwxrwx

User: read, write, execute
Group: read, write, execute
Others: read, write, execute

If you use a minus sign, it takes permissions away. For example, the following would take read permission away from Others:

chmod o-r "name of file"

Now it shows this:

-rwxrwx-wx

User: read, write, execute
Group: read, write, execute
Others: write, execute

This would take away write and execute permissions from all:

chmod a-wx "name of file"

Now it looks like this:

-r--r-----

User: read
Group: read
Others: no access

As another example, the following would add write and execute permissions to Group and Others:

chmod go+wx "name of file"

Now it returns this:

-r--rwx-wx

User: read
Group: read, write, execute
Others: write, execute

The symbols are:

  • r = read
  • w = write
  • x = execute
  • u = User
  • g = Group
  • o = Others
  • a = all

Numeric mode

You may choose to set the permissions using the numeric mode. In the numeric mode, three numbers represent the permissions of User, Group, and Others respectively.

For example, chmod 756 “name of file” represents 7 for User, 5 for Group, and 6 for Others. The numbers are determined by taking the values from the following list that add up to each digit:

  • read = 4
  • write = 2
  • execute = 1
  • no access = 0

If we invoke the following in the command line:

chmod 756 "name of file"

We get the following:

-rwxr-xrw-

User: read, write, execute (because 4 + 2 + 1 = 7)
Group: read, execute (because 4 + 1 = 5)
Others: read, write (because 4 + 2 = 6)

Another example:

chmod 777 "name of file"

Will set the permissions to the following:

-rwxrwxrwx

User: read, write, execute (because 4 + 2 + 1 = 7)
Group: read, write, execute (because 4 + 2 + 1 = 7)
Others: read, write, execute (because 4 + 2 + 1 = 7)

An additional example:

chmod 700 "name of file"

Will return the following permissions:

-rwx------

User: read, write, execute (because 4 + 2 + 1 = 7)
Group: no access (because 0 = 0)
Others: no access (because 0 = 0)

A further example:

chmod 755 "name of file"

Will set the permissions to:

-rwxr-xr-x

User: read, write, execute (because 4 + 2 + 1 = 7)
Group: read, execute (because 4 + 1 = 5)
Others: read, execute (because 4 + 1 = 5)

Finally:

chmod 444 "name of file"

Will set the permissions to this:

-r--r--r--

User: read (because 4 = 4)
Group: read (because 4 = 4)
Others: read (because 4 = 4)

Leave a Reply