📄 ch01_23.htm
字号:
>File access is based on a file's user and group ownership and a set ofaccess bits (commonly called the <EMCLASS="emphasis">mode bits</EM>). When you try toaccess a file, you are put into one of three classes. You are eitherthe file's owner, a member of the file's group, or an "other."Three bits then determine whether you are allowed to read, write, orexecute the file.So, as<ACLASS="xref"HREF="ch01_23.htm#UPT-ART-1026-FIG-0"TITLE="Filesystem Permission Bits">Figure 1.5</A>shows, there are a total of nine mode bits (three foreach class) that set the basic access permissions.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-1026-FIG-0">Figure 1.5: Filesystem Permission Bits</A></H4><IMGCLASS="graphic"SRC="figs/1026a.gif"ALT="Figure 1.5"><PCLASS="para">It is common to see these nine basic mode bits interpreted as an octal(base-8) number, in which each digit specifies the access permittedfor one class.Each three bits makes one octal digit.<ACLASS="xref"HREF="ch01_23.htm#UPT-ART-1026-FIG-1"TITLE="Changing Permission Bits to an Octal Number">Figure 1.6</A>shows how to do it.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-1026-FIG-1">Figure 1.6: Changing Permission Bits to an Octal Number</A></H4><IMGCLASS="graphic"SRC="figs/1026b.gif"ALT="Figure 1.6"><PCLASS="para">Let's turn the mode bits 111101001 into an octal number.Break it into chunks of three bits: 111 101 001. The first group, 111,is 4+2+1 or 7. The second group, 101, is 4+0+1 or 5. The third group,001, is 0+0+1 or 1. So those mode bits can be written as the octal number 751.</P><PCLASS="para">To tie this together, look at<ACLASS="xref"HREF="ch01_23.htm#UPT-ART-1026-FIG-0"TITLE="Filesystem Permission Bits">Figure 1.5</A>again-and work out these examples yourself.For example, if the owner of a file has read and write access,but no one else is allowed to touch the file, we say that it has theaccess mode 600. A file that is readable, writable, and executableby everyone has access mode 777. A file that is readable andwritable by everyone (i.e., a public text file) has mode 666.</P><PCLASS="para">It is also common to see the mode bits expressed as a sequence often alphabetic characters (look at the listing from<SPANCLASS="link"><EMCLASS="emphasis">ls -l</EM> (<ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>).The first character tells you the file's type. For a plain file, thischaracter is a <CODECLASS="literal">-</CODE>. For a directory, it's a <CODECLASS="literal">d</CODE>. Thenext three bits report the owner's access, the middle three bitsreport group access, and the final three bits report access for others.An <CODECLASS="literal">r</CODE> indicates that read access is allowed, <CODECLASS="literal">w</CODE> indicatesthat write access is allowed, and <CODECLASS="literal">x</CODE> indicates that executeaccess is allowed. For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">-rw-------<ICLASS="lineannotation">is mode 600</I>-rwxrwxrwx<ICLASS="lineannotation">is mode 777</I>-rw-rw-rw-<ICLASS="lineannotation">is mode 666</I></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2201"></A><ACLASS="indexterm"NAME="AUTOID-2202"></A>You can change a string like <CODECLASS="literal">rw-rw-rw-</CODE> into an octal numberwith the technique in<ACLASS="xref"HREF="ch01_23.htm#UPT-ART-1026-FIG-1"TITLE="Changing Permission Bits to an Octal Number">Figure 1.6</A>Split it into three-bit chunks.For example, <CODECLASS="literal">rw-</CODE> would have the value 4+2+0-that's 6.Therefore, <CODECLASS="literal">rw-rw-rw-</CODE> is 666 octal.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2209"></A>If the file is executable, a few other bits come into play. One isthe "sticky bit," which tells UNIX to leave the executable in memoryafter the program has finished running. In theory, leaving theexecutable around reduces the program's startup time for subsequentusers. The sticky bit was aninteresting idea a long time ago, but it is obsolete now: modernvirtual memory techniques like demand paging have made itunnecessary. Many UNIX users and UNIX books still believethat the sticky bit does something important, so you will hear itmentioned from time to time.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2212"></A><ACLASS="indexterm"NAME="AUTOID-2214"></A><ACLASS="indexterm"NAME="AUTOID-2216"></A><ACLASS="indexterm"NAME="AUTOID-2219"></A>More important are the "set user ID" and "set group ID" (SUID andSGID) bits. If you execute an SUID file, your user ID is set to theuser ID of the file's owner. Therefore, if you execute an SUID filethat is owned by root, you are the superuser-for the durationof the program. Likewise, executing an SGID file sets your group ID tothe file's group while the file is executing. SUID and SGID files canbe security holes, but they really exist to enhance security. Forexample, you might want to allow any user to create a backup tape,but you shouldn't give every user the root password. Therefore, you<ACLASS="indexterm"NAME="AUTOID-2222"></A><ACLASS="indexterm"NAME="AUTOID-2225"></A><ACLASS="indexterm"NAME="AUTOID-2228"></A>can create a special version of the <EMCLASS="emphasis">dump</EM> utility that is ownedby root and that has the SUID bit set. When a user invokes thisutility, he or she will be able to back up the entire filesystembecause the <EMCLASS="emphasis">dump</EM> command will run as if it were executed byroot. But the user can't do anything else: he doesn't know thesuperuser password and can't do anything that <EMCLASS="emphasis">dump</EM> won't let himdo. Used carefully, SUID programs can be a powerful administrative tool.<BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> SUID and SGID programs are such major security holes that manyconscientious administrators refuse to add new SUID utilities.Some versions of UNIX ignore the SUID andSGID bits for shell scripts (command files)-on those versions, only compiledprogramscan be SUID or SGID. SUID and SGID programs always losetheir special properties when they are copied. However, makingSUID and SGID programs completely safe is very difficult (ormaybe impossible). For better or forworse, a lot of standard UNIX utilities (<EMCLASS="emphasis">uucp</EM> and <EMCLASS="emphasis">lpr</EM>, forexample) are SUID.<ACLASS="indexterm"NAME="AUTOID-2237"></A><ACLASS="indexterm"NAME="AUTOID-2238"></A><ACLASS="indexterm"NAME="AUTOID-2239"></A><ACLASS="indexterm"NAME="AUTOID-2240"></A>Article<ACLASS="xref"HREF="ch22_01.htm#UPT-ART-5010"TITLE="Introduction to File Ownership and Security ">22.1</A>introduces other information about file access permissions.</P></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_22.htm"TITLE="1.22 How UNIX Keeps Track of Files: Inodes "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.22 How UNIX Keeps Track of Files: Inodes "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_24.htm"TITLE="1.24 The Superuser (Root) "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.24 The Superuser (Root) "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.22 How UNIX Keeps Track of Files: Inodes </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">1.24 The Superuser (Root) </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -