Linux file permissions are a mechanism that allows you to specify who can access, modify, or execute your files and directories on your system. This is an essential feature for ensuring the security and privacy of your data, as well as the proper functioning of your applications and services.
In this blog post, we will explain the following topics:
- How to view and interpret file permissions using the
ls
command - How to change file permissions using the
chmod
command - How to use symbolic and octal notation to specify file permissions
- How to apply file permissions recursively to subdirectories and files
- How to View and Interpret File Permissions
To view the file permissions of a file or a directory, you can use the ls -l
command, which lists the files and directories in a long format. For example, if you run the command on your home directory, you may see something like this:
$ ls -l
total 12
drwxr-xr-x 2 user user 4096 Apr 30 10:10 Documents
-rw-r--r-- 1 user user 12 Apr 30 10:10 file.txt
lrwxrwxrwx 1 user user 8 Apr 30 10:10 link.txt -> file.txt
The first column of the output shows the file permissions, followed by the number of links, the owner, the group, the size, the date and time of the last modification, and the file name.
The file permissions consist of 10 characters, which can be divided into four parts:
The first character indicates the file type. It can be one of the following:
-
: a regular filed
: a directoryl
: a symbolic linkc
: a character device fileb
: a block device files
: a socket filep
: a named pipe file
The next three characters show the permissions for the file owner. They can be one of the following:
r
: the owner has read permissionw
: the owner has write permissionx
: the owner has execute permission-
: the owner has no permission
The next three characters show the permissions for the file group. They have the same meaning as the owner permissions.
The last three characters show the permissions for the others (everyone else). They have the same meaning as the owner and group permissions.
For example, in the output above, the file file.txt
has the following permissions:
-
: it is a regular filerw-
: the owner can read and write the file, but not execute itr--
: the group can only read the file, but not write or execute itr--
: the others can only read the file, but not write or execute it
The directory Documents
has the following permissions:
d
: it is a directoryrwx
: the owner can read, write, and execute (enter) the directoryr-x
: the group can read and execute (enter) the directory, but not write to itr-x
: the others can read and execute (enter) the directory, but not write to it
The symbolic link link.txt
has the following permissions:
l
: it is a symbolic linkrwx
: the owner can read, write, and execute the linkrwx
: the group can read, write, and execute the linkrwx
: the others can read, write, and execute the link
Note that the permissions of a symbolic link do not affect the access to the file or directory it points to. The permissions of the target file or directory are used instead.
How to Change File Permissions
To change the file permissions of a file or a directory, you can use the chmod
command, which stands for change mode. The syntax of the command is:
chmod [options] mode file
where mode
is the new set of permissions, and file
is the name of the file or directory. You can also specify multiple files or directories, separated by spaces.
There are two ways to specify the mode: symbolic notation and octal notation. We will explain both methods in the following sections.
Symbolic Notation
Symbolic notation allows you to modify the file permissions by using symbols to represent the classes of users and the types of permissions. The general format of the mode is:
[who][operator][permissions]
where:
who
is one or more characters that specify the class of users to modify. It can be one of the following:u
: the owner of the fileg
: the group of the fileo
: the othersa
: all users (equivalent tougo
)
operator
is one character that specifies how to modify the permissions. It can be one of the following:+
: add the specified permissions to the existing permissions-
: remove the specified permissions from the existing permissions=
: set the specified permissions and clear the others
permissions
is one or more characters that specify the type of permissions to modify. It can be one of the following:r
: the read permissionw
: the write permissionx
: the execute permissions
: the setuid or setgid permission (explained later)t
: the sticky bit permission (explained later)
You can also use commas to separate multiple modes, which will be applied in order. For example, the mode u+r,g+w,o-rx
means:
- Add the read permission to the owner
- Add the write permission to the group
- Remove the read and execute permissions from the others
Here are some examples of using the chmod
command with symbolic notation:
To add the execute permission to the owner of the file
file.txt
, use the following command:chmod u+x file.txt
To remove the write permission from the group and the others of the directory
Documents
, use the following command:chmod go-w Documents
To set the read and write permissions for the owner, and the read permission for the group and the others of the file
file.txt
, use the following command:chmod u=rw,go=r file.txt
Octal Notation
Octal notation allows you to modify the file permissions by using numbers to represent the permissions for each class of users. The general format of the mode is:
[owner][group][others]
where:
owner
is a number that specifies the permissions for the owner of the filegroup
is a number that specifies the permissions for the group of the fileothers
is a number that specifies the permissions for the others
Each number is a combination of the following values:
4
: the read permission2
: the write permission1
: the execute permission0
: no permission
To get the number for each class of users, you need to add the values of the permissions you want to set. For example, the number 7
means read, write, and execute permissions, because 4 + 2 + 1 = 7
. The number 5
means read and execute permissions, because 4 + 1 = 5
. The number 0
means no permissions.
Here are some examples of using the chmod
command with octal notation:
To set the read, write, and execute permissions for the owner, and the read and execute permissions for the group and the others of the file
file.txt
, use the following command:chmod 755 file.txt
To set the read and write permissions for the owner, and the read permission for the group and the others of the directory
Documents
, use the following command:chmod 644 Documents
To remove all permissions from the others of the file
file.txt
, use the following command:chmod 700 file.txt
How to Apply File Permissions Recursively
By default, the chmod
command only changes the permissions of the specified file or directory. If you want to change the permissions of a directory and all its subdirectories and files, you need to use the -R
option, which stands for recursive. For example, to set the read, write, and execute permissions for the owner, and the read and execute permissions for the group and the others of the directory Documents
and all its contents, use the following command:
chmod -R 755 Documents
Be careful when using the recursive option, as it can affect a large number of files and directories, and potentially cause unwanted changes or errors.
Comments
Post a Comment