Linux permissions: making sense of 755 and rwxr-xr-x

Linux permissions: making sense of 755 and rwxr-xr-x

Understanding Linux permissions might seem like a near-impossible task—what does 755 or u=rwx,g=rw,o=r mean, anyway?—but it’s actually easier than you think. Let’s take a look.

Linux is a multi-user operating system that can be accessed by many users simultaneously. This might make you to think that a user can manipulate files and directories of another user, but all Linux operating systems protect filesystems under two levels of authorization—ownership and permission—to prevent unauthorized access to the filesystem in an effective and easy manner.

Owners of files, directories, and processes

Before we try to explore who are the owners of files and directories, let’s get an overview of user types in Linux. In Linux, there are two types of users, system users and regular users. System users are created by the operating system itself and are used to manage background processes.

We generally create regular users to create and run processes interactively through a GUI or terminal. Besides these two types of users, there is a superuser by the name root, which has access to entire system to manage and override any settings in the system.

In Linux, the owners of the files, directories and processes will be assigned to these three types of users: regular, system, or root. Before we try to explore what permissions can be assigned to these three types of users, let’s try to understand the types of permission that are available in Linux.

What Linux permissions types are there?

There are two levels of permissions assigned to the files, directories, and processes in Linux. The first one is permission groups, which is otherwise referred to as the ownership. The second one is permission types, which can be read, write, or execute.

Permission groups

For every file and directory in Linux, there are the sets of users for whom we specify permissions. They are:

  • Owners
  • Groups
  • Others

Owners: The user who creates a file, folder, or process is the owners.

Groups: Groups refers to anyone who is in the same group as the owner.

Others: Any user who is neither the owner of the file/directory and doesn’t belong to the same group is assigned to others group.

Permission types

What operations can each of the above three user groups can do is defined by permission types. There are three basic permission types that can be assigned to three groups of users and they are read (r) , write (w), and execute (x).

What do read, write and execute mean for files and directories ?

For files:

  • Read is the ability to view the contents of a file.
  • Write is the ability to edit or delete a file.
  • Execute is the ability to run a file as an executable program.

For directories:

  • Read is the ability to read the contents of a directory.
  • Write is the ability to write into the directory, like creating files and sub-directories inside a directory.
  • Execute is the ability to cd into the directory and to view the metadata of the files inside the directory using ls command.

How do I find the permissions of a file?

Let’s try to find the permissions of files and directories. To find the permissions that is already assigned to files or directories, use ls command with -l switch.

$ ls -l
drwxr-xr-x  3 dd users   4096 Jun  10 08:01 Pictures
...
...

The first ten characters in the format drwxrwxrwx, represents the permissions for all the three classes of users. Let’s try to understand what each of these letters means. The first character, d, signifies that the file is a directory. This position can be blank(-) or any of the following characters:

c: Character device
b: Block device
s: socket
p: pipe
D: Door
l: symbolic link etc.

Then the next three characters (drwxr-xr-x) represent the permissions that have been assigned to the owners of the file. The owner dd can read, write, and execute to the folder Pictures.

Moving on to the next three characters (drwxr-xr-x), which is r-x, represents the group permissions. The users from users group can access the file according to the group permissions, which specify they can read and execute in the directory but cannot write into it. The hyphen signifies that the permission is not granted.

The last three characters (drwxr-xr-x) represents the permissions for other groups who are neither the owner nor a member of the group users and the permissions are set to read and execute only.

The 11th character is a number that represents the number of hard links for the file and is not related to permission for a file. The two columns next to this number (drwxr-xr-x 3 dd users) represents the owner and group of the file.

To find the permissions for a particular file or directory, specify the name of the file in the ls command like below.

$ ls -l filename

Permissions in numeric notation

Two notations are used to represents the permissions for files and folders. The one that we already came about (r,w,x) is known as symbolic notation. The other one is numeric notation. In this notation, a number (0,1,2,4) represents a permission and are as follows:

  • 0: No permission
  • 1: Execute (x)
  • 2: Write (w)
  • 4: Read ®

Now, how to calculate permissions for users and groups in numeric notation? Just add the permission’s value to get the value of user, group, and other permissions respectively.

For example:

read(4), write(2) and execute(1) permission rwx translated to 7 (4+2+1)
read(4) and write(2) permission rw- translated to 6 (4+2)
write(2) and execute(1) permission -wx translated to 3 (2+1) etc.

Therefore the permission rwxrwxrwx is same as 777, rwxr-xr-x is same as 755, and so on.

Changing Linux permissions using symbolic notation

Using the chmod command, one can add or remove permissions from a file or a directory. The letters u (owner/user), g (group) and o (other) are used to add or remove permissions for each of the three user types along with following three signs.

  • the minus sign (-), which means “remove these permissions”
  • the plus sign (+), which means “add these permissions”
  • the equals sign (=), which means “change the permissions to exactly these”.

Add permissions

To add permissions, use chmod command along with plus sign (+), which means “add these permissions”.

So if you want to add execute permission for all three types of users for a script file, use the following chmod command.

$ chmod +x hello.sh
         OR
$ chmod a+x hello.sh
// 'a' means all

To add execute permission for owner of the file only, use the following chmod command.

$ chmod u+x hello.sh

Similarly, you can use +r to add the read permissions, and +w to add the write permissions.

You may also assign permissions to users, groups and others or by combining them selectively. Just specify the classes of users (u, g, or o) and the permission (r, w, or x) that you want to assign. For example, the following chmod command will add execute and write permission to the owner of the file.

$ chmod u+xw hello.sh

To add write permission to both the owners and groups use the following command.

$ chmod ug+w hello.sh

You can also add permissions for multiple classes of users at one go. The following example will add read, write and execute permission for owner and for the group and others, permission are sets to read and execute.

$ chmod u=rwx,g=rw,o=rw example.txt

Remove permissions

In some situations, you may need to remove permissions rather than to add them. Just change + to - to remove permissions for any of the three classes of users. Below are the few examples that shows how to remove permissions using chmod.

$ chmod g-w readme.txt
// removes write permission for groups

$ chmod ug-x script.sh
// removes execute permission for both owner and groups

$ chmod -R go-rwx test_directory
// removes read, write and execute permission for groups and other users recursively for test_directory including all files and subdirectories inside it.

Changing Linux permissions using numeric notation

You can also set permissions using numeric notation instead of symbolic notation. Permissions set in this way use up to four digits. Now you may ask why 4 digits since there are only three classes of users for which you want to set the permissions. The first digits signifies value for set user id (4) OR set group id (2) OR sticky bit(1). The rest of the three digits are used for setting permission for three classes of users.

It is also possible to set permission using 3 digits only leaving the permission for user id, group id and stick bit unset. So the permission 0755 and 755 are the same.

$ chmod 755 hello.sh
// Sets all permission to owners and read/execute permission to group and others

$ chmod 0755 hello.sh
// Same as 755

$ chmod -R 644 test_directory
// Recursively sets read and write permission to owner, read permission to group and other for the test_directory and all files and subdirectories inside it.

Set user id

If a file with set user ID permission is set, then the file is executed as if by the owner of the file rather than the user who is executing the file. For example, /bin/mount is commonly owned by root and has permissions 4755 where the digit 4 signifies that, even if the file is executed by a normal user, it will run with the owner’s (root’s) privileges since the file is owned by root. The following example will show how to set the suid bit for a file.

$ chmod u+s hello.sh
        OR
$ chmod 4664 hello.sh

// Sets the suid bit of the file hello.sh

$ ls -l hello.sh
-rwSrw-r-- 1 peter peter 0 Jun 13 10:16 hello.sh

// The fourth character in the permission shows the suid bit is set.
// The capital 'S' signifies that executable bit is not set otherwise executable bit will be 's'

Set group id

SGID can be set to both files and directories and is represented symbolically by g and numerically by 2. When a directory has the sgid bit set, any files or directories created inside it will inherit the group ID of the directory. To set the sgid bit for a directory, use the following chmod command.

$ chmod g+s test_directory
          OR
$ chmod 2755 test_directory

Find if the sgid bit is set for the directory using the ls command.

$ ls -ld test_directory/
drwxrwsr-x 3 peter peter 4096 Jun 12 10:30 test_directory/

The seventh character in the group permission section ('s') signifies that the sgid bit is set for groups.

Sticky bit

The next access mode bit is called the sticky bit and is represented symbolically by t and numerically by 1. This bit works on directories only. With sticky bit set on a directory, anyone can create files or directories inside it. Files owned by other users cannot be deleted except his own files and directories.

To add a sticky bit to other types of users, use +t option in the chmod command.

$ chmod o+t some_directory

To test if the sticky bit is set for the directory use the ls command:

$ ls -ld some_directory
drwxrwxr-t 2 peter peter 4096 Jun 12 11:47 some_directory

There will be a t in the x bit section of other users. Also a lowercase t implies that the executable bit is also present, otherwise you would see a capital T

To remove the sticky bit use - sign in the chmod command:

$ chmod o-t some_directory

Using chown to change ownership

There may be situations when you need to change the ownership of files and directories. The chown command as described below changes the owner and groups of files and directories.

$ chown dd hello.sh
// changes the owner of the file only.

To change the group ownership, specify a colon or dot followed by group name right after owner name with no spaces between them, the group ownership of the files is changed as well.

$ chown dd:users hello.sh
             OR
$ chown dd.users hello.sh

If no group name is mentioned after colon or dot followed by OWNER, then the user is made the owner of the files and the group of the files is changed to owners login group.

$ chown dd. hello.sh
      OR
$ chown dd: hello.sh

If the owner name is omitted right before colon or dot and a group name is mentioned afterwards then the group ownership is changed. In this case, chown performs the same function as chgrp.

$ chown .users hello.sh
        OR
$ chown :users hello.sh

To change the owner and group of a directory recursively use -R switch:

 $ chown -R dd:admin some_directory

Now that you have a basic idea of permissions in Linux and its usage through chmod and chown, you can now implement a proper permissions policy to secure your system.

Get 24GB RAM for $9.99/mo!

Save $3,960 with SSD Nodes versus competitors like Digital Ocean or Amazon Lightsail.

Snag limited-time prices: