📄 system.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>System and Administrative Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Beyond the Basics"HREF="part3.html"><LINKREL="PREVIOUS"TITLE="Miscellaneous Commands"HREF="extmisc.html"><LINKREL="NEXT"TITLE="Command Substitution"HREF="commandsub.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="extmisc.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="commandsub.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="SYSTEM">Chapter 13. System and Administrative Commands</A></H1><P>The startup and shutdown scripts in <TTCLASS="FILENAME">/etc/rc.d</TT> illustrate the uses (and usefulness) of many of these comands. These are usually invoked by root and used for system maintenance or emergency filesystem repairs. Use with caution, as some of these commands may damage your system if misused.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="USERSGROUPS1"></A>Users and Groups</B></P><DL><DT><BCLASS="COMMAND">users</B></DT><DD><P>Show all logged on users. This is the approximate equivalent of <BCLASS="COMMAND">who -q</B>.</P></DD><DT><BCLASS="COMMAND">groups</B></DT><DD><P>Lists the current user and the groups she belongs to. This corresponds to the <AHREF="variables2.html#GROUPSREF">$GROUPS</A> internal variable, but gives the group names, rather than the numbers.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>groups</B></TT> <TTCLASS="COMPUTEROUTPUT">bozita cdrom cdwriter audio xgrp</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $GROUPS</B></TT> <TTCLASS="COMPUTEROUTPUT">501</TT></PRE></TD></TR></TABLE></DD><DT><BCLASS="COMMAND">chown</B>, <BCLASS="COMMAND">chgrp</B></DT><DD><P>The <BCLASS="COMMAND">chown</B> command changes the ownership of a file or files. This command is a useful method that <TTCLASS="REPLACEABLE"><I>root</I></TT> can use to shift file ownership from one user to another. An ordinary user may not change the ownership of files, not even her own files. <ANAME="AEN10818"HREF="#FTN.AEN10818">[1]</A> </P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">root# </TT><TTCLASS="USERINPUT"><B>chown bozo *.txt</B></TT> <TTCLASS="COMPUTEROUTPUT"></TT> </PRE></TD></TR></TABLE> </P><P>The <BCLASS="COMMAND">chgrp</B> command changes the <TTCLASS="REPLACEABLE"><I>group</I></TT> ownership of a file or files. You must be owner of the file(s) as well as a member of the destination group (or <TTCLASS="REPLACEABLE"><I>root</I></TT>) to use this operation. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 chgrp --recursive dunderheads *.data 2 # The "dunderheads" group will now own all the "*.data" files 3 #+ all the way down the $PWD directory tree (that's what "recursive" means).</PRE></TD></TR></TABLE></P></DD><DT><BCLASS="COMMAND">useradd</B>, <BCLASS="COMMAND">userdel</B></DT><DD><P>The <BCLASS="COMMAND">useradd</B> administrative command adds a user account to the system and creates a home directory for that particular user, if so specified. The corresponding <BCLASS="COMMAND">userdel</B> command removes a user account from the system <ANAME="AEN10849"HREF="#FTN.AEN10849">[2]</A> and deletes associated files.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">adduser</B> command is a synonym for <BCLASS="COMMAND">useradd</B> and is usually a symbolic link to it.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">usermod</B></DT><DD><P>Modify a user account. Changes may be made to the password, group membership, expiration date, and other attributes of a given user's account. With this command, a user's password may be locked, which has the effect of disabling the account.</P></DD><DT><BCLASS="COMMAND">groupmod</B></DT><DD><P>Modify a given group. The group name and/or ID number may be changed using this command.</P></DD><DT><ANAME="IDREF"></A><BCLASS="COMMAND">id</B></DT><DD><P>The <BCLASS="COMMAND">id</B> command lists the real and effective user IDs and the group IDs of the user associated with the current process. This is the counterpart to the <AHREF="variables2.html#UIDREF">$UID</A>, <AHREF="variables2.html#EUIDREF">$EUID</A>, and <AHREF="variables2.html#GROUPSREF">$GROUPS</A> internal Bash variables.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>id</B></TT> <TTCLASS="COMPUTEROUTPUT">uid=501(bozo) gid=501(bozo) groups=501(bozo),22(cdrom),80(cdwriter),81(audio)</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $UID</B></TT> <TTCLASS="COMPUTEROUTPUT">501</TT></PRE></TD></TR></TABLE><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">id</B> command shows the <ICLASS="EMPHASIS">effective</I> IDs only when they differ from the <ICLASS="EMPHASIS">real</I> ones.</P></TD></TR></TABLE></DIV><P>Also see <AHREF="variables2.html#AMIROOT">Example 9-5</A>.</P></DD><DT><ANAME="WHOREF"></A><BCLASS="COMMAND">who</B></DT><DD><P>Show all users logged on to the system.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>who</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo tty1 Apr 27 17:45 bozo pts/0 Apr 27 17:46 bozo pts/1 Apr 27 17:47 bozo pts/2 Apr 27 17:49</TT> </PRE></TD></TR></TABLE> </P><P>The <TTCLASS="OPTION">-m</TT> gives detailed information about only the current user. Passing any two arguments to <BCLASS="COMMAND">who</B> is the equivalent of <BCLASS="COMMAND">who -m</B>, as in <BCLASS="COMMAND">who am i</B> or <BCLASS="COMMAND">who The Man</B>.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>who -m</B></TT> <TTCLASS="COMPUTEROUTPUT">localhost.localdomain!bozo pts/2 Apr 27 17:49</TT> </PRE></TD></TR></TABLE> </P><P><ANAME="WHOAMIREF"></A><BCLASS="COMMAND">whoami</B> is similar to <BCLASS="COMMAND">who -m</B>, but only lists the user name.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>whoami</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo</TT> </PRE></TD></TR></TABLE> </P></DD><DT><BCLASS="COMMAND">w</B></DT><DD><P>Show all logged on users and the processes belonging to them. This is an extended version of <BCLASS="COMMAND">who</B>. The output of <BCLASS="COMMAND">w</B> may be piped to <BCLASS="COMMAND">grep</B> to find a specific user and/or process.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>w | grep startx</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo tty1 - 4:22pm 6:41 4.47s 0.45s startx</TT></PRE></TD></TR></TABLE></DD><DT><BCLASS="COMMAND">logname</B></DT><DD><P>Show current user's login name (as found in <TTCLASS="FILENAME">/var/run/utmp</TT>). This is a near-equivalent to <AHREF="system.html#WHOAMIREF">whoami</A>, above.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>logname</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>whoami</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo</TT></PRE></TD></TR></TABLE><P>However...</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>su</B></TT> <TTCLASS="COMPUTEROUTPUT">Password: ......</TT> <TTCLASS="PROMPT">bash# </TT><TTCLASS="USERINPUT"><B>whoami</B></TT> <TTCLASS="COMPUTEROUTPUT">root</TT> <TTCLASS="PROMPT">bash# </TT><TTCLASS="USERINPUT"><B>logname</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo</TT></PRE></TD></TR></TABLE><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>While <BCLASS="COMMAND">logname</B> prints the name of the logged in user, <BCLASS="COMMAND">whoami</B> gives the name of the user attached to the current process. As we have just seen, sometimes these are not the same.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="SUREF"></A><BCLASS="COMMAND">su</B></DT><DD><P>Runs a program or script as a <ICLASS="EMPHASIS">s</I>ubstitute <ICLASS="EMPHASIS">u</I>ser. <BCLASS="COMMAND">su rjones</B> starts a shell as user <ICLASS="EMPHASIS">rjones</I>. A naked <BCLASS="COMMAND">su</B> defaults to <ICLASS="EMPHASIS">root</I>. See <AHREF="contributed-scripts.html#FIFO">Example A-15</A>.</P></DD><DT><BCLASS="COMMAND">sudo</B></DT><DD><P>Runs a command as root (or another user). This may be used in a script, thus permitting a regular user to run the script.</P><P><TABLE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -