In Linux, permissions are a crucial part of the security model. Permissions control who can access files and directories and what they can do with them. There are three types of permissions: read, write, and execute, which are often referred to as the “triad of permissions.” In this tutorial, we will explore what each of these permissions means and how to set them.
Understanding the Triad of Permissions
The read permission allows a user to read the contents of a file or directory. When applied to a file, it means that the user can view the contents of the file, but cannot modify it. When applied to a directory, it means that the user can view the contents of the directory, but cannot add, remove or modify the contents of the directory.
The write permission allows a user to modify the contents of a file or directory. When applied to a file, it means that the user can modify the contents of the file, but cannot view it if the read permission is not set. When applied to a directory, it means that the user can add, remove or modify the contents of the directory.
The execute permission allows a user to execute a file or access the contents of a directory. When applied to a file, it means that the user can execute the file if it is an executable file. When applied to a directory, it means that the user can access the contents of the directory, but cannot view or modify them unless the appropriate read and write permissions are set.
Permissions can be set for three categories of users: owner, group, and others. The owner is the user who created the file or directory, the group is a set of users who share a common set of permissions, and others are all other users on the system.
To set permissions, you can use the
chmod command, which stands for “change mode.” The syntax of the
chmod command is as follows:
chmod [options] mode file
mode argument specifies the permissions you want to set, and the
file argument specifies the file or directory you want to set the permissions for.
The mode argument can be specified in two ways: symbolic mode and numeric mode.
Symbolic mode is a way of specifying permissions using letters and symbols. The letters used are r (read), w (write), and x (execute). The symbols used are + (add permissions), – (remove permissions), and = (set permissions).
To add the read permission for the owner of a file, you can use the following command:
chmod u+r file
To remove the write permission for the group, you can use the following command:
chmod g-w file
To set the execute permission for others and remove all other permissions, you can use the following command:
chmod o=x,g=,u= file
Numeric mode is a way of specifying permissions using numbers. Each permission has a numeric value: read (4), write (2), and execute (1). The sum of these values gives the numeric mode for the file or directory. For example, a file with read and write permissions for the owner and read-only permissions for the group and others would have a numeric mode of 644 (4+2+4+4=14).
To set the read and write permissions for the owner, you can use the following command
chmod 600 file
To set the read, write, and execute permissions for the owner, read and execute permissions for the group, and read-only permissions for others, you can use the following command
chmod 750 directory
To check the permissions of a file or directory, you can use the
ls command with the
-l option. The output of the
ls -l command displays the file or directory permissions, owner, group, size, modification date, and name.
$ ls -l file -rw-r--r-- 1 user group 0 Mar 16 12:00 file
In the example above, the file has read and write permissions for the owner, read-only permissions for the group and others, and is owned by the user “user” and group “group.”
Understanding and managing permissions is essential for maintaining a secure and functional Linux system. By using the
chmod command, you can set the read, write, and execute permissions for files and directories. The
ls command with the
-l option can be used to check the current permissions of a file or directory. With the knowledge gained from this tutorial, you should be able to confidently manage file and directory permissions in Linux.