[Linux实用命令四]文件权限详解

来源:互联网 发布:php post传递数组 编辑:程序博客网 时间:2024/06/05 17:16

http://www.qingsword.com/qing/883.html

 

 

这篇文章介绍一下Linux中文件权限的概念,实例演示如何利用命令来更改文件权限、文件所属用户组和文件所属用户。文章最后会补充一个Linux下非常实用的查找命令”find”的使用方法。


文章目录

  • [*1*].Linux文件属性详解
  • [*2*].Linux如何修改文件权限
  • [*3*].Linux如何修改文件所属用户组
  • [*4*].Linux如何修改文件所属用户
  • [*5*].Linux如何用”find”来查找文件

[*1*].Linux文件属性详解

在我们使用命令”ls -al”或者使用命令”ll -a”显示文件详细信息的时候,经常会看到下面这样的输出结构:

1/*下面是ls输出中的两条信息*/
2[qingsword@localhost ~]$ ls -al
3drwxr-xr-x  3 root root       4096 2012-12-31 03:42 .wine/
4-rwxrw-rw-  1 qingsword qingsword   13724074 2012-12-31 11:03  testfile

这里首先对第一条”drwxr-xr-x 3 root root 4096 2012-12-31 03:42 .wine/”的第一个字段”drwxr-xr-x”做出解释,这十个字符的意义:

第一个字符(drwxr-xr-x)代表的含义:

“d”代表目录,本例中第一条输出就是一个目录。
“-“则代表普通文件,本例中第二条输出就是一个普通文件。
“l”为符号链接,指向其它文件,有点类似Windows中的”快捷方式”。
“c”为字符设备节点(如键盘)访问设备,一般在/dev/目录下比较多见,大家可以”[qingsword@localhost ~]$ ll -a /dev”看一下。
“b”为块设备文件(如硬盘),同上,一般在/dev/目录下比较多见。
“P”(大写)命名管道函数,用于进程间通信。
“s”套接字,用于设备间通信。

后九个字符中(drwxr-xr-x),每三个字符是一组:

“rwx”文件拥有者对该文件的访问权限。
“r-x”文件所属用户组的成员对该文件的访问权限。
“r-x”既非文件拥有者,也非文件所在用户组的其他用户对该文件的访问权限。

“r”代表只读权限。
“w”代表可写权限。
“x”代表文件是可执行的(有点类似windows中的.exe后缀所代表的意思,Linux中没有后缀的概念,是否可执行查看的是文件是否有”x”权限)。

所以在”drwxr-xr-x”的后九个字符中:
“rwx”代表,文件拥有者对该文件拥有可读写与可执行的权限。
“r-x”代表文件所属用户组的成员对该文件拥有可读和可执行的权限,没有赋予的权限用”-“代替。
“r-x”代表其他用户对该文件拥有可读和可执行的权限。

上面介绍完了文件属性的第一个字段,下面是后面几个字段的介绍;

在”drwxr-xr-x 3 root root 4096 2012-12-31 03:42 .wine/”中:

3代表连接数。
第一个root是文件所属用户。
第二个root代表文件所属用户组是root组。
“4096”是文件大小,单位字节。
“2012-12-31 03:42″是文件最后修改时间,有些系统中的显示是”Dec 31 03:42″。
“.wine/”是文件或者文件夹名称。

所以综上所述我们很容易的得出第二条输出”-rwxrw-rw- 1 qingsword qingsword 13724074 2012-12-31 11:03 testfile”的含义:

这是一个普通文件”-“,文件拥有者对文件有可读写和可执行权限(rwx),文件所属组对文件有可读写权限(rw-),其他用户对文件也有可读写权限(rw-),文件的连接数是1,文件拥有者是”qingsword”,文件所属组是”qingsword”组,此文件的大小是”13724074″字节,最后修改时间是”2012-12-31 11:03″,文件的名称叫”testfile”。

除了这些常见属性之外,Linux下的文件还有一个需要了解的属性”Inode”号:

1/*
2 * 显示每个文件的Inode号,每个文件或者文件夹前面会出现一串数字,
3 * 在Linux中每个文件都有一个唯一的"Inode"编号。
4 */
5[qingsword@localhost ~]$ ls -i
6523273 anaconda-ks.cfg  523267 install.log.syslog  529410 testfile

上面介绍了文件或文件夹的各个属性字段,下面就来介绍如何修改这些字段的值。

[*2*].Linux如何修改文件权限

修改文件权限前,需要了解一下权限中的”rwx”与数字的对应关系,其中r=4,w=2,x=1。

例如:”drwxr-xr-x”,第一个”d”是代表文件夹,这里不用考虑,后面九个字符,每三个一组相加:

rwx=4+2+1=7
r-x=4+0+1=5

那么

drwxr-xr-x = 755

同理

-rwxrw-rw- = 766 (rw-就是4+2+0=6)

可以通过”chmod”来修改文件属性中的权限字段的值,请看下面的实例:

1/*新建一个文件*/
2[qingsword@localhost ~]$ touch testfile
3 
4/*查看一下文件的详细属性*/
5[qingsword@localhost ~]$ ls -l
6total 0
7-rw-rw-r--. 1 qingsword qingsword 0 Dec 31 11:03 testfile
8 
9/*修改文件权限*/
10[qingsword@localhost ~]$ chmod 766 testfile
11[qingsword@localhost ~]$ ls -l
12-rwxrw-rw-. 1 qingsword qingsword 0 Dec 31 11:03 testfile

上面的实例中文件的权限字段”-rw-rw-r–“(664)被修改成了”-rwxrw-rw-“(766),如果不习惯这种数字化的修改方式,还有一种字符方式:

1[qingsword@localhost ~]$ chmod u=rw,g=rw,o=r testfile
2[qingsword@localhost ~]$ ls -l
3-rw-rw-r--. 1 qingsword qingsword 0 Dec 31 11:03 testfile

这种修改方式u=后面填写文件拥有者对文件的权限,g=后面是文件所属组中的成员对文件的权限,o=后面表示其他用户对文件的权限。

[*3*].Linux如何修改文件所属用户组

可以使用chgrp修改文件所属用户组,请看下面实例:

1/*首先使用root权限创建一个组"testgroup"*/
2[qingsword@localhost ~]$ su
3Password:
4[root@localhost qingsword]# groupadd testgroup
5[root@localhost qingsword]# exit
6exit
7 
8/*查看一下文件"testfile"所属组,是"qingsword"组*/
9[qingsword@localhost ~]$ ls -l testfile
10-rw-rw-r--. 1 qingsword qingsword 0 Dec 31 11:03 testfile
11 
12/*使用普通权限更改这个文件所属组,显示权限不够*/
13[qingsword@localhost ~]$ chgrp testgroup testfile
14chgrp: changing group of `testfile': Operation not permitted
15 
16/*使用管理员权限更改这个文件所属组,成功*/
17[qingsword@localhost ~]$ su
18Password:
19[root@localhost qingsword]# chgrp testgroup testfile
20[root@localhost qingsword]# ls -l
21total 0
22-rw-rw-r--. 1 qingsword testgroup 0 Dec 31 11:03 testfile
23 
24/*使用普通权限,将文件改回自己的组,成功*/
25[qingsword@localhost ~]$ chgrp qingsword testfile
26[qingsword@localhost ~]$ ls -l
27total 0
28-rw-rw-r--. 1 qingsword qingsword 0 Dec 31 11:03 testfile

[*4*].Linux如何修改文件所属用户

可以使用chown来更改文件拥有者(只有root才有权执行),请看下面的实例:

1/*使用普通权限尝试更改文件拥有者,将"testfile"的拥有者更改成testuser,失败*/
2[qingsword@localhost ~]$ chown testuser testfile
3chown: changing ownership of `testfile': Operation not permitted
4 
5/*使用root权限更改文件拥有者,成功*/
6[qingsword@localhost ~]$ su
7Password:
8[root@localhost qingsword]# chown testuser testfile
9 
10[root@localhost qingsword]# ls -l
11total 0
12-rw-rw-r--. 1 testuser qingsword 0 Dec 31 11:03 testfile
13 
14/*
15 * 上面虽然更改了文件的拥有者,但是文件所属组没有改变,
16 * 可以通过添加参数-R,来一次性更改文件拥有者和文件所属组。
17 */
18[root@localhost qingsword]# chown -R root:testgroup testfile
19[root@localhost qingsword]# ls -l
20total 0
21-rw-rw-r--. 1 root testgroup 0 Dec 31 11:03 testfile

[*5*].Linux如何用”find”来查找文件

这一部分补充一个Linux下面很好用的查找命令,有点类似Windows DOS下面的”dir /s”命令,都是用来查找系统中是否存在某文件或文件夹的,请看下面的实例:

最好使用root权限执行这个命令,否则有些文件夹无权查找:

查看源代码
打印帮助
1/*使用"find"查找目录"/etc"下,名称是"profile"的文件或文件夹*/
2[root@localhost qingsword]# find /etc -name "profile"
3/etc/profile     /*找到一个*/
4 
5/*"find"的查找参数可以使用通配符*/
6[root@localhost qingsword]# find /etc -name "pro*"
7/etc/sysconfig/networking/profiles
8/etc/yum/protected.d
9/etc/selinux/targeted/modules/active/modules/procmail.pp
10/etc/profile.d
11/etc/protocols
12/etc/profile
0 0
原创粉丝点击