首页 > 代码库 > Linux命令篇之chown命令和chgrp命令

Linux命令篇之chown命令和chgrp命令

chown命令:

chown可以改变某个文件的所有者或所属组,文件是以空格分开的要改变权限的文件列表,支持通配符。

系统管理员经常使用chown命令,在将文件拷贝到另一个用户的名录下之后,让用户拥有使用该文件的权限。 

命令格式:

chown [  -f ] [ -h ] [  -R ] Owner [ :Group ]{ File ... | Directory ... }
chown 
-R  [  -f ] [ -H | -L | -P ] Owner [ :Group ]{ File ... | Directory … }

命令说明:


chown 
命令将 File 参数指定的文件的所有者更改为 Owner 参数指定的用户。
Owner 参数的值可以是可在 /etc/passwd 文件中找到的用户标识或登录名。还可以选择性地指定组。
Group 参数的值可以是可在 /etc/group 文件中找到的组标识或组名。

注意:

1. 只有 root 用户可以更改文件的所有者。 只在您是root 用户或拥有该文件的情况下才可以更改文件的组。

2. 如果拥有文件但不是 root 用户,则只可以将组更改为您是其成员的组。

3. 虽然 -H、-L 和 -P 标志是互斥的,指定不止一个也不认为是错误。

命令参数

1. 必要参数:

    -c 显示更改的部分的信息
    -f 忽略错误信息
    -h 修复符号链接
    -R 处理指定目录以及其子目录下的所有文件
    -v 显示详细的处理信息
    -deference 作用于符号链接的指向,而不是链接文件本身

2. 选择参数:

    --reference=<目录或文件> 把指定的目录/文件作为参考,把操作的文件/目录设置成参考文件/目录相同拥有者和群组
    --from=<当前用户:当前群组> 只有当前用户和群组跟指定的用户和群组相同时才进行改变
    --help 显示帮助信息
    --version 显示版本信息

命令举例

更改文件的所有者

例子1:更改文件testchown.txt的所有者

[plain] view plain copy

1.  bixiaopeng@ubuntu:~$ chown bixiaopeng testchown.txt   

2.    

3.  bixiaopeng@ubuntu:~$ ls -al testchown.txt  

4.  -rw-rw-r-- 1 bixiaopeng bixiaopeng 22  8 14 08:08 testchown.txt  



更改目录的所有者


例子2:将文件夹testfile中所有文件的所有者和组更改为用户bixiaopeng 和组sambashare

[plain] view plain copy

1.  <span style="font-family:Microsoft YaHei;font-size:12px;">#先查看一下testfile所属用户和分组  

2.  bixiaopeng@ubuntu:~$ ls -al testfile  

3.  -rw-rw-r-- 1 bixiaopeng bixiaopeng 536870912  6 16 01:30 testfile  

4.  #更改  

5.  bixiaopeng@ubuntu:~$ chown -R bixiaopeng:sambashare testfile  

6.  bixiaopeng@ubuntu:~$ ls -al testfile  

7.  -rw-rw-r-- 1 bixiaopeng sambashare 536870912  6 16 01:30 testfile  

8.  #再将所属组改回来  

9.  bixiaopeng@ubuntu:~$ chown -R bixiaopeng:bixiaopeng testfile  

10. bixiaopeng@ubuntu:~$ ls -al testfile  

11. -rw-rw-r-- 1 bixiaopeng bixiaopeng 536870912  6 16 01:30 testfile  

12. </span>  




chgrp命令:

在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理。可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以。Chgrp命令就是change group的缩写!要被改变的组名必须要在/etc/group文件内存在才行。

1命令格式:

chgrp [选项] [组] [文件]

2命令功能:

chgrp命令可采用群组名称或群组识别码的方式改变文件或目录的所属群组。使用权限是超级用户。 

3命令参数:

必要参数:

-c 当发生改变时输出调试信息

-f 不显示错误信息

-R 处理指定目录以及其子目录下的所有文件

-v 运行时显示详细的处理信息

--dereference 作用于符号链接的指向,而不是符号链接本身

--no-dereference 作用于符号链接本身

选择参数:

--reference=<文件或者目录>

--help 显示帮助信息

--version 显示版本信息

4使用实例:

实例1:改变文件的群组属性 

命令:

chgrp -v bin log2012.log

输出:

[root@localhost test]# ll

---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# chgrp -v bin log2012.log

“log2012.log” 的所属组已更改为 bin

[root@localhost test]# ll

---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log

说明:

log2012.log文件由root群组改为bin群组

实例2:根据指定文件改变文件的群组属性 

命令:

chgrp --reference=log2012.log log2013.log

输出:

[root@localhost test]# ll

---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

[root@localhost test]#  chgrp --reference=log2012.log log2013.log 

[root@localhost test]# ll

---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

说明:

改变文件log2013.log 的群组属性,使得文件log2013.log的群组属性和参考文件log2012.log的群组属性相同

实例3:改变指定目录以及其子目录下的所有文件的群组属性 

命令:

输出:

[root@localhost test]# ll

drwxr-xr-x 2 root root   4096 11-30 08:39 test6

[root@localhost test]# cd test6

[root@localhost test6]# ll

---xr--r-- 1 root root 302108 11-30 08:39 linklog.log

---xr--r-- 1 root root 302108 11-30 08:39 log2012.log

-rw-r--r-- 1 root root     61 11-30 08:39 log2013.log

-rw-r--r-- 1 root root      0 11-30 08:39 log2014.log

-rw-r--r-- 1 root root      0 11-30 08:39 log2015.log

-rw-r--r-- 1 root root      0 11-30 08:39 log2016.log

-rw-r--r-- 1 root root      0 11-30 08:39 log2017.log

[root@localhost test6]# cd ..

[root@localhost test]# chgrp -R bin test6

[root@localhost test]# cd test6

[root@localhost test6]# ll

---xr--r-- 1 root bin 302108 11-30 08:39 linklog.log

---xr--r-- 1 root bin 302108 11-30 08:39 log2012.log

-rw-r--r-- 1 root bin     61 11-30 08:39 log2013.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2014.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2015.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2016.log

-rw-r--r-- 1 root bin      0 11-30 08:39 log2017.log

[root@localhost test6]# cd ..

[root@localhost test]# ll

drwxr-xr-x 2 root bin    4096 11-30 08:39 test6

[root@localhost test]#

说明:

改变指定目录以及其子目录下的所有文件的群组属性

实例4:通过群组识别码改变文件群组属性

命令:

chgrp -R 100 test6

输出:

[root@localhost test]# chgrp -R 100 test6

[root@localhost test]# ll

drwxr-xr-x 2 root users   4096 11-30 08:39 test6

[root@localhost test]# cd test6

[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]#

说明:

通过群组识别码改变文件群组属性,100为users群组的识别码,具体群组和群组识别码可以去/etc/group文件中查看


本文出自 “鹏哥的博客” 博客,谢绝转载!

Linux命令篇之chown命令和chgrp命令