更改linux文件的拥有者及用户组(chown和chgrp)

、使用 chown命令 更改文件拥有者
在 shell 中,能够使用 chown命令 来改变文件全部者。 chown命令 是change owner(改变拥有者)的缩写。须要要注意的是, 用户必须是已经存在系统中的,也就是仅仅能改变为在 /etc/passwd这个文件里有记录的username称才干够

chown命令 的用途非常多,还能够顺便直接改动用户组的名称。

此外,假设要连文件夹下的全部子文件夹或文件同一时候更改文件拥有者的话,直接加上 

-R 的參数就可以。
基本的语法:
chown [ -R]  账号名称  文件文件夹
chown [ -R账号名称: 用户组名称  文件文件夹
參数
-R : 进行递归( recursive )的持续更改,即连同子文件夹下的全部文件、文件夹
都更新成为这个用户组。经常常使用在更改某一文件夹的情况。
演示样例1
[root@localhost home]#  touch  testfile  //由 root 用户创建文件 
[root@localhost home]#  ls  testfile  –l 
-rw--w--w- 1 root root 0 Jun 7 19:35 testfile  //文件的拥有者及拥有者级均为 root 
[root@localhost home]#  chown  yangzongde  testfile  //改动文件拥有者为 yangzongde 
[root@localhost home]#  ls  testfile  -l 
-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile  //查看文件拥有者为 yangzongde。但组仍为 root 
演示样例2
chown   bin   install.log
ls  -l
-rw-r--r--  1 bin  users 68495 Jun 25 08:53 install.log
chown  root: root  install.log
ls -l
-rw-r--r--  1 root root 68495 Jun 25 08:53 install.log
三、使用 chgrp命令 更改文件所属用户组
在shell中,能够使用 chgrp命令 来改变文件所属用户组,该命令就是change group(改变用户组)的缩写。

须要注意的是要改变成为的用户组名称,必须在

  /etc/group 里存在,否则就会显示错误。
基本的语法:
chgrp [ -R用户组名称  dirname/ filename ...
參数:
-R : 进行递归( recursive )的持续更改,即连同子文件夹下的全部文件、文件夹
都更新成为这个用户组。经常常使用在更改某一文件夹的情况。
演示样例3
[root@localhost home]#  ls  testfile  -l 
-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile  //查看文件拥有者为 yangzongde。但组为 root 
[root@localhost home]#  chgrp   yangzongde  testfile  //改动拥有者组为 yangzongde 
[root@localhost home]#  ls  testfile  -l 
-rw--w--w- 1 yangzongde yangzongde 0 Jun 7 19:35 testfile 
[root@localhost home]#  chown  root: root  testfile  // 使用 chown 一次性改动拥有者及组 
[root@localhost home]#  ls  testfile  -l 
-rw--w--w- 1 root root 0 Jun 7 19:35 testfile 
演示样例4
[root@linux ~]#  chgrp  users  install.log
[root@linux ~]#  ls  -l
-rw-r--r--  1 root users 68495 Jun 25 08:53 install.log
演示样例5
更改为一个   /etc/group 不存在的用户组
[root@linux ~]#  chgrp  testing  install.log
chgrp: invalid group name `testing' <== 出现错误信息~找不到这个用户组名~
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值