chgrp:改变文件所属用户组(change group的简称)
chown:改变文件所有者
chmod:改变文件的权限。
下面逐个介绍上面三种常用命令
一、改变所属用户组:chgrp
在root身份下登陆linux执行下面命令
[root@localhost ~]# chgrp users install.log
[root@localhost ~]# ls -l
总计 72
-rw------- 1 root root 1688 07-18 23:28 anaconda-ks.cfg
-rw-r--r-- 1 root users 39162 07-18 23:28 install.log
我们会观察到install.log 这个文件所属的用户组原来是root的,现在变为了users这个用户组里面的一个文件。值得注意的一点是,你所改的文件用户组必须要在/etc/group里面存有的用户组,否则会出现错误。
二、改变文件所有者:chown
在root身份下登陆linux执行下面命令
[root@localhost ~]# ls -l
总计 72
-rw------- 1 root root 1688 07-18 23:28 anaconda-ks.cfg
-rw-r--r-- 1 bin users 39162 07-18 23:28 install.log
我们观察到install.log这个文件所属的所有者原来是root的,现在变为了bin这个所有者拥有。值得注意的一点是,你所改变的文件用户必须是已经存在于系统的账号。
[root@localhost ~]# chown root:root install.log
[root@localhost ~]# ls -l
总计 72
-rw------- 1 root root 1688 07-18 23:28 anaconda-ks.cfg
-rw-r--r-- 1 root root 39162 07-18 23:28 install.log
上面的命令,将instal.log的所有者与用户组改回为root
三、修改文件属性:chmod
很多时候需要修改文件的属性才能很好的登陆访问某个文件或者限制他人登陆访问甚至修改文件,这里就可以看出linux下的文件属性是有多么的重要了。该变文件的属性有二种方法:
①数字类型改变文件权限
Linux文件的基本权限有9个,分别是Owner Group Others三种身份各有自己的r w x权限,其中,我们可以用数字来代表各个权限。
r:4
w:2
x:1
当权限为[-rwxrwx---] 分数在是:
owner = rwx =4+2+1 = 7
group = rwx = 4+2+1 = 7
others = --- = 0+0+0= 0
所以我们要设置权限更改时,该文件权限数字就是770,更改权限的命令举例如下
[root@localhost ~]# ls -al .bashrc
-rw-rw-rw- 1 root root 176 2006-07-13 .bashrc
[root@localhost ~]# chmod 777 .bashrc
[root@localhost ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 176 2006-07-13 .bashrc
这样我们就把.bashrc这个文件的权限由rw-rw-rw- 改为了rwxrwxrwx 就是这么简单
②符号类型改变文件权限
我们通过u g o a四种身份修改权限,其中+ - =分别代表:加入、除去、设置
假如我们将 .bashrc这个文件权限改为 "-rwxr-xr-x"如下所示
[root@localhost ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 176 2006-07-13 .bashrc
[root@localhost ~]# chmod u=rwx,go=rx .bashrc
[root@localhost ~]# ls -al .bashrc
-rwxr-xr-x 1 root root 176 2006-07-13 .bashrc
其中,u=rwx,go=rx分别代表,user、group、others的权限分别设置为【可读、可写、可执行】、【可读、不可写、可执行】、【可读、不可写、可执行】
当我们想增加.bashrc这个文件的每个人均可写入的权限,我们就使用
[root@localhost ~]# ls -al .bashrc
-rwxr-xr-x 1 root root 176 2006-07-13 .bashrc
[root@localhost ~]# chmod a+w .bashrc
[root@localhost ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 176 2006-07-13 .bashrc
如果我们想将权限去掉而不更改其他属性,我们就使用
[root@localhost ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 176 2006-07-13 .bashrc
[root@localhost ~]# chmod a-x .bashrc
[root@localhost ~]# ls -al .bashrc
-rw-rw-rw- 1 root root 176 2006-07-13 .bashrc