In Linux, the chown command is used to change the owner and group of a file or directory. This command can authorize a user to become the owner of the specified file or change the file. The group it belongs to; the syntax for this command is "chown [options]... [owner][:[group]] file...".
#The operating environment of this tutorial: Red Hat Enterprise Linux 6.1 system, Dell G3 computer.
linux chown command
The chown command changes the owner and group of a file or directory. This command can change the owner of a file or directory to a user. Authorization makes the user the owner of the specified file or changes the group to which the file belongs. The user can be user or user D, and the user group can be group name or group id. The filename can be a space-separated list of files, and the filename can contain wildcard characters.
Only file owners and super users can use this command.
Command format:
chown [选项]... [所有者][:[组]] 文件...
Command function:
Change the owner and group of the file through chown . You can use the Username and User ID settings when changing the owner or group of a file. Ordinary users cannot change their own files to other owners. Its operating rights are generally administrators.
Command parameters:
Required parameters:
-c displays the changed part of the information
-f Ignore error messages
-h Repair symbolic links
-R Process all files in the specified directory and its subdirectories
-v Display Detailed processing information
-deference acts on the pointer of the symbolic link, not the link file itself
Select parameters:
--reference=
--from=< ;Current user: current group> Changes will only be made if the current user and group are the same as the specified user and group
--help Display help information
--version Display version Information
Usage Example:
Example 1: Changing Owner and Group
Command:
chown mail:mail log2012.log
Output:
[root@localhost test6]# ll ---xr--r-- 1 root users 302108 11-30 08:39 linklog.log ---xr--r-- 1 root users 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root users 0 11-30 08:39 log2014.log -rw-r--r-- 1 root users 0 11-30 08:39 log2015.log -rw-r--r-- 1 root users 0 11-30 08:39 log2016.log -rw-r--r-- 1 root users 0 11-30 08:39 log2017.log [root@localhost test6]# chown mail:mail log2012.log [root@localhost test6]# ll ---xr--r-- 1 root users 302108 11-30 08:39 linklog.log ---xr--r-- 1 mail mail 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root users 0 11-30 08:39 log2014.log -rw-r--r-- 1 root users 0 11-30 08:39 log2015.log -rw-r--r-- 1 root users 0 11-30 08:39 log2016.log -rw-r--r-- 1 root users 0 11-30 08:39 log2017.log [root@localhost test6]#
Example 2: Change file owner and group
Command:
chown root: log2012.log
Output:
[root@localhost test6]# ll 总计 604 ---xr--r-- 1 root users 302108 11-30 08:39 linklog.log ---xr--r-- 1 mail mail 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root users 0 11-30 08:39 log2014.log -rw-r--r-- 1 root users 0 11-30 08:39 log2015.log -rw-r--r-- 1 root users 0 11-30 08:39 log2016.log -rw-r--r-- 1 root users 0 11-30 08:39 log2017.log [root@localhost test6]# chown root: log2012.log [root@localhost test6]# ll 总计 604 ---xr--r-- 1 root users 302108 11-30 08:39 linklog.log ---xr--r-- 1 root root 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root users 0 11-30 08:39 log2014.log -rw-r--r-- 1 root users 0 11-30 08:39 log2015.log -rw-r--r-- 1 root users 0 11-30 08:39 log2016.log -rw-r--r-- 1 root users 0 11-30 08:39 log2017.log [root@localhost test6]#
Example 3: Change file group
Command:
chown :mail log2012.log
Output:
[root@localhost test6]# ll 总计 604 ---xr--r-- 1 root users 302108 11-30 08:39 linklog.log ---xr--r-- 1 root root 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root users 0 11-30 08:39 log2014.log -rw-r--r-- 1 root users 0 11-30 08:39 log2015.log -rw-r--r-- 1 root users 0 11-30 08:39 log2016.log -rw-r--r-- 1 root users 0 11-30 08:39 log2017.log [root@localhost test6]# chown :mail log2012.log [root@localhost test6]# ll 总计 604 ---xr--r-- 1 root users 302108 11-30 08:39 linklog.log ---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root users 0 11-30 08:39 log2014.log -rw-r--r-- 1 root users 0 11-30 08:39 log2015.log -rw-r--r-- 1 root users 0 11-30 08:39 log2016.log -rw-r--r-- 1 root users 0 11-30 08:39 log2017.log
Example 4 : Change the owner and group of all files in the specified directory and its subdirectories
Command:
chown -R -v root:mail test6
Output:
[root@localhost test]# ll drwxr-xr-x 2 root users 4096 11-30 08:39 test6 [root@localhost test]# chown -R -v root:mail test6 “test6/log2014.log” 的所有者已更改为 root:mail “test6/linklog.log” 的所有者已更改为 root:mail “test6/log2015.log” 的所有者已更改为 root:mail “test6/log2013.log” 的所有者已更改为 root:mail “test6/log2012.log” 的所有者已保留为 root:mail “test6/log2017.log” 的所有者已更改为 root:mail “test6/log2016.log” 的所有者已更改为 root:mail “test6” 的所有者已更改为 root:mail [root@localhost test]# ll drwxr-xr-x 2 root mail 4096 11-30 08:39 test6 [root@localhost test]# cd test6 [root@localhost test6]# ll 总计 604 ---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log ---xr--r-- 1 root mail 302108 11-30 08:39 log2012.log -rw-r--r-- 1 root mail 61 11-30 08:39 log2013.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2014.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2015.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2016.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2017.log
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of What is the use of linux chown command?. For more information, please follow other related articles on the PHP Chinese website!