chown 命令

语法格式:  chown -选项 用户或组 文件

                 chown [-R] user[.group] 文件或目录

                 chown [-R] user[:group] 文件或目录  (  . 与 : 均可)

说明:chown将指定文件的拥有者改为指定的用户或组。用户可以是用户名或用户ID。组可以是组名或组ID。文件是以空格分开的要改变权限的文件列表,支持通配符。

选项说明:

user : 新的文件拥有者的使用者 ID

group : 新的文件拥有者的使用者组(group)

-c : 若该档案拥有者确实已经更改,才显示其更改动作

-f : 若该档案拥有者无法被更改也不要显示错误讯息

-v : 显示拥有者变更的详细资料

-R : 对目前目录下的所有档案与子目录进行相同的拥有者变更(即以递回的方式逐个变更)

-help : 显示辅助说明

-version : 显示版本

1. 文件的所有者和所属组

[root@VM-83-114-centos /data/zhiyun_test/folder_01]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 26 13:24 file_01
-rw-r--r-- 1 root root 0 Jan 26 13:22 file_02
-rw-r--r-- 1 root root 0 Jan 26 13:24 file_03
[root@VM-83-114-centos /data/zhiyun_test/folder_01]# chown user_00 file_01
[root@VM-83-114-centos /data/zhiyun_test/folder_01]# ll
total 0
-rw-r--r-- 1 user_00 root 0 Jan 26 13:24 file_01
-rw-r--r-- 1 root    root 0 Jan 26 13:22 file_02
-rw-r--r-- 1 root    root 0 Jan 26 13:24 file_03

[root@VM-83-114-centos /data/zhiyun_test/folder_01]# chown .users file_02
[root@VM-83-114-centos /data/zhiyun_test/folder_01]# ll
total 0
-rw-r--r-- 1 user_00 root  0 Jan 26 13:24 file_01
-rw-r--r-- 1 root    users 0 Jan 26 13:22 file_02
-rw-r--r-- 1 root    root  0 Jan 26 13:24 file_03

root@VM-83-114-centos /data/zhiyun_test/folder_01]# chown user_00.users file_03
[root@VM-83-114-centos /data/zhiyun_test/folder_01]# ll
total 0
-rw-r--r-- 1 user_00 root  0 Jan 26 13:24 file_01
-rw-r--r-- 1 root    users 0 Jan 26 13:22 file_02
-rw-r--r-- 1 user_00 users 0 Jan 26 13:24 file_03
[root@VM-83-114-centos /data/zhiyun_test/folder_01]# 

1.  只修改文件的属主(所有者)

     chown user_00 test.txt

 2.  只修改文件的属组

     chown .user_00 test.txt   

(只修改文件的所属组       ## 注意 . 前面空出表示不修改属主,只修改属组user_00)

3.  时修改文件的所有者和所属组

     chown user_01.user_01 test.txt 

    ( 同时修改文件的所有者和所属组 )

2. 目录的所有者和所属组

[root@VM-83-114-centos //data/zhiyun_test/folder_02]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 26 13:40 file_05
[root@VM-83-114-centos //data/zhiyun_test/folder_02]# cd ../
[root@VM-83-114-centos //data/zhiyun_test]# ls
folder_01  folder_02
[root@VM-83-114-centos //data/zhiyun_test]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 26 13:24 folder_01
drwxr-xr-x 2 root root 4096 Jan 26 13:40 folder_02
[root@VM-83-114-centos //data/zhiyun_test]# chown user_00.users folder_02
[root@VM-83-114-centos //data/zhiyun_test]# ll
total 8
drwxr-xr-x 2 root    root  4096 Jan 26 13:24 folder_01
drwxr-xr-x 2 user_00 users 4096 Jan 26 13:40 folder_02
[root@VM-83-114-centos //data/zhiyun_test]# cd folder_02
[root@VM-83-114-centos //data/zhiyun_test/folder_02]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 26 13:40 file_05


[root@VM-83-114-centos //data/zhiyun_test]# chown -R user_00:users folder_02
[root@VM-83-114-centos //data/zhiyun_test]# ll
total 8
drwxr-xr-x 2 root    root  4096 Jan 26 13:24 folder_01
drwxr-xr-x 2 user_00 users 4096 Jan 26 13:40 folder_02
[root@VM-83-114-centos //data/zhiyun_test]# cd folder_02
[root@VM-83-114-centos //data/zhiyun_test/folder_02]# ll
total 0
-rw-r--r-- 1 user_00 users 0 Jan 26 13:40 file_05

     1. 修改目录的所有者和所属组

         chown priv1.priv2  test

       (只会修改目录本身,对它里面的文件或目录无影响)

    2. 递归修改目录的所有者和所属组

       chown -R  priv1.priv2   test

chgrp 命令

语法格式: chgrp 组名 文件或目录

chgrp   修改文件或目录的 所属组


   chgrp priv1 passwd
(  对于修改目录的所属组,chgrp也是可以递归的,选项为-R。)

[root@VM-83-114-centos //data/zhiyun_test]# ll
total 12
drwxr-xr-x 2 root    root  4096 Jan 26 13:24 folder_01
drwxr-xr-x 2 user_00 users 4096 Jan 26 13:40 folder_02
drwxr-xr-x 2 root    root  4096 Jan 26 13:46 folder_03
[root@VM-83-114-centos //data/zhiyun_test]# chgrp users folder_03
[root@VM-83-114-centos //data/zhiyun_test]# ll
total 12
drwxr-xr-x 2 root    root  4096 Jan 26 13:24 folder_01
drwxr-xr-x 2 user_00 users 4096 Jan 26 13:40 folder_02
drwxr-xr-x 2 root    users 4096 Jan 26 13:46 folder_03
[root@VM-83-114-centos //data/zhiyun_test]# cd folder_03
[root@VM-83-114-centos //data/zhiyun_test/folder_03]# ls
file_06
[root@VM-83-114-centos //data/zhiyun_test/folder_03]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 26 13:46 file_06
[root@VM-83-114-centos //data/zhiyun_test/folder_03]# cd ../
[root@VM-83-114-centos //data/zhiyun_test]# chgrp -R users folder_03
[root@VM-83-114-centos //data/zhiyun_test]# ll
total 12
drwxr-xr-x 2 root    root  4096 Jan 26 13:24 folder_01
drwxr-xr-x 2 user_00 users 4096 Jan 26 13:40 folder_02
drwxr-xr-x 2 root    users 4096 Jan 26 13:46 folder_03
[root@VM-83-114-centos //data/zhiyun_test]# cd folder_03
[root@VM-83-114-centos //data/zhiyun_test/folder_03]# ll
total 0
-rw-r--r-- 1 root users 0 Jan 26 13:46 file_06
[root@VM-83-114-centos //data/zhiyun_test/folder_03]# 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐