⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch22_22.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 22] 22.22 The su Command Isn't Just for the Superuser </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:40:45Z"><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_21.htm"TITLE="22.21 How to Change File Ownership Without chown"><LINKREL="next"HREF="ch23_01.htm"TITLE="23. Removing Files"></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_21.htm"TITLE="22.21 How to Change File Ownership Without chown"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 22.21 How to Change File Ownership Without chown"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="CHAPTER"HREF="ch23_01.htm"TITLE="23. Removing Files"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23. Removing Files"BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1920">22.22 The su Command Isn't Just for the Superuser </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1920-IX-SU-COMMAND"></A><ACLASS="indexterm"NAME="AUTOID-24925"></A><ACLASS="indexterm"NAME="AUTOID-24928"></A>System administrators use the <EMCLASS="emphasis">su</EM> command to become the<SPANCLASS="link">superuser (<ACLASS="linkend"HREF="ch01_24.htm"TITLE="The Superuser (Root) ">1.24</A>)</SPAN>.But you can use it for lots more:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Become another user temporarily, without logging off your account.</P></LI><LICLASS="listitem"><PCLASS="para">Become another user without tying up another terminal port.</P></LI><LICLASS="listitem"><PCLASS="para">Switch between multiple users any time (on systems with job control).</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-24943"></A>Do a &quot;quick login&quot; to another user's account, especially when the system is busy.</P></LI></UL><PCLASS="para">When you type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">youraccount% <CODECLASS="userinput"><B>su </B></CODE><CODECLASS="replaceable"><I>whoever</I></CODE>Password:whoever%</PRE></BLOCKQUOTE></P><PCLASS="para">UNIX starts a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>that runs as the user <CODECLASS="replaceable"><I>whoever</I></CODE>.After you use the <EMCLASS="emphasis">cd</EM> command to go to the user's home directory, you canrun commands as if you'd logged into that account (more or less... see below).</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1920-SECT-1.1">22.22.1 Ending or Suspending </A></H3><PCLASS="para">End the subshell and go back to the account where you typed <CODECLASS="literal">su</CODE> with the<SPANCLASS="link"><EMCLASS="emphasis">exit</EM> (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>command or aCTRL-d.</P><PCLASS="para">Or, on systems with<SPANCLASS="link">job control (<ACLASS="linkend"HREF="ch12_08.htm"TITLE="Job Control in a Nutshell ">12.8</A>)</SPAN>,you can stop the subshell temporarily and go back to the account where youstarted the <EMCLASS="emphasis">su</EM>.To do that, type <CODECLASS="literal">suspend</CODE> if <CODECLASS="replaceable"><I>whoever</I></CODE>'s shell has job control(most shells do); otherwise, enterCTRL-zat the shell prompt.&#13;</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> If the <EMCLASS="emphasis">su</EM> subshell doesn't have job control but your starting shell does, enteringCTRL-zto <EMCLASS="emphasis">any</EM> command you run from the subshell will stop the command <EMCLASS="emphasis">and thesubshell</EM>.</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-24972"></A><ACLASS="indexterm"NAME="AUTOID-24975"></A><ACLASS="indexterm"NAME="AUTOID-24979"></A>You can use <EMCLASS="emphasis">suspend</EM> to start multiple <EMCLASS="emphasis">su</EM> sessions from the sameshell.You can go back to your original login, from any of those sessions,without losing your shell history, current directory, etc.Because these shells run on the same<SPANCLASS="link">tty (<ACLASS="linkend"HREF="ch03_08.htm"TITLE="What tty Am I On? ">3.8</A>)</SPAN>as your login shell, <EMCLASS="emphasis">su</EM>doesn't tie up other tty/pty ports like multiple logins or multiplewindows can.This is helpful on busy machines with lots of users.</P><PCLASS="para">On any UNIX system, you can type <CODECLASS="literal">exit</CODE> (or use CTRL-d)to go backto the original login.But on systems with job control, you can <EMCLASS="emphasis">su</EM> to several other users andjump back to your original login at any time.Job control lets you suspend an <EMCLASS="emphasis">su</EM> and go back to theplace you left off without typing another <EMCLASS="emphasis">su</EM> (and password).Many shells have a <EMCLASS="emphasis">suspend</EM> command that lets you do that.On other shells, you may be able to enter CTRL-z(your job suspendcharacter) or make a command<SPANCLASS="link">alias (<ACLASS="linkend"HREF="ch10_04.htm"TITLE="Aliases in ksh and bash ">10.4</A>)</SPAN>to stop the current shell:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch38_10.htm"TITLE="38.10 Destroying Processes with kill ">kill</A> <ACLASS="co"HREF="ch08_19.htm"TITLE='8.19 "Special&quot; Characters and Operators '>$$</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">alias suspend='kill -STOP $$'</PRE></TD></TR></TABLE></P><PCLASS="para">Here's a demo.I'm logged in to the account <EMCLASS="emphasis">jerry</EM> on the computer <EMCLASS="emphasis">wheeze</EM>.I've <EMCLASS="emphasis">su</EM>ed to the superuser, <EMCLASS="emphasis">sarah</EM>, and <EMCLASS="emphasis">manuals</EM>accounts, too.I'm using job control to switch users:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">jerry@wheeze% <CODECLASS="userinput"><B>jobs</B></CODE>[1]    Stopped      su[2] -  Stopped      su sarah[3] +  Stopped      su manualsjerry@wheeze% <CODECLASS="userinput"><B>fg</B></CODE>su manuals   ...<EMCLASS="emphasis">Do stuff as manuals</EM>...manuals@wheeze% <CODECLASS="userinput"><B>suspend</B></CODE>Stoppedjerry@wheeze% <CODECLASS="userinput"><B>fg %1</B></CODE>suwheeze#   ...<EMCLASS="emphasis">Do stuff as root</EM>...wheeze# <CODECLASS="userinput"><B>suspend</B></CODE>Stoppedjerry@wheeze%</PRE></BLOCKQUOTE></P><PCLASS="para">I use that so much that I've made a single-letter<SPANCLASS="link">alias (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>named <EMCLASS="emphasis">z</EM> that does a <EMCLASS="emphasis">suspend</EM>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1920-SECT-1.2">22.22.2 Who Are You Now? </A></H3><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="whoami">whoami</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="id">id</A><BR></TH><TDVALIGN="TOP">It's easier to jump between accounts if the shells'<SPANCLASS="link">prompts (<ACLASS="linkend"HREF="ch07_01.htm#UPT-ART-4820"TITLE="Why Change Your Prompt? ">7.1</A>)</SPAN>have the usernamein them, as shown above.If not,use the command <EMCLASS="emphasis">whoami</EM> or <EMCLASS="emphasis">id</EM> to see which user you are.Your system should have one or both; both GNU versions are on theCD-ROM. Also, to see your original login name (the account where you started the<EMCLASS="emphasis">su</EM>), try<CODECLASS="literal">who&nbsp;am&nbsp;i</CODE>(with spaces).</TD></TR></TABLE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1920-SECT-1.3">22.22.3 Problems You Might Have </A></H3><PCLASS="para">Some System V versions don't change the environment variable<SPANCLASS="link"><EMCLASS="emphasis">HOME</EM> (<EMCLASS="emphasis">LOGDIR</EM>) (<ACLASS="linkend"HREF="ch14_11.htm"TITLE="Finding (Anyone's) Home Directory, Quickly ">14.11</A>)</SPAN>to the right value for the account you <EMCLASS="emphasis">su</EM> to.That means a <EMCLASS="emphasis">cd</EM> command will take you to the home directory ofyour original login, not the home directory of your <EMCLASS="emphasis">su</EM>ed account.Also, a C shell you start on the other account won't read your <EMCLASS="emphasis">.cshrc</EM>file.The best fix for that is a shell script named <EMCLASS="emphasis">su</EM> that sets the variablefor you.The script is run by the<SPANCLASS="link">C shell <CODECLASS="literal">:-(</CODE> (<ACLASS="linkend"HREF="ch47_02.htm"TITLE="C Shell Programming Considered Harmful ">47.2</A>)</SPAN>because <EMCLASS="emphasis">csh</EM> has the<SPANCLASS="link"><CODECLASS="literal">~</CODE> (tilde) operator (<ACLASS="linkend"HREF="ch14_11.htm"TITLE="Finding (Anyone's) Home Directory, Quickly ">14.11</A>)</SPAN>for finding the account's home directory.<SPANCLASS="link"> Add this script to a directory before <EMCLASS="emphasis">/bin</EM> in your path  (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>or make an alias or shell function that runs the script instead of the standard<EMCLASS="emphasis">su</EM>.</P><PCLASS="para"><TABLE

⌨️ 快捷键说明

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