Read Also
In this article, you will learn how to change permissions and owner of files/folders via command line on Linux/Unix systems. There are 2 basic commands you can use to do this: chmod and chown.
Step 1 – Change file and folder permissions via command line
Chmod - This command is used to change file/folder permissions. Basically every file can be accessed by 3 types of users, namely:
Type Explanation
owner The user who created and owns the file/directory.
group All users belonging to the same group.
other people All other users in the system who are not owners or members of the group..
The command ls -l, can be used to see the permissions on a file and its owner. For example, ls -l file1.txt would output:
-rwxr–rw- 1 user user 0 Jan 19 12:59 file1.txt
- “-rwxr–rw-“ – This section will display the permissions. There are 3 letters that you may see often: r, w, x, d.
- d means the file type is a directory (folder). In our example, the letter d is missing (it should be in the first place, but in our example it is missing/represented by the symbol "-" which means "no"). The letter x means permission to execute files/folders (we often need this permission to enter folders).
- 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't edit or execute the file.
- 1 – Number of hard links. Usually a hard link is an additional name for a file.
- user user – Displays the owner and group owner of the file.
- 0 – Displays the file size.
- Jan 19 12:59 – Displays 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'll learn how to do this simply by plugging 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:
-rwxr–rw- 1 user user 0 Jan 19 12:59 pm file1.txt
So the command we use is:
chmod 746 file1.txt
Basically, each number in this command represents a permission 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 to get the number 7 is to add up the digits 4, 2 and 1, the form is: 4+2+1= 7. This means ALL permissions (read , write, and run – rwx). This first number will create permissions for the file owner.
The 2nd number is 4. It means r permission (read), this number gives permission to the group owner.
The 3rd number is 6, based on the meaning of the number above, the way to get the number is to add 4 and 2, so 4+2=6. So we give permission to others to read (4) and write (2) to the file.
The 3rd part of the command (file1.txt) is the filename, we write the name of the file for which we want to set the permissions.
Another example: chmod 777 file2. txt, this command will grant ALL permissions for all types of users (owners, groups and others).
The following is a list of the most frequently used files:
Value Numerical Explanation
-rw——- 600 Owner can read and write.
-rw-r–r– 644 Owners can read and write, groups 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 can't do anything with the file.
-rwx–x–x 711 Owner can read, write, and execute, group, and others 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 Numerical 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 study one of them and use it at all times (use numbers as above). If you want to learn other ways to change permissions.
Step 2 – Change owner of files and folders via command line
chown – This command is used to change the owner of a file/folder. The basic command is:
chown [owner/group owner] [filename]
Basically, if we have a “demo.txt” file and we want to make this file owner “bambang” and group owner “clients”, then the command we will use is:
chown bambang:clients demo.txt
So, as you can see, we separated the owner and group owner by the symbol “:” (colon). If we just want to change the owner of the file, we can use:
chown bambang 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 is as follows:
chown :demo client.txt`
In this state, only the group owner will change to a client (the owner remains unchanged).
Step 3 – Using additional options with the chmod command and the chown command
One of the main options that work with both commands is -R, which means recursive. This option allows you to change permissions/owners on a folder and ALL files and sub folders within it
IMPORTANT! You should be careful with this option because if you use it incorrectly, you can actually change the permissions/owners of all files on your system. The effect will cause fatal errors and it will be more difficult for you to return to normal conditions.
Other options for "chmod" and "chown" are:
- “-f” – Will force execution and give no 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 actually occurred.
Conclusion
In this tutorial, you have now learned how to change permissions and owners for files and folders via the command line. If you are already proficient, of course, server management will be easier. You can continue to study other tutorials that we have provided.