How Linux permissions works

Linux permissions are an important concept to understand when working with the Linux operating system. They allow you to control who has access to files and directories, and what actions they can perform on them.

To view the permissions on a file or directory, you can use the ls -l command. This will display a list of files and directories, along with their permissions. Here's an example:

$ ls -l
total 8
drwxr-xr-x 2 user1 user1 4096 Jan 1 00:00 directory1
-rw-r--r-- 1 user1 user1   22 Jan 1 00:00 file1

The first column in the output shows the permissions for each file or directory. The first character indicates the type of file or directory. A d indicates a directory, while a - indicates a regular file. The next nine characters are divided into three groups of three. These groups represent the permissions for the owner of the file, the group the file belongs to, and other users, respectively.

Each group of three consists of three characters: r, w, and x. r stands for read permission, w stands for write permission, and x stands for execute permission. If a permission is granted, the corresponding character will be present. If it is not granted, it will be replaced with a -.

Here's a breakdown of what each permission means:

  • r: Allows the user to read the file.
  • w: Allows the user to write to or modify the file.
  • x: Allows the user to execute the file, if it is a program or script.

For example, in the output above, the permissions for directory1 are drwxr-xr-x, which means that the owner has read, write, and execute permissions for the directory. The group and other users have read and execute permissions, but not write permission.

The chmod command can be used to change the permissions on a file or directory. It takes a numeric mode as an argument, which specifies the permissions to set. The numeric mode is made up of three digits, each representing the permissions for the owner, group, and other users, respectively.

For example, to give the owner full permissions (read, write, and execute), the group read and execute permissions, and no permissions to other users, you can use the following command:

$ chmod 750 file1

You can also use symbolic modes with chmod, which use characters to represent the permissions. For example, the following command sets the same permissions as the previous example:

$ chmod u=rwx,g=rx,o= file1

In this case, u stands for the owner, g stands for the group, and o stands for other users. The = operator sets the permissions specified to the given user or group. The , operator separates the permissions for different users or groups.

It's important to understand Linux permissions, as they play a crucial role in ensuring the security and integrity of your system. By setting the appropriate permissions, you can control who has access to sensitive files and prevent unauthorized changes to important system files.