📄 ch22_07.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 22] 22.7 Using chmod to Change File Permission </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:40:27Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch22_01.htm"TITLE="22. File Security, Ownership, and Sharing"><LINKREL="prev"HREF="ch22_06.htm"TITLE="22.6 Protecting Files with the Sticky Bit "><LINKREL="next"HREF="ch22_08.htm"TITLE="22.8 The Handy chmod = Operator "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch22_06.htm"TITLE="22.6 Protecting Files with the Sticky Bit "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 22.6 Protecting Files with the Sticky Bit "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 22<BR>File Security, Ownership, and Sharing</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch22_08.htm"TITLE="22.8 The Handy chmod = Operator "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 22.8 The Handy chmod = Operator "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2991">22.7 Using chmod to Change File Permission </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-24067"></A><ACLASS="indexterm"NAME="AUTOID-24069"></A>To change a file's permissions, you need to use the <EMCLASS="emphasis">chmod</EM>command and you must be the file's owner or root.The command's syntax is pretty simple:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>chmod <CODECLASS="replaceable"><I>new-mode file(s)</I></CODE></B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="replaceable"><I>new-mode</I></CODE> describes the access permissions you want<EMCLASS="emphasis">after</EM> the change. There are twoways to specify the mode: you can use either a <EMCLASS="emphasis">numericmode</EM> or some symbols that describe the changes.I generally prefer the numeric mode (because I'm strange, Isuppose). Anyway, to usea numeric mode, decide what permissions youwant to have, <SPANCLASS="link">express them as an octal number (<ACLASS="linkend"HREF="ch01_23.htm"TITLE="File Access Permissions ">1.23</A>, <ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>,and give a command like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>chmod 644 report.txt</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This gives read and write access to the owner of <EMCLASS="emphasis">report.txt</EM>and read-only access to everyone else.</P><PCLASS="para">Most users prefer to use the <EMCLASS="emphasis">symbolic mode</EM> to specify permissions.A symbolic <EMCLASS="emphasis">chmod</EM> command looks like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>chmod g-w report.txt</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This means "take away write access for group members."The symbols used in mode specifications are shown in <ACLASS="xref"HREF="ch22_07.htm#UPT-ART-2991-TAB-0"TITLE="chmod Symbolic Modes">Table 22.1</A>. </P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-2991-TAB-0">Table 22.1: chmod Symbolic Modes</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Category</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Mode</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Description</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Who:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">u</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">User (owner) of the file.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">g</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Group members.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">o</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Others.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">a</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">All (i.e., user, group, and others).</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">What to do:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">-</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Take away this permission. </TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">+</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Add the indicated permission.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">=</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><SPANCLASS="link">Set exactly this permission (<ACLASS="linkend"HREF="ch22_08.htm"TITLE="The Handy chmod = Operator ">22.8</A>)</SPAN>.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Permissions:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">r</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Read access.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">w</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Write access.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">x</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Execute access.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">X</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Give (or deny) execute permission to directories, or to filesthat have another "execute" bit set.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">s</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Set user or group ID (only valid with <CODECLASS="literal">+</CODE> or <CODECLASS="literal">-</CODE>).</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">t</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Set the<SPANCLASS="link">"sticky bit" (<ACLASS="linkend"HREF="ch22_06.htm"TITLE="Protecting Files with the Sticky Bit ">22.6</A>, <ACLASS="linkend"HREF="ch01_23.htm"TITLE="File Access Permissions ">1.23</A>)</SPAN>.</TD></TR></TBODY></TABLE><PCLASS="para">(Article<ACLASS="xref"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>explains the "Who" and "Permissions" categories.)Here are a few example symbolic modes: </P><DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="literal">o=r</CODE></DT><DDCLASS="listitem"><PCLASS="para">Set others access to read-only, regardless of what other bits are set.</P></DD><DTCLASS="term"><CODECLASS="literal">o+r</CODE></DT><DDCLASS="listitem"><PCLASS="para">Add read access for others.</P></DD><DTCLASS="term"><CODECLASS="literal">go-w</CODE></DT><DDCLASS="listitem"><PCLASS="para">Take away write access for group members and others.</P></DD><DTCLASS="term"><CODECLASS="literal">a=rw</CODE></DT><DDCLASS="listitem"><PCLASS="para">Give everyone (user, group, and others) read-write (but not execute) access. </P></DD></DL><PCLASS="para">Remember that <CODECLASS="literal">+</CODE> and <CODECLASS="literal">-</CODE> add or delete certain permissions, butleave the others untouched. The commands below show how permissionsare added and subtracted:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -l foo</B></CODE>-rwx---x 1 mikel 0 Mar 30 11:02 foo% <CODECLASS="userinput"><B>chmod a+x foo</B></CODE>% <CODECLASS="userinput"><B>ls -l foo</B></CODE>-rwx-x-x 1 mikel 0 Mar 30 11:02 foo% <CODECLASS="userinput"><B>chmod o-x,g+r foo</B></CODE>% <CODECLASS="userinput"><B>ls -l foo</B></CODE>-rwxr-x-- 1 mikel 0 Mar 30 11:02 foo%</PRE></BLOCKQUOTE></P><PCLASS="para">Note the last <EMCLASS="emphasis">chmod</EM> command. It shows something we haven'tmentioned before. With symbolic mode, you're allowed to combine two(or more) specifications, separated by commas. This command says"take away execute permission for others, and add read access forgroup members."</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="chmod">chmod</A><BR></TH><TDVALIGN="TOP"> The GNU version of <EMCLASS="emphasis">chmod</EM> is on the CD-ROM.</TD></TR></TABLE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-24227"></A><ACLASS="indexterm"NAME="AUTOID-24230"></A><ACLASS="indexterm"NAME="AUTOID-24233"></A>On occasion, I've wanted to change the permissions of a wholedirectory tree: all the files in a directory and all of itssubdirectories. In this case, you want to use <EMCLASS="emphasis">chmod -R</EM> (the <EMCLASS="emphasis">R</EM>stands for recursive) or<SPANCLASS="link"><EMCLASS="emphasis">find -exec</EM> (<ACLASS="linkend"HREF="ch17_10.htm"TITLE="Running Commands on What You Find ">17.10</A>)</SPAN>.You won't need this often, but when you do,it's a real lifesaver.</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="ch22_06.htm"TITLE="22.6 Protecting Files with the Sticky Bit "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 22.6 Protecting Files with the Sticky Bit "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="ch22_08.htm"TITLE="22.8 The Handy chmod = Operator "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 22.8 The Handy chmod = Operator "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">22.6 Protecting Files with the Sticky Bit </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">22.8 The Handy chmod = Operator </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 + -