📄 ch22_21.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 22] 22.21 How to Change File Ownership Without chown</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:43Z"><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_20.htm"TITLE="22.20 Why Can't You Change File Ownership Under BSD UNIX?"><LINKREL="next"HREF="ch22_22.htm"TITLE="22.22 The su Command Isn't Just for the Superuser "></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_20.htm"TITLE="22.20 Why Can't You Change File Ownership Under BSD UNIX?"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 22.20 Why Can't You Change File Ownership Under BSD UNIX?"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_22.htm"TITLE="22.22 The su Command Isn't Just for the Superuser "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 22.22 The su Command Isn't Just for the Superuser "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6640">22.21 How to Change File Ownership Without chown</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-24881"></A><ACLASS="indexterm"NAME="AUTOID-24883"></A>UNIX systems with<SPANCLASS="link">disk quotas (<ACLASS="linkend"HREF="ch24_17.htm"TITLE="Disk Quotas ">24.17</A>)</SPAN><SPANCLASS="link">won't let you change the owner (<ACLASS="linkend"HREF="ch22_20.htm"TITLE="Why Can't You Change File Ownership Under BSD UNIX?">22.20</A>)</SPAN>of a file;only the superuser can use <EMCLASS="emphasis">chown</EM>.Here's a workaround for those systems.</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">The file's current owner should make sure that the new owner has writepermission on the directory where the file is and read permission on thefile itself:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">jerry% <CODECLASS="userinput"><B>ls -ld . afile</B></CODE>drwxr-xr-x 2 jerry 512 Aug 10 12:20 .-rw-r--r-- 1 jerry 1934 Aug 10 09:34 afilejerry% <CODECLASS="userinput"><B>chmod go+w .</B></CODE></PRE></BLOCKQUOTE></P></LI><LICLASS="listitem"><PCLASS="para">The new owner (logged in as herself) should rename the file, make acopy, and delete the original file.If the new owner is there at the same time,<SPANCLASS="link"><EMCLASS="emphasis">su</EM> (<ACLASS="linkend"HREF="ch22_22.htm"TITLE="The su Command Isn't Just for the Superuser ">22.22</A>)</SPAN>is probably the fastest way to change accounts:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch23_10.htm"TITLE="23.10 Deletion with Prejudice: rm -f ">-f</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">jerry% <CODECLASS="userinput"><B>su laura</B></CODE>Password:laura% <CODECLASS="userinput"><B>mv afile afile.tmp</B></CODE>laura% <CODECLASS="userinput"><B>cp -p afile.tmp afile</B></CODE>laura% <CODECLASS="userinput"><B>ls -l afile</B></CODE>-rw-r--r-- 1 laura 1934 Aug 10 09:34 afilelaura% <CODECLASS="userinput"><B>rm -f afile.tmp</B></CODE>laura% <CODECLASS="userinput"><B>exit</B></CODE>jerry% <CODECLASS="userinput"><B>chmod go-w .</B></CODE></PRE></TD></TR></TABLE></P></LI></OL><PCLASS="para">The<ACLASS="indexterm"NAME="AUTOID-24911"></A><SPANCLASS="link"><EMCLASS="emphasis">cp -p</EM> (<ACLASS="linkend"HREF="ch18_15.htm"TITLE="Copying Directory Trees with cp -r ">18.15</A>)</SPAN>command preserves the file's original permissions andlast modification time.After the new owner (<EMCLASS="emphasis">laura</EM>) is done copying, the old owner (<EMCLASS="emphasis">jerry</EM>)takes away the directory's write permission again.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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_20.htm"TITLE="22.20 Why Can't You Change File Ownership Under BSD UNIX?"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 22.20 Why Can't You Change File Ownership Under BSD UNIX?"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_22.htm"TITLE="22.22 The su Command Isn't Just for the Superuser "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 22.22 The su Command Isn't Just for the Superuser "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">22.20 Why Can't You Change File Ownership Under BSD UNIX?</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.22 The su Command Isn't Just for the Superuser </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 + -