173-176.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 121 行

HTML
121
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:File and Directory Permissions</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=9//-->

<!--PAGES=173-176//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch08/170-172.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="176-179.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 9<BR>File and Directory Permissions

</FONT></H2>

<P><I>by Tim Parker</I></P>

<DL>

<DT><B>In This Chapter</B>

<DT>&#149;&nbsp;&nbsp; File and directory ownership

<DT>&#149;&nbsp;&nbsp; UMASK settings

<DT>&#149;&nbsp;&nbsp; Changing file permissions

<DT>&#149;&nbsp;&nbsp; Changing directory permissions

</DL>

<P>If there&#146;s one subject that tends to confuse newcomers to UNIX and Linux, it&#146;s working with file permissions. This subject is confusing because it has rarely been properly explained. After you understand what is going on and what the file permission commands do, you&#146;ll readily understand this subject. Take your time working through this material and you should find it fairly clear and easy to grasp.

</P>

<P>You already saw that when you perform a long directory listing with the <TT>ls -l</TT> command, there&#146;s a lot of information displayed other than the filename. For example, look at this long directory listing:</P>

<!-- CODE SNIP //-->

<PRE>

-rwxr-xr-x   2 tparker group      4512 May 9 09:20 book1

-rwxr-xr-x   2 tparker group      5727 May 9 09:20 book

</PRE>

<!-- END CODE SNIP //-->

<P>There are seven fields of data in this directory listing. From left to right they are:

</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;A set of permissions (which we&#146;ll explain shortly)

<DD><B>&#149;</B>&nbsp;&nbsp;The number of links (we&#146;ll worry about that in another chapter)

<DD><B>&#149;</B>&nbsp;&nbsp;The user ID that owns the files (in this case, <TT>tparker</TT>)

<DD><B>&#149;</B>&nbsp;&nbsp;The group that owns the files (in this case, <TT>group</TT>)

<DD><B>&#149;</B>&nbsp;&nbsp;The size of the file in bytes

<DD><B>&#149;</B>&nbsp;&nbsp;The date and time the file was created

<DD><B>&#149;</B>&nbsp;&nbsp;The name of the file

</DL>

<P>In this chapter we&#146;re going to concentrate on the permissions, owner, and group of the file.

</P>

<P>Every Linux file and directory has an owner and group, as well as a set of permissions. You can change the permissions and ownership of your files and directories to provide greater or lesser access. File permissions also determine whether a file can be executed as a command or not.</P>

<H3><A NAME="Heading2"></A><FONT COLOR="#000077">File and Directory Ownership</FONT></H3>

<P>When you create a file, you are that file&#146;s owner by default, and your login appears in the third column of the directory listing. Whatever group you are in when the file is created is placed in the fourth column. (We haven&#146;t talked about groups yet, but they are simply a number of user IDs that have a similar group name for common access to files and directories.) Being the file&#146;s owner gives you the privilege of changing the file&#146;s permissions or ownership. Of course, once you change the ownership to another user, you will probably not be able to change the ownership or permissions anymore.

</P>

<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Users and Ownership</FONT></H4>

<P>File <I>owners</I> are set up by the system during installation. Linux system files are owned by IDs such as <TT>root</TT>, <TT>uucp</TT>, and <TT>bin</TT>. Do not change the ownership of these files, even if you are logged in as <TT>root</TT>. That&#146;s because the permissions are set correctly when Linux installs the software, and if you change them, you may prevent the system from accessing the file properly.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>Although it&#146;s sometimes tempting to change ownerships of system files, this can cause real problems for utilities that don&#146;t run as <TT>root</TT> and need to read those files. In most cases if you change the ownership or the permissions of a system file, the utilities that use that file will lock up or terminate. Change ownership of files only when you know they are user files!<HR></FONT>

</BLOCKQUOTE>

<P>You can use the <TT>chown</TT> (change ownership) command to change the ownership of a file. The syntax of the <TT>chown</TT> command is</P>

<!-- CODE SNIP //-->

<PRE>

chown &lt;<I>owner</I>&gt; &lt;<I>filename</I>&gt;.

</PRE>

<!-- END CODE SNIP //-->

<P><TT>owner</TT> indicates where to insert the user ID that will own the file, and <TT>filename</TT> indicates the name of the file that you are setting ownership for. You can use wildcards with the <TT>chown</TT> command to indicate a number of files at once.</P>

<P>In the following example, you change the ownership of the file <TT>myfile</TT> to the login <TT>bill</TT>:</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>ls -l myfile</B>

-rw-r--r--   1 fido   users      114 Dec 7 14:31 myfile

darkstar:~&#36; <B>chown bill myfile</B>

darkstar:~&#36; <B>ls -l myfile</B>

-rw-r--r--   1 bill   users      114 Dec 7 14:31 myfile

</PRE>

<!-- END CODE SNIP //-->

<P>Before you can change the login of the file owner, Linux checks to confirm that it exists. After you have changed the ownership of the file, you may not be able to do anything else with the file, so be careful. To make any further changes to the file <TT>myfile</TT> or to <TT>chown</TT> it back to <TT>fido</TT>, you must use either <TT>su</TT> or log in as <TT>bill</TT> and use the <TT>chown</TT> command to set you back to the owner.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>Although Linux shows you the login name that owns the files and directories, it really keeps track of this information through the user ID number (such as 101). Linux prefers numbers to characters because they are easier to manipulate.<HR></FONT>

</BLOCKQUOTE>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>It is possible to have files owned by a user who doesn&#146;t exist. This usually occurs when the system administrator deletes a user. All the files that user ID owns are still tagged with that ownership because Linux doesn&#146;t scan the filesystem and change these names when a user is deleted. In the case of a file owner that doesn&#146;t exist, <TT>root</TT> is usually the only login that can change the ownership.<HR></FONT>

</BLOCKQUOTE>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch08/170-172.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="176-179.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?