linux 文件和目录的属性 linuxsir_org.htm
来自「linuxSir 网站的精华文章」· HTM 代码 · 共 843 行 · 第 1/5 页
HTM
843 行
example.txt<BR>-rw-r--r-- 1 beinan root 0 04-24 22:37
example.txt</CODE></DIV>
<P></P>
<P><B><BR>chown 所接的新的属主和新的属组之间应该以.或:连接,属主和属组之一可以为空。如果属主为空,应该是 :属组 ;如果属组为空
就就不必需要.或:了。<BR></B></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -l sun.txt<BR>-rwxr-xr-x
2 root root 29 04-22 21:02 sun.txt<BR>[root@localhost ~]# chown beinan
sun.txt 注:把sun.txt文件的属主改为beinan;<BR>[root@localhost ~]# ls -l
sun.txt<BR>-rwxr-xr-x 2 beinan root 29 04-22 21:02 sun.txt
<BR>[root@localhost ~]# chown :beinan sun.txt 注:把sun.txt文件的属组改为beinan
<BR>[root@localhost ~]# ls -l sun.txt<BR>-rwxr-xr-x 2 beinan beinan 29
04-22 21:02 sun.txt<BR>[root@localhost ~]# chown root:linuxsir sun.txt
注:把sun.txt的属主改为root,把改组改为linuxsir用户组;<BR>[root@localhost ~]# ls -l
sun.txt<BR>-rwxr-xr-x 2 root linuxsir 29 04-22 21:02 sun.txt</CODE></DIV>
<P></P>
<P><B><BR>chown
也提供了-R参数,这个参数对目录改变属主和属组极为有用,我们可以通过加-R参数来改变某个目录下的所有文件到新的属主或属组;<BR></B></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -ld mydir/
注:查看mydir目录的属性<BR>drwxr-xr-x 2 root root 4096 04-25 09:08 mydir/
注:我们发现此目录的属主是root用户,属组是root用户组;<BR>[root@localhost ~]# ls -lr mydir/
注:我们查看mydir目录下的有什么文件,及他们的属主和属组;<BR>总计 8<BR>-rwxr-xr-x 2 beinan linuxsir 29
04-22 21:02 sun.txt<BR>-rwxr-xr-x 2 beinan linuxsir 29 04-22 21:02
sun002.txt<BR>lrwxrwxrwx 1 root root 7 04-21 22:16 sun001.txt ->
sun.txt<BR>-rw-r--r-- 1 linuxsirorg root 0 04-24 22:37
example.txt<BR>-rw-rw-rw- 1 beinan root 0 04-24 15:05
dony.txt<BR>[root@localhost ~]# chown -R beinan:linuxsir mydir/
注:我们修改mydir及它的下级目录和所有文件到新的用户和用户组; <BR>[root@localhost ~]# ls -ld
mydir/<BR>drwxr-xr-x 2 beinan linuxsir 4096 04-25 09:08
mydir/<BR>[root@localhost ~]# ls -lr mydir/<BR>总计 8<BR>-rwxr-xr-x 2 beinan
linuxsir 29 04-22 21:02 sun.txt<BR>-rwxr-xr-x 2 beinan linuxsir 29 04-22
21:02 sun002.txt<BR>lrwxrwxrwx 1 beinan linuxsir 7 04-21 22:16 sun001.txt
-> sun.txt<BR>-rw-r--r-- 1 beinan linuxsir 0 04-24 22:37
example.txt<BR>-rw-rw-rw- 1 beinan linuxsir 0 04-24 15:05
dony.txt</CODE></DIV>
<P></P>
<P><B><BR>值得注意的是chown 和chmod 类似,我们
改变一个目录下的所有.file时的属主和属组时要小心操作。比如我们要改变testdir及它目录下的所有.file
的属主和改组时。下面的例子操作是错误的。<BR></B></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -ld /root/
注:查看/root目录的属性,主要看他的属主和属组;<BR>drwxr-xr-x 19 root root 4096 04-25 09:20
/root/ 注:属主是root,属组是root用户组;<BR>[root@localhost ~]# ls -ld /root/testdir/
注:查看/root/testdir 目录的属主和属组;<BR>drwxr-xr-x 2 root root 4096 04-25 09:20
/root/testdir/ 注:属主是root,属组是root用户组;<BR>[root@localhost ~]# chown -R
beinan:beinan /root/testdir/.*
注:改变/root/testdir目录下的以.开头的所有文件(注:假设我们是这么想的)<BR>[root@localhost ~]# ls -ld
/root/ 注:查看/root的属性;<BR>drwxr-xr-x 19 beinan beinan 4096 04-25 09:20
/root/ 注:看到了吧,testdir 目录的上组目录/root的属主和属组也改变了。<BR>[root@localhost ~]# ls
-ld /root/testdir/ 注:查看testdir<BR>drwxr-xr-x 2 beinan beinan 4096 04-25
09:20 /root/testdir/</CODE></DIV>
<P></P>
<P><B><BR>所以我们用.*来匹配文件时,可能会存在让chown
误操作,这样会连同父目录的属主和属组的归属关系也被改变。<BR></B></P>
<P><FONT id=7.2 size=3><B><BR>7.2 改变文件的属组工具 chgrp ;<BR></B></FONT></P>
<P><B>语法:</B></P>
<P>
<DIV class=codeblock><CODE>chgrp [参数选项]... 组 文件...</CODE></DIV>
<P></P>
<P>它的用户和chown
类似,只不过它仅是用来改变文件或目录的属组的;-R参数用于目录及目录下所有文件改变属组的。这和chown也是一样的。简单来两个例子;</P>
<P><B>例子一:</B></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -l sun.txt<BR>-rw-r--r--
1 root root 0 04-25 09:38 sun.txt<BR>[root@localhost ~]# chgrp beinan
sun.txt 注:改变sun.txt的属组为beinan用户组; <BR>[root@localhost ~]# ls -l
sun.txt<BR>-rw-r--r-- 1 root beinan 0 04-25 09:38 sun.txt</CODE></DIV>
<P></P>
<P><B>例子二:</B></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -ld mydir/<BR>drwxr-xr-x
2 root root 4096 04-25 09:08 mydir/<BR>[root@localhost ~]# ls -lr
mydir/<BR>总计 8<BR>-rwxr-xr-x 2 root root 29 04-22 21:02
sun.txt<BR>-rwxr-xr-x 2 root root 29 04-22 21:02 sun002.txt<BR>lrwxrwxrwx
1 root root 7 04-21 22:16 sun001.txt -> sun.txt<BR>-rw-r--r-- 1 root
root 0 04-24 22:37 example.txt<BR>-rw-rw-rw- 1 root root 0 04-24 15:05
dony.txt<BR>[root@localhost ~]# chgrp -R linuxsir mydir/
注:改变所mydir及其下面所有的文件及子目录的属组为linuxsir;<BR>[root@localhost ~]# ls -ld
mydir/<BR>drwxr-xr-x 2 root linuxsir 4096 04-25 09:08
mydir/<BR>[root@localhost ~]# ls -lr mydir/<BR>总计 8<BR>-rwxr-xr-x 2 root
linuxsir 29 04-22 21:02 sun.txt<BR>-rwxr-xr-x 2 root linuxsir 29 04-22
21:02 sun002.txt<BR>lrwxrwxrwx 1 root linuxsir 7 04-21 22:16 sun001.txt
-> sun.txt<BR>-rw-r--r-- 1 root linuxsir 0 04-24 22:37
example.txt<BR>-rw-rw-rw- 1 root linuxsir 0 04-24 15:05
dony.txt</CODE></DIV>
<P></P>
<P><FONT id=7.3 size=3><B><BR>7.3 文件属主和属组的特殊情况 ;<BR></B></FONT></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -lh
sungood.txt<BR>-rw-r--r-- 1 501 502 85 04-25 13:45
sungood.txt</CODE></DIV>
<P></P>
<P>上面的例子是不是有点怪?因为他的属主和属组都是一个数值;这是为什么呢?出现这种情况的原因是系统中不存在与之对应的用户,所以只能以数字形式显示了。有时我们删除了用户,但没有删除其家目录,这种情况下,它的家目录的属主和属组也会变成数字;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# userdel
linuxsir<BR>[root@localhost ~]# ls -ld /home/linuxsir<BR>drwx------ 16 501
502 4096 03-27 02:28 /home/linuxsir</CODE></DIV>
<P></P>
<P><FONT id=8 size=4><B><BR>8、影响文件的读写执行的因素;<BR></B></FONT></P>
<P>之所以把这部份的内容单列出来,是因为这部份的内容是基于我们对用户管理及文件权限了解的基础上进行的。比如一个文件的读、写、执行,它要受到哪几方面的影响。</P>
<P>一个文件能不能被读取,要受到它的属主、属组及其它用户权限的影响,还要受到其父目录权限的影响。我们来举个例子;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# cd /home 注:进入/home
目录;<BR>[root@localhost home]# mkdir redhatdir
注:创建一个目录redhatdir<BR>[root@localhost home]# touch redhatdir/test.txt
注:创建一个文件test.txt<BR>[root@localhost home]# chmod 700 redhatdir/
注:修改redhatdir的权限 ,为属主可读可写可执行,属组和其它用户无权限;<BR>[root@localhost home]# ls -ld
redhatdir/ 注:查看redhatdir的属性;<BR>drwx------ 2 root root 4096 04-25 13:01
redhatdir/<BR>[root@localhost home]# ls -lr redhatdir/ 注:查看test.txt
文件的属性;<BR>总计 0<BR>-rw-r--r-- 1 root root 0 04-25 13:02
test.txt<BR>[root@localhost home]# su beinan 注:我们切换到普通用户beinan
<BR>[beinan@localhost home]$ cd redhatdir/
注:进入redhatdir目录,以beinan用户身份。<BR>bash: cd: redhatdir/:
权限不够<BR>[beinan@localhost home]$ more
redhatdir/test.txt<BR>redhatdir/test.txt: 权限不够</CODE></DIV>
<P></P>
<P><B>解释:</B>我们通过这个例子来看,为什么test.txt在其它用户权位上拥有可读权限r--,但我们用普通用户还不能查看它的内容呢?这是因为他的父目录没有其它用户的何读权限。我们是不是redhatdir目录的其它用户可读权限打开,就能让普通用户beinan能读取
test.txt的内容了呢??</P>
<P>
<DIV class=codeblock><CODE>[root@localhost home]# chmod 704
redhatdir/<BR>[root@localhost home]# ls -ld redhatdir/<BR>drwx---r-- 2
root root 4096 04-25 13:02 redhatdir<BR>[root@localhost home]# su
beinan<BR>[beinan@localhost home]$ cd redhatdir/<BR>bash: cd: redhatdir/:
权限不够</CODE></DIV>
<P></P>
<P>看来如果不设置属组的权限,只打开属主的权限及其它用户在redhatdir目录的读权限的情况下,其它用户是不能访问的;我们应该把test.txt父目录的
redhatdir 的属主的读、写、执行要打开,还要把父目录的属组的读和执行权限打开,其它用户的读和执行权限打开,也就是要拥有 rwxr-xr-x
权限,这样文件的其它用户才能访问。</P>
<P>
<DIV class=codeblock><CODE>[root@localhost home]# chmod 755
redhatdir/<BR>[root@localhost home]# more redhatdir/test.txt</CODE></DIV>
<P></P>
<P>好象这块说的不太清楚,如果您看不太明白,多多chmod 练习练习,也没有什么难的。</P>
<P><B>其实为文件分配权限的最终目的是让文件的属主有何权限,让属组下的用户有何权限,让其它用户有何权限。文件权限是和用户管理相关联的,所以理解这方面的内容还得了解用户管理。</B></P>
<P><FONT id=9 size=4><B><BR>9、文件被修改或被访问的时间;<BR></B></FONT></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -l
adduml02.sh<BR>-rwxr-xr-x 1 root root 545 04-21 22:26
adduml02.sh</CODE></DIV>
<P></P>
<P>我们通过查看 文件的属性时,会发现它的时间标记,比如上面的 04-21 22:26
。这个时间并不代表文件被创建的时候,他是代表文件被访问或被修改的时间。文件被修改的时间比较好理解,比如我们可以用编辑器来修改文本文件,然后保存一下,这样文件的时间就变了。</P>
<P>当然也有其它的工具不修改文件的内容,只修改文件的时间,这时可以被称为访问时间。比如touch工具能达到这个目的。</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -l
adduml02.sh<BR>-rwxr-xr-x 1 root root 545 04-21 22:26
adduml02.sh<BR>[root@localhost ~]# touch adduml02.sh
注:更新文件的访问时间成为当前系统时间;<BR>[root@localhost ~]# ls -l
adduml02.sh<BR>-rwxr-xr-x 1 root root 545 04-25 11:21
adduml02.sh</CODE></DIV>
<P></P>
<P>关于touch 的用法, 更多的请参见: man touch或touch --help </P>
<P><FONT id=10 size=4><B><BR>10、文件属性和文件系统属性的关系;<BR></B></FONT></P>
<P>文件系统的特性决定着文件属性的定义和修改,比如我们通过 chattr 来锁定一个文件为不可修改或不可删除时,要用到chattr
的+i参数;这在ext2和ext3文件系统是有效的,但在reiserfs 文件系统是没有任何效果的;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# chattr +i
lsfile.sh<BR>[root@localhost ~]# lsattr lsfile.sh<BR>----i--------
lsfile.sh<BR>[root@localhost ~]# rm -rf lsfile.sh<BR>rm: 无法删除 “lsfile.sh”:
不允许的操作</CODE></DIV>
<P></P>
<P>注:如果把lsfile.sh变成可修改可删除,应该用-i参数;</P>
<P>比如在ext3或ext2 文件系统中,我们要让一个文件只能追加内容,但不能删除。应该用chattr的+a参数。</P>
<P>如果您在用ext3文件系统,想查看chattr 的帮助,请man chattr 。</P>
<P><FONT id=11 size=4><B><BR>11、关于本文;<BR></B></FONT></P>
<P>关于Linux文件系统相关的文章,我是写过很多篇了,但感觉还是没有把问题说的更明白一点。存在这样问题的主要原因是Linux基础知识都是关联的。我们为了把一个问题说的清楚,得解决其相当的知识,这是最让人头痛的。<BR>有些更为深入的东西可能我没有能力去写,一是因为这些内容用的比
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?