更改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' <== 出现错误信息~找不到这个用户组名~
原文地址:https://www.cnblogs.com/gavanwanggw/p/6925286.html