Linux文件或目录的读,写,执行权限

<meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE"> <meta content="OpenOffice.org 2.3 (Linux)" name="GENERATOR"> <style type="text/css"> <!-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } TD P { margin-bottom: 0cm } PRE.western { font-family: "Nimbus Mono L", "Courier New", monospace } PRE.cjk { font-family: "文鼎PL细上海宋Uni", monospace } PRE.ctl { font-family: "文鼎PL细上海宋Uni", monospace } TT.cjk { font-family: "文鼎PL细上海宋Uni", monospace } --> </style>

文件或目录的存取权限



快速理解Linux文件的权限,查看

Permissions, important and tricky!

Unix permissions concern who can read a file or directory, write to it, and execute it. Permissions are granted or withheld with a magic 3-digit number. The three digits correspond to the owner (you); the group (?); and the world (everyone else).

Think of each digit as a sum:


execute permission

= 1


write permission

= 2


<!-- D(["mb","write and execute (1+2)\u003c/td\>\u003ctd\>\u003d 3 \u003cbr\>\u003c/td\>\n\u003c/tr\>\u003ctr\>\u003ctd width\u003d\"30\"\>\u003cbr\>\u003c/td\>\u003ctd\>read permission \u003c/td\>\u003ctd\>\u003d 4\u003cbr\>\u003c/td\>\n\u003c/tr\>\u003ctr\>\u003ctd width\u003d\"30\"\>\u003cbr\>\u003c/td\>\u003ctd\>read and execute (4+1)\u003c/td\>\u003ctd\>\u003d 5\u003cbr\>\u003c/td\>\n\u003c/tr\>\u003ctr\>\u003ctd width\u003d\"30\"\>\u003cbr\>\u003c/td\>\u003ctd\>read and write (4+2)\u003c/td\>\u003ctd\>\u003d 6\u003cbr\>\u003c/td\>\n\u003c/tr\>\u003ctr\>\u003ctd width\u003d\"30\"\>\u003cbr\>\u003c/td\>\u003ctd\>read, write and execute (4+2+1)\u003c/td\>\u003ctd\>\u003d 7\u003cbr\>\u003c/td\>\n\u003c/tr\>\u003c/tbody\>\u003c/table\>\n\u003c/p\>\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd colspan\u003d\"2\"\>Add the number value of the permissions you want to grant each group to make a three digit number, \none digit each for the owner, the group, and the world. Here are some useful combinations. Try to figure them out!\u003cp\>\n\u003c/p\>\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod 600 {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>You can read and write; the world can&#39;t. Good for files.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod 700 {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>You can read, write, and execute; the world can&#39;t. Good for scripts.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod 644 {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>You can read and write; the world can only read. Good for web pages.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod 755 {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>You can read, write, and execute; the world can read and execute. Good for programs you want to share, and your \u003ctt\>public_html\u003c/tt\> directory.\u003cbr\>\n\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd colspan\u003d\"3\"\>\u003chr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd colspan\u003d\"3\"\>\u003cfont size\u003d\"+1\"\>Permissions, another way\u003c/font\>\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd colspan\u003d\"2\"\>\nYou can also change file permissions with letters:\u003cp\>\n\u003ctable border\u003d\"0\" width\u003d\"400\"\>\n \u003ctbody\>\u003ctr\>\u003ctd width\u003d\"30\"\>\u003cbr\>\u003c/td\>\u003ctd\>\u003cb\>u\u003c/b\> \u003d user (yourself) \u003c/td\>\u003ctd\>\u003cb\>g\u003c/b\> \u003d group \u003c/td\>\u003ctd\>\u003cb\>a\u003c/b\> \u003d everyone\u003cbr\>\u003c/td\>\n \u003c/tr\>\u003ctr\>\u003ctd width\u003d\"30\"\>\u003cbr\>\u003c/td\>\u003ctd\>\u003cb\>r\u003c/b\> \u003d read \u003c/td\>\u003ctd\>\u003cb\>w\u003c/b\> \u003d write \u003c/td\>\u003ctd\>\u003cb\>x\u003c/b\> \u003d execute\u003cbr\>\u003c/td\>\n\u003c/tr\>\u003c/tbody\>\u003c/table\>\n\u003c/p\>\u003cp\>\n\u003c/p\>\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod u+rw {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>Give yourself read and write permission\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod u+x {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>Give yourself execute permission.",1] ); //--> write and execute (1+2)

= 3


read permission

= 4


read and execute (4+1)

= 5


read and write (4+2)

= 6


read, write and execute (4+2+1)

= 7


Add the number value of the permissions you want to grant each group to make a three digit number, one digit each for the owner, the group, and the world. Here are some useful combinations. Try to figure them out!

修改方式1:以绝对方式改变权限
chmod 600 {filespec}You can read and write; the world can't. Good for files.

chmod 700 {filespec}You can read, write, and execute; the world can't. Good for scripts.

chmod 644 {filespec}You can read and write; the world can only read. Good for web pages.

chmod 755 {filespec}You can read, write, and execute; the world can read and execute. Good for programs you want to share, and your public_html directory.


Permissions, another way(修改方式2:以符号模式改变权限(在原来基础上增加添加定义的权限))

You can also change file permissions with letters:


u = user (yourself)

g = group

a = everyone


r = read

w = write

x = execute


chmod u+rw {filespec}Give yourself read and write permission

chmod u+x {filespec}Give yourself execute permission. <!-- D(["mb","\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>chmod a+rw {filespec}\u003c/tt\>\u003c/td\>\u003ctd\>Give read and write permission to everyone.\u003cbr\>\n\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd colspan\u003d\"3\"\>\u003chr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd colspan\u003d\"3\"\>\u003cfont size\u003d\"+1\"\>Applications I use\u003c/font\>\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>finger {userid}\u003c/tt\>\u003c/td\>\u003ctd\>Find out what someone&#39;s up to.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>gopher\u003c/tt\>\u003c/td\>\u003ctd\>Gopher.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>irc\u003c/tt\>\u003c/td\>\u003ctd\>IRC, but not available on RAIN.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>lynx\u003c/tt\>\u003c/td\>\u003ctd\>Text-based Web browser, fast and lean.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>ncftp\u003c/tt\>\u003c/td\>\u003ctd\>Better FTP.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>pico {filename}\u003c/tt\>\u003c/td\>\u003ctd\>Easy text editor, but limited. \u003cb\>vi\u003c/b\> and \u003cb\>emacs\u003c/b\> are available.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>pine\u003c/tt\>\u003c/td\>\u003ctd\>Email.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>telnet {host}\u003c/tt\>\u003c/td\>\u003ctd\>Start Telnet session to another host.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>tin\u003c/tt\>\u003c/td\>\u003ctd\>Usenet.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>uudecode {filename}\u003cbr\>uuencode {filename}\u003c/tt\>\u003c/td\>\u003ctd\>Do it on the server to reduce download size about 1/3.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>ytalk {userid}\u003c/tt\>\u003c/td\>\u003ctd\>Chat with someone else online, eg \u003cb\>ytalk mkummel\u003c/b\>. Please use \u003cb\>w\u003c/b\> first so you don&#39;t interrupt a big download!\u003cbr\>\n\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd colspan\u003d\"3\"\>\u003chr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd colspan\u003d\"3\"\>\u003cfont size\u003d\"+1\"\>System info\u003c/font\>\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>date\u003c/tt\>\u003c/td\>\u003ctd\>Show date and time.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>df\u003c/tt\>\u003c/td\>\u003ctd\>Check system disk capacity.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>du\u003c/tt\>\u003c/td\>\u003ctd\>Check your disk usage and show bytes in each directory.\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>more /etc/motd\u003c/tt\>\u003c/td\>\u003ctd\>Read message of the day, &quot;motd&quot; is a useful alias..\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>\u003ctt\>printenv\u003c/tt\>\u003c/td\>\u003ctd\>Show all environmental variables (in C-shell% - use \u003cb\>set\u003c/b\> in Korn shell$).\u003cbr\>\n\u003c/td\>\u003c/tr\>\u003ctr\>\u003ctd\>\u003cbr\>\u003c/td\>\u003ctd\>",1] ); //-->

chmod a+rw {filespec}Give read and write permission to everyone.


使用文件命令对文件进行操作的前提是拥有相应的权限。下面我们介绍如何控制这些权限。

(1)用户和权限
Linux为每个文件都分配了一个文件所有者,称为文件主,并赋予文件主惟一的注册名。对文件的控制取决于文件主或超级用户(root)。文件或目录的创建者对创建的文件或目录拥有特别使用权。

文件的所有关系是可以改变的,可以将文件或目录的所有权转让给其它用户,但只有文件主或root用户才有权改变文件的所有关系。文件的所有权的标志是用户ID(UID)。

利用chown 命令可以更改某个文件或目录的所有权。例如,超级用户把自己的一个文件myfile拷贝给用户xu。为了让用户xu能够存取这个文件,超级用户(root)应该把这个文件的属主设为xu,否则,用户xu无法存取这个文件,命令如下:
chown xu myfile

如果改变了文件或目录的所有权,原文件主将不再拥有该文件或目录的权限。

系统管理员经常使用chown命令,以便将文件拷贝到另一个用户的目录下以后,让该用户拥有使用该文件的权限。

(2)用户组
在Linux下,每个文件又同时属于一个用户组。当创建一个文件或目录时,系统会赋予它一个用户组关系,用户组的所有成员都可以使用此文件或目录。
文件用户组关系的标志是GID。文件的GID只能由文件主或超级用户(root)来修改。chgrp命令可以改变文件的GID。

(3)用户和权限
Linux系统中的每个文件和目录都有存取许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作。

Linux系统中规定了四种不同类型的用户,即文件主(owner)同组用户(group)、可以访问系统的其它用户(others)超级用户(root),具有管理系统的特权。

存取权限规定三种访问文件或目录的方式:读(r)、写(w)、可执行或查找(x)。

当用“ls -l”命令显示文件或目录的详细信息时,最左边的一列为文件的存取权限,其中各位的含义如图4所示。


图4 文件权限表示

◆文件的存取权限
读权限(r)表示只允许指定用户读取相应文件的内容,而禁止对它做任何的更改操作。将所访问的文件的内容作为输入的命令都需要有读的权限。例如cat、more等。
写权限(w)表示允许指定用户打开并修改文件。例如命令vi、cp等。
执行权限(x)表示允许指定用户将该文件作为一个程序执行。

◆目录的存取权限
在ls命令后加上-d选项,可以了解目录文件的使用权限。
读权限(r)表示可以列出存储在该目录下的文件,即读目录内容列表。这一权限允许Shell使用文件扩展名列出相匹配的文件名。
写权限(w)表示允许用户从目录中删除或添加新的文件,通常只有文件主才有写权限。
执行权限(x)表示允许用户在目录中查找,并能用cd命令将工作目录改到该目录。

(4)chmod命令
chmod命令用于改变或设置文件或目录的存取权限。只有文件主或超级用户root才有权用chmod改变文件或目录的存取权限。

根据表示权限的方式不同,该命令有以符号模式改变权限和以绝对方式改变权限两种用法。

以符号模式改变权限
chmod命令的一般格式是:chmod key 文件名

其中,key由以下各项组成:

                     

上面这三部分必须按顺序输入,可以用多个key,但必须以逗号隔开。例如:
$ chmod a+x ex1

上面代码表示将文件ex1的权限改为所有用户(a)都有执行权限(+x)
$ chmod u=r,ug=x ex1

上面这行代码表示将文件ex1的权限重新设置为文件主可以读和执行,组用户可以执行,其它用户无权访问。

以绝对方式改变权限
以绝对方式改变权限一般格式是:chmod mode 文件名

用绝对方式设置或改变文件的存取权限就是用数字“1”和“0”表示图4中所 示的9个权限位, 置为“1”表示有相应权限, 置为“0”表示没有相应权限。例如,某个文件的存取权限是文件主有读、写和执行的权限,组用户有读和执行的权限,其它用户仅有读的权限。用符号模式表示就 是rwxr-xr--,用二进制数字表示就是111101100。

为了方便记忆和表示,通常将这9位二进制数用等价的3个从0到7的八进制数表示,即从右到左3个二进制数换成一个八进制数。这样,上述二进制数就等价于八进制数“754”。

也就是说,mode是以3位八进制数字出现的,最左位表示文件主权限,中间位表示组用户权限,最右位表示其它用户权限。

例如,chmod 664 ex1使文件ex1的文件主和同组用户具有读写权限,但其它用户只可读。

◆umask命令
umask命令用来设置限制新建文件权限的掩码。其一般格式是:umask mode

当新文件被创建时,其最初的权限由文件创建掩码决定。用户每次注册进入系统时,umask命令都被执行,并自动设置掩码mode来限制新文件的权限。用户可以通过再次执行umask来改变默认值,新的权限将会把旧的覆盖掉。

利用umask命令可以指定哪些权限将在新文件的默认权限中被删除。例如,可以使用下面的命令创建掩码,取消组用户的写权限以及其他用户的读、写和执行权限: umask u=,g=w,o=rwx

执行该命令以后,下面所建新文件的文件主权限未作任何改变,而组用户没有写权限,其它用户的所有权限都被取消。

应注意,umask命令和chmod命令中,操作符“=”的作用恰恰相反。在chmod,利用它来设置指定的权限,而其余权限都被取消;但是在umask命令中,它将在原有权限的基础上把指定的权限删除。

不能直接利用umask创建一个可执行的文件,用户只能在其后利用chmod命令使它具有执行权限。假设执行了下面命令:
umask u=,g=w,o=rwx

虽然在命令行中,文件主和组用户的执行权限没有被删去,但默认的文件权限还 是640(即rw-r-----),而不是750(rwxr-x---)。但是,如果创建的是目录,或者通过编译程序创建一个可执行文件,将不受此限制。 在这种情况下,文件的执行权限会被设置。

此外,也可以使用八进制数值来设置mode。在umask中所指定的权限 表示要删除的权限,所以,如果一个文件原来的初始权限是777,那么执行命令umask 022以后, 那么该文件的权限将变为 755;如果该文件原来的初始权限是666, 那么该文件的权限将变为644。

使用下面的命令可以检查新创建文件的默认权限:
umask -s

上面命令中,选项-s表示以字符形式显示当前的掩码。

如果直接输入umask命令,不带任何参数,那么将以八进制形式显示当前的掩码。系统默认的掩码是0022。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值