Linux命令 - chown命令

Linux命令 - chown命令

  chown是change owner的缩写,在Linux中 chown 命令将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID;组可以是组名或者组ID;文件是以空格分开的要改变权限的文件列表,支持通配符。系统管理员经常使用chown命令,在将文件拷贝到另一个用户的名录下之后,让用户拥有使用该文件的权限。

1.语法:

chown [选项]... [所有者][:[组]] 文件...

2.功能:

  通过chown改变文件的拥有者和群组。在更改文件的所有者或所属群组时,可以使用用户名称和用户识别码设置。普通用户不能将自己的文件改变成其他的拥有者。其操作权限一般为管理员。

3.参数:
-c或--changes  效果类似"-v"参数,但仅显示更改的部分。 
-f或--quite或--silent  不显示错误信息。 
-h或--no-dereference  之对符号连接的文件作修改,而不更动其他任何相关文件。 
-R或--recursive  递归处理,将指定目录下的所有文件及子目录一并处理。 
-v或--version  显示指令执行过程。 
--dereference  效果和"-h"参数相同,作用于符号链接的指向,而不是链接文件本身。 
--reference=<参考文件或目录>  把指定文件或目录的拥有者与所属群组全部设成和参考文件或目录的拥有者与所属群组相同。
--from=<当前用户:当前群组>   只有当前用户和群组跟指定的用户和群组相同时才进行改变。
4.常用范例:

例一:改变拥有者和群组

命令:chown runoob:runoobgroup log1.txt

  将文件log1.txt的拥有者设为runoob,群体的使用者runoobgroup 。

[root@localhost test]# ll log1.txt 
---xr--r--. 1 root root 156 5月  19 11:17 log1.txt
[root@localhost test]# chown runoob:runoobgroup log1.txt
[root@localhost test]# ll log1.txt 
-rwxr-xr-x. 1 runoob runoobgroup 156 5月  19 11:17 log1.txt

例二:改变指定目录以及其子目录下的所有文件的拥有者和群组

命令: chown -R -v esther:esther test

  递归地给test目录下所有文件和子目录改变拥有者和群组为esther,并显示指令执行过程。

[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root  96 5月  18 15:27 color.sh
-rwxr--r--. 1 root root  96 5月  18 10:15 lncolor
---xr--r--. 1 root root 156 5月  19 11:17 log1.txt
-rwxrw-r-x. 1 root root 592 5月  14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月  14 17:06 log3.txt
-rwxr--r--. 1 root root 272 5月  19 10:51 log.txt
[root@localhost test]# cd../
bash: cd../: 没有那个文件或目录
[root@localhost test]# cd ../
[root@localhost games]# ll
总用量 0
drwxr-xr-x. 2 root root 100 5月  19 11:17 test
[root@localhost games]# chown -R -v esther:esther test
'test/log2.txt' 的所有者已从 root:root 更改为 esther:esther
'test/log3.txt' 的所有者已从 root:root 更改为 esther:esther
'test/lncolor' 的所有者已从 root:root 更改为 esther:esther
'test/color.sh' 的所有者已从 root:root 更改为 esther:esther
'test/log.txt' 的所有者已从 root:root 更改为 esther:esther
'test/log1.txt' 的所有者已从 root:root 更改为 esther:esther
'test' 的所有者已从 root:root 更改为 esther:esther
[root@localhost games]# cd test/
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 esther esther  96 5月  18 15:27 color.sh
-rwxr--r--. 1 esther esther  96 5月  18 10:15 lncolor
---xr--r--. 1 esther esther 156 5月  19 11:17 log1.txt
-rwxrw-r-x. 1 esther esther 592 5月  14 17:00 log2.txt
-rwxr-x--x. 1 esther esther 655 5月  14 17:06 log3.txt
-rwxr--r--. 1 esther esther 272 5月  19 10:51 log.txt

例三:改变文件拥有者

命令: chown esther: log1.txt

[root@localhost test]# ll log1.txt 
---xr--r--. 1 root root 156 5月  19 11:17 log1.txt
[root@localhost test]# chown esther: log1.txt 
[root@localhost test]# ll log1.txt 
---xr--r--. 1 esther esther 156 5月  19 11:17 log1.txt

例四:改变文件群组

命令: chown :root log1.txt

[root@localhost test]# ll log1.txt 
---xr--r--. 1 esther esther 156 5月  19 11:17 log1.txt
[root@localhost test]# chown :root log1.txt 
[root@localhost test]# ll log1.txt 
---xr--r--. 1 esther root 156 5月  19 11:17 log1.txt
  • 4
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值