The concept of file owner and group is Linux The basis of . Each file is associated with an owner and group . You can use chown and chgrp Command to change the owner or group of a particular file or directory .
In this paper , We will discuss “chown” command , Because it also covers “chgrp” Most of the commands .
Even if you already know this command , It is also possible that one of the examples mentioned below may be new to you .
1. Change the owner of the file
# ls -lart tmpfile-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile# chown root tmpfile# ls -l tmpfile-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile
So we see the owner of the file from “himanshu” Turned into “root”.
2. Change the group of files
adopt chown command , You can also change the group ( Group to which the file belongs ).
# ls -l tmpfile-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile# chown :friends tmpfile# ls -l tmpfile-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
Observe carefully , The grouping of files is from “ family ” Turned into “ friend ”. So we see , Just add a... After the new group name “:”, You can change the group of files .
3. Change owner and group
# ls -l tmpfile-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile# chown himanshu:friends tmpfile# ls -l tmpfile-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
So we see the use of grammar '<newOwner>:<newGroup>', You can change the owner and group at once .
4. Use... For symbolic link files chown command
This is a symbolic link :
# ls -l tmpfile_symlnklrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile
So we see symbolic links “tmpfile_symlink” Link to file “tmpfile”.
Let's take a look at what happens when a symbolic link is posted chown What happens to the order :
# chown root:friends tmpfile_symlnk# ls -l tmpfile_symlnklrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile# ls -l tmpfile-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile
When issued on a symbolic link chown Command to change the owner and group , It is the reference object of symbolic link , That is, the owner and group have changed “tmpfile”. This is a chown The default behavior of the command . Besides , There is also a sign “-dereference”.
5. Use chown The command forces you to change the owner of the symbol file / Group .
Use logo “-h”, You can force changes to the owner or group of symbolic links , As shown below .
# ls -l tmpfile_symlnklrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile# chown -h root:friends tmpfile_symlnk# ls -l tmpfile_symlnklrwxrwxrwx 1 root friends 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile
6. Change the owner only if the file is owned by a specific user
Use chown “-from” sign , You can change the owner of the file , If the file is owned by a specific owner .
# ls -l tmpfile-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile# chown --from=guest himanshu tmpfile# ls -l tmpfile-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile# chown --from=root himanshu tmpfile# ls -l tmpfile-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
- In the example above , We verified the file “tmpfile” The original owner of / Group is root/friends.
- So let's use “-from” Flag changes owner to “himanshu”, But only if the existing owner is “guest”.
- Now? , Because the existing owner is not “ guest ”. therefore , The command failed to change the owner of the file .
- Next , If the existing owner is “root”( This is true ), We try to change the owner , And this command succeeds and the owner is changed to “himanshu”.
7. Change the group only if the file already belongs to a group
It also uses “--from” sign , But in the following way :
# ls -l tmpfile-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile# chown --from=:friends :family tmpfile# ls -l tmpfile-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile
Because of the documents 'tmpfile' Actually belongs to the group 'friends', So the condition is correct and the command succeeds .
So we see , By using signs '–from=:<conditional-group-name>' We can change the group under certain conditions .
Be careful : By following the template “–from=<conditional-owner-name>:<conditional-group-name>”, You can apply both owner and group conditions .
8. Will owner / Group settings are copied from one file to another
This can be done by using “-reference” Logo to achieve .
# ls -l file-rwxr-xr-x 1 himanshu family 8968 2012-04-09 07:10 file# ls -l tmpfile-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile# chown --reference=file tmpfile# ls -l tmpfile-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile
In the example above , We first checked the references “file” Owner / Group , Then I checked the target file “tmpfile” Owner / Group . Both are different . Then we use the with “–reference” Option chown The command will be the owner / Group settings are applied from the reference file to the target file . The command succeeded , also “tmpfile” Owner / Group settings are similar to “ file ”.
9. Change the owner of the file by recursively traversing the directory / Group
This can be done by “-R” Option implementation .
# ls -l linux/linuxKernel-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/linuxKernel# ls -l linux/ubuntu/ub10-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/ubuntu/ub10# ls -l linux/redhat/rh7-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/redhat/rh7# chown -R himanshu:family linux/# ls -l linux/redhat/rh7-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/redhat/rh7# ls -l linux/ubuntu/ub10-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/ubuntu/ub10# ls -l linux/linuxKernel-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/linuxKernel
So we are checking the catalog “linux” And its two subdirectories “ubuntu” and “redhat” Owner of all files in / Group and see . We send out with “-R” Option chown Command to change the owner and group . The order was successful , Owner of all files / Group successfully changed .
10. Use... For symbolic link directories chown command
Let's see if we send 'chown' Command recursively changes the owner of a directory / Filegroups , This directory is a symbolic link to other directories , What's going to happen .
This is a link to the directory 'linux' Symlink directory for 'linux_symlnk'( Already in the above example '9' Use in ):
$ ls -l linux_symlnklrwxrwxrwx 1 himanshu family 6 2012-05-22 22:02 linux_symlnk -> linux/
Now? , Let's recursively change the owner of this symbolic link directory ( from hemanshu To root):
# chown -R root:friends linux_symlnk# ls -l linux_symlnk/-rw-r--r-- 1 himanshu friends 0 2012-05-22 21:52 linuxKerneldrwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 redhatdrwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 ubuntu
In the output above , We see that the owners of the files and directories have not changed . This is because by default “chown” The command cannot traverse symbolic links . This is the default behavior , But there is also a sign “-P”.
11. Use chown Recursively force the change of the owner of the symbolic link directory / Group
This can be done by using the logo -H To achieve
# chown -R -H guest:family linux_symlnk# ls -l linux_symlnk/total 8-rw-r--r-- 1 guest family 0 2012-05-22 21:52 linuxKerneldrwxr-xr-x 2 guest family 4096 2012-05-22 21:52 redhatdrwxr-xr-x 2 guest family 4096 2012-05-22 21:52 ubuntu
So we see , By using -H sign , All the files / The owner of the folder / Groups have been changed .
12. List chown All changes made by the command
Use detailed options -v, It will show whether the ownership of the file is changed or retained , As shown below .
# chown -v -R guest:friends linuxchanged ownership of `linux/redhat/rh7' to guest:friendschanged ownership of `linux/redhat' retained to guest:friendsownership of `linux/redhat_sym' retained as guest:friendsownership of `linux/ubuntu_sym' retained as guest:friendschanged ownership of `linux/linuxKernel' to guest:friendschanged ownership of `linux/ubuntu/ub10' to guest:friendsownership of `linux/ubuntu' retained as guest:friendsownership of `linux' retained as guest:friends