DEV Community

Safitri24
Safitri24

Posted on

use of chmod and chown

Step 1 (Change file and folder permissions via command line)
chmod –>This command is used to change the permissions of a file/folder. Basically, every file can be accessed by 3 types of users namely:
Type Explanation
owner The user who creates and owns the file/directory.
group All users who are members of the same group.
others All other users in the system who are not owners or members of a group.
The command ls -l, can be used to see the permissions on the file and its owner. For example, ls -l file1.txt will output:

Image description

“-rwxrwxr––.“ –> This section will display the permissions. There are 3 letters that you will probably see often: r, w, x, d.
d means the file type is a directory (folder). In our example, the letter d doesn't exist (it should be in the first place, but in the example it doesn't exist/represented by the symbol "-" which means "no"). The letter x means permission to execute files/folders (we often need this permission to enter the folder).
The letter w means permission to write files/folders (edit, delete, etc.). Finally, the letter r means read. If we have permission to read, we can read the contents of the file, but that's about it. We can not edit or execute the file.
1 –>Number of hard links. Usually a hard link is an additional name for a file.
Student -> Displays the owner and group owner of the file.
0 –>Display file size.
Sep 30 21:37 –>Shows the last time the file was modified.
file1.txt –>File/folder name
Back to the chmod command. This command will allow us to change the permissions of the file/folder. We're going to learn how to do this just by including the numbers together. Each permission type has its own number:
r (read) – 4
w (write) – 2
x (execute) – 1

So, for example, if you want to set file1.txt permissions to:

-rwxrwxr––. 1 student student 0 sep 30 21:37 file1.txt

So the command we use is:

chmod 747 file1.txt

Basically, each number in this command represents permissions for one type of user (owner, group owner, etc.).

So, the first number is 7. Based on the explanation of the meaning of the numbers above, the only way we can get the number 7 is by adding the numbers 4, 2 and 1, the form is: 4+2+1=7. This means ALL permissions (read, write and execute – rwx). This first number will create permissions for the owner of the file.
The 2nd number is 4. This means that it is permission r (read), this number gives permission to the group owner.
The 3rd number is 7, based on the meaning of the numbers above, the way to get this number is to add 4 and 2, so 4+2+1=7. So we give others permission to read (4) write (2) execute (1) on the file.
The 3rd part of the command (file1.txt) is the file name, we write the name of the file we want to set the permissions for.

Another example: chmod 777 file2. txt, this command will give ALL permissions to all types of users (owner, group and other).

The following is a list of the most frequently used files:
Value Numeric Value Explanation
-rw——- 600 Owner can read and write.
-rw-r–r– 644 Owner can read and write, group and other people can read.
-rw-rw-rw- 666 Owners, groups and others can read and write.
-rwx—— 700 Owner can read, write and execute, group and others cannot do anything with the file.
-rwx–x–x 711 Owner can read, write and execute, group and other people can execute.
-rwxr-xr-x 755 Owner can read, write and execute, group and others can read and execute.
-rwxrwxrwx 777 Owners, groups and others can read, write and execute.
Commonly used permissions for directories:
Value Numeric Value Explanation
drwx—— 700 Only the owner can read and write in this directory.
drwxr-xr-x 755 Owners, groups and others can read directories, but owners can only change their contents.
There are other ways to change permissions using the chmod command, but we recommend that you learn about one of them and use it all the time (use numbers as above).

Step 2 (Change file and folder owner via command line)
chown –>This command is used to change the owner of the file/folder. The basic command is:
chown [owner/group owner] [filename]
Basically, if we have a “demo.txt” file and we want to make the owner of this file “jamaludin” and the group owner to “clients”, then the command we will use is:
chown jamaludin:clients demo.txt
So, as you can see, we separated the owner and group owner with the symbol “:” (colon). If we only want to change the file owner, we can use:
chown jamaludin demo.txt
We omit the group owner and just type in the new file owner, under such conditions, the group owner will remain unchanged. Another similar example is if we want to change the group owner of a file, the command would be as follows:
chown :clients demo.txt
In this condition, only the group owner will change to clients (the owner remains unchanged).

Step 3 (Using additional options with chmod and chown commands)
One of the main options that work with both commands is -R, which means recursive. This option allows you to change permissions/owners in a folder and ALL files and sub folders in it

IMPORTANT! You have to be careful with this option because if you use it incorrectly you can actually change the permissions/owners of all the files on your system. The effect will cause fatal errors and you will be increasingly bothered to return to normal conditions.

Other options for “chmod” and “chown” are:
“-f” – Will force execution and not give many error messages.
“-v” – Gives you a diagnosis of each file affected by the command.
“-c” – Same as -v, but will only provide information when the change has actually occurred.

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Just a heads up that you can add highlighting to the code blocks if you'd like. Just change:

code block with no colors example

... to specify the language:

code block with colors example

More details in our editor guide!