简介

chown:全称change owner,是用于设置文件所有者和文件关联组的命令。
还有一个与之密切相关的命令----chmod。都属于修改文件权限的命令。

详解

chown 可以将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID,组可以是组名或者组ID,文件是以空格分开的要改变权限的文件列表,支持通配符。
chown 需要超级用户 root 的权限才能执行此命令。只有超级用户和属于组的文件所有者才能变更文件关联组。非超级用户如需要设置关联组可能需要使用 chgrp 命令。

语法

chown [OPTION]... [OWNER][:[GROUP]] FILE...

参数解义
user新的文件拥有者的使用者 ID
group新的文件拥有者的使用者组(group)
-c显示更改的部分的信息
-f忽略错误信息
-h修复符号链接
-v显示详细的处理信息
-R处理指定目录以及其子目录下的所有文件
--help显示辅助说明
--version显示版本

实例

┌──(root㉿kali)-[~/Desktop/example]
└─# ll
total 0
-rw-r--r-- 1 root root 0 Dec  8 21:52 ces.txt  #第一个root是所属用户,第二个是用户组,ces.txt是文件名
                                                                                                                         
┌──(root㉿kali)-[~/Desktop/example]
└─# chown ces ces.txt  #修改ces.txt所属用户
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# ll    
total 0
-rw-r--r-- 1 ces root 0 Dec  8 21:52 ces.txt   #ces.txt所属用户变成了ces
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# chown :ces ces.txt     #修改ces.txt所属用户组
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# ll    
total 0
-rw-r--r-- 1 ces ces 0 Dec  8 21:52 ces.txt   #ces.txt所属用户组变成了ces
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# chown root:root ces.txt      #同时修改ces.txt所属用户和用户组
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# ll
total 0
-rw-r--r-- 1 root root 0 Dec  8 21:52 ces.txt    #ces.txt所属用户和用户组变成了root
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# mkdir cesdir     #创建文件夹
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# cd cesdir              
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example/cesdir]
└─# touch ces1.txt     #创建ces1.txt文件
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example/cesdir]
└─# cd ..    
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# ll
total 4
drwxr-xr-x 2 root root 4096 Dec  8 22:23 cesdir    #cesdir文件夹所属用户与用户组
-rw-r--r-- 1 root root    0 Dec  8 21:52 ces.txt
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# chown -R ces:ces cesdir       #修改cesdir目录下的所有文件夹和文件的所属用户和用户组
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# ll
total 4
drwxr-xr-x 2 ces  ces  4096 Dec  8 22:23 cesdir   #cesdir文件夹所属用户与用户组变成ces
-rw-r--r-- 1 root root    0 Dec  8 21:52 ces.txt
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example]
└─# cd cesdir              
                                                                                                                          
┌──(root㉿kali)-[~/Desktop/example/cesdir]
└─# ll
total 0
-rw-r--r-- 1 ces ces 0 Dec  8 22:23 ces1.txt   #ces1.txt文件所属用户与用户组变成ces

注意

  • 使用 ls 命令不会显示非自己所在用户组的文件或文件夹。
  • 使用 rm 命令删除不了非自己所在用户组的文件或文件夹,会报错(No such file or directory)。

结语

对于新手,权限应该算是遇到比较多的坑吧。学习Linux,权限也是一个比较重要的内容。