In this article you can deepen your knowledge about the rights and permissions of your files and folders like chmod and chown commands.. Moreover, you will learn to adjust this under Linux. There are two assignments to perform these tasks chmod and chown commands.
Chmod: how to change the authorizations per command line?
the chmod command you can change the permissions of a file or folder via command line. Each file has three types of users divided as follows:
- owner: This is the user who created the folder. He therefore has all rights.
- group: These are all users who are members of the group
- different: All users who are not owners or members of the group.
How to use the ls command?
Ls is the basic linux command. In fact, it shows the files in your folder flow. By adding the indicator “l“With this command you can see the permissions that are attached to each file on your VPS server.
$ is -l -rw-r - r-- 1 root root 236 2 Dec 2018 install.log -rwxr-xr-x 1 root root 2275 28 September 2018 file2.php drwxr-xr-x 1 root root 4096 May 31, 2018 file10
This is what the ls command shows:
authorization, number of links, user group, size, date, name
- authorization: -rw-r-r-: this first part shows the permissions of the file. There are four letters available, these are the letters r, w, x and d. The letterr“Means”read“This means that you have the option to read the file. The letter w“(write) means that you can write to the file or folder. The “X“Defined that you run the file, and it gives you access to a folder. Finally, the letter”d“Defined that this is a folder.
- number of links: 1: set the number of hard links. A hard link is an extra name for an existing file.
- user group: root root: the first shows the owner of the file and the second the group.
- size: 236: Displays the size of your file
- date: December 2, 2018: Displays the date of the last change to your file or folder.
- name: file2.php: indicates the name of the file or your folder.
The chmod command is certainly the most difficult to use among the 2 present. They, however you can change the permissions of each file or folder according to your needs. For your convenience, each letter has its own number as follows:
- R (read): 4
- W (write): 2
- X (execute): 1
Therefore, if you want to set the permissions of the following file:
-rwxr-xr-x 1 root root 2275 28 September 2018 file2.php
You must execute the following command:
chmod 745 file2.php
In the example above, the first number is 7. To get this number, simply add 4 + 2 + 1 (R + W + X). This means that the file owner can read, write and execute the file. The second number in the example is 4, it is only possible to read the file. The third and last number is 5 (4 + 1), it is possible to read and execute the file.
How to change the owners of a file or folder via the command line?
Now that you can view the permissions, but you can also use the chmod command to change the permissions of a file or folder, the chown command follows here. This command is used to change the owner of a file. The syntax of this command is as follows:
owner chown: group file.php
For example if you want change the owner of the file “file.php” in “goodies“And the owner of the group in”habit“, You can use the command as follows:
chown clement: custom file.php
As you can see, the owner of the file is separated from the owner of the group by a double dot. You can only change the owner of the file without changing the owner of the group with the following syntax:
chown clement file.php
That is possible in the same spirit change the owner of the group without changing the owner of the file via the following command line:
chown: custom file.php
These commands make managing your server easier 👍
Feel free to share your knowledge in the field and some orders that you use with us 😉