Home>Article>Operation and Maintenance> How to modify the user and group to which a file belongs in Linux
In Linux, you can use the chown command to modify the user and group to which a file belongs. The syntax for modifying the user is "chown [-R] owner file or directory" and the syntax for modifying the group is " chown [-R]: The group file or directory it belongs to."
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
In Linux, when a file is created, the owner of the file is the user who created the file. The file user can modify the owner and user group of the file; or under the root user, the owner and user group of any file can be modified.
To modify the user and group to which the file belongs, you need to use the chown command.
chown command, which can be considered as the abbreviation of "change owner", is mainly used to modify the owner of a file (or directory). In addition, this command can also modify the group to which the file (or directory) belongs. .
When you only need to modify the owner, you can use the following basic format of the chown command:
chown [-R] 所有者 文件或目录
-R
(Note the capitalization) option means to change the owner along with all files in the subdirectory.
When you only need to modify the group to which it belongs, you can use the following basic format of the chown command:
chown [-R] :所属组 文件或目录
If you need to change all at the same time The basic format of the chown command is:
chown [-R] 所有者:所属组 文件或目录
Note that in the chown command, a dot (.) can also be used between the owner and the group it belongs to, but it will cause a problem. If When the user adds a decimal point (such as zhangsan.temp) when setting the account, it will cause misjudgment by the system. Therefore, it is recommended that you use colons to connect the owner and the group it belongs to.
Of course, the chown command also supports simply modifying the group to which a file or directory belongs. For example, chown :group install.log means modifying the group to which the install.log file belongs. However, the chgrp command is usually used to modify the group, so It is not recommended that you use the chown command.
Another thing to note is that when using the chown command to modify the owner (or owner) of a file or directory, you must ensure that the user user (or user group) exists, otherwise the command cannot be executed correctly and will Prompt "invalid user" or "invaild group".
Example of using the chown command
1. Change the owner of the file
First we Use the ls -l command to check the owner of the file, for example:
#ls -l tmpfile -rw-r-r-- 1 himanshu family 0 2019-03-30 11:03 tmpfile
Below we use the chown command to change the owner of the tmpfile file
#chown root tmpfile
and then use the ls -l command to check Looking at the ownership of the tmpfile file
#ls -l tmpfile -rw-r-r-- 1 root family 0 2019-03-30 11:04 tmpfile
, we can see that the owner of the tmpfile file "himanshu" has changed to "root"
2. Change the file group
#You can also change the group (the group to which the file belongs) through the chown command.
Use the following command to change the group to which the file belongs:
#chown :root tmpfile
Then use the ls -l command to check the group to which the tmpfile file belongs.
#ls -l tmpfile -rw-r-r-- 1 root root 0 2019-03-30 11:04 tmpfile
You can also directly do it all at once To change the owner and group of a file to root, you need to use the following command:
#chown root:root tmpfile
Recommended related video tutorials: "Linux Tutorial"
The above is the detailed content of How to modify the user and group to which a file belongs in Linux. For more information, please follow other related articles on the PHP Chinese website!