One linux command per day (30) : chown command in detail
- 2020-05-13 04:28:16
- OfStack
chown changes the owner of a specified file to a specified user or group, which can be a user name or user ID; A group can be a group name or group ID; The file is a whitespace separated list of files to change permissions. Wildcards are supported. System administrators often use the chown command to give a user permission to use a file after copying it to another user's directory.
1. Command format:
chown [option]... [owner][:[group]] file...
2. Command functions:
Change the owner and group of files via chown. When you change the owner of a file or the group to which it belongs, you can use the user name and user identifier Settings. Regular users cannot change their files to other owners. Its operation permission 1 as the administrator.
3. Command parameters:
Necessary parameters:
-c displays information about the changed part -f ignores error messages -h fixes symlinks -R handles all files in the specified directory and its subdirectories -v displays detailed processing information -deference works on the pointing of symbolic links, not the link file itselfSelection parameters:
- reference = < Directory or file > Use the specified directory/file as a reference, and set the operating file/directory to the same owner and group as the reference file/directory - from = < Current user: current group > Only if the current user and group are the same as the specified user and group --help displays help --version displays version information4. Examples of use:
Example 1: change 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: changing file owners and groups
Command:
chown root: log2012.log
Output:
[root@localhost test6]# ll
A total of 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
A total of 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: changing file groups
Command:
chown :mail log2012.log
Output:
[root@localhost test6]# ll
A total of 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
A total of 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 " The owner of has been changed to root:mail
" test6/linklog.log " The owner of has been changed to root:mail
" test6/log2015.log " The owner of has been changed to root:mail
" test6/log2013.log " The owner of has been changed to root:mail
" test6/log2012.log " The owner has been retained as root:mail
" test6/log2017.log " The owner of has been changed to root:mail
" test6/log2016.log " The owner of has been changed to root:mail
" test6 " The owner of has been changed to 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
A total of 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