📄 system.html
字号:
>ubstitute <BCLASS="COMMAND">u</B>ser. <BCLASS="COMMAND">su rjones</B> starts a shell as user <SPANCLASS="emphasis"><ICLASS="EMPHASIS">rjones</I></SPAN>. A naked <BCLASS="COMMAND">su</B> defaults to <ICLASS="FIRSTTERM">root</I>. See <AHREF="contributed-scripts.html#FIFO">Example A-15</A>.</P></DD><DT><ANAME="SUDOREF"></A><BCLASS="COMMAND">sudo</B></DT><DD><P>Runs a command as <ICLASS="FIRSTTERM">root</I> (or another user). This may be used in a script, thus permitting a <ICLASS="FIRSTTERM">regular user</I> to run the script.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # Some commands. 4 sudo cp /root/secretfile /home/bozo/secret 5 # Some more commands.</PRE></TD></TR></TABLE></P><P>The file <TTCLASS="FILENAME">/etc/sudoers</TT> holds the names of users permitted to invoke <BCLASS="COMMAND">sudo</B>.</P></DD><DT><ANAME="PASSWDREF"></A><BCLASS="COMMAND">passwd</B></DT><DD><P>Sets, changes, or manages a user's password.</P><P>The <BCLASS="COMMAND">passwd</B> command can be used in a script, but probably <SPANCLASS="emphasis"><ICLASS="EMPHASIS">should not</I></SPAN> be.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SETNEWPW"></A><P><B>Example 16-1. Setting a new password</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # setnew-password.sh: For demonstration purposes only. 3 # Not a good idea to actually run this script. 4 # This script must be run as root. 5 6 ROOT_UID=0 # Root has $UID 0. 7 E_WRONG_USER=65 # Not root? 8 9 E_NOSUCHUSER=70 10 SUCCESS=0 11 12 13 if [ "$UID" -ne "$ROOT_UID" ] 14 then 15 echo; echo "Only root can run this script."; echo 16 exit $E_WRONG_USER 17 else 18 echo 19 echo "You should know better than to run this script, root." 20 echo "Even root users get the blues... " 21 echo 22 fi 23 24 25 username=bozo 26 NEWPASSWORD=security_violation 27 28 # Check if bozo lives here. 29 grep -q "$username" /etc/passwd 30 if [ $? -ne $SUCCESS ] 31 then 32 echo "User $username does not exist." 33 echo "No password changed." 34 exit $E_NOSUCHUSER 35 fi 36 37 echo "$NEWPASSWORD" | passwd --stdin "$username" 38 # The '--stdin' option to 'passwd' permits 39 #+ getting a new password from stdin (or a pipe). 40 41 echo; echo "User $username's password changed!" 42 43 # Using the 'passwd' command in a script is dangerous. 44 45 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <BCLASS="COMMAND">passwd</B> command's <TTCLASS="OPTION">-l</TT>, <TTCLASS="OPTION">-u</TT>, and <TTCLASS="OPTION">-d</TT> options permit locking, unlocking, and deleting a user's password. Only <ICLASS="FIRSTTERM">root</I> may use these options.</P></DD><DT><ANAME="ACREF"></A><BCLASS="COMMAND">ac</B></DT><DD><P>Show users' logged in time, as read from <TTCLASS="FILENAME">/var/log/wtmp</TT>. This is one of the GNU accounting utilities.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ac</B></TT> <TTCLASS="COMPUTEROUTPUT"> total 68.08</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="LASTREF"></A><BCLASS="COMMAND">last</B></DT><DD><P>List <SPANCLASS="emphasis"><ICLASS="EMPHASIS">last</I></SPAN> logged in users, as read from <TTCLASS="FILENAME">/var/log/wtmp</TT>. This command can also show remote logins.</P><P>For example, to show the last few times the system rebooted:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>last reboot</B></TT> <TTCLASS="COMPUTEROUTPUT">reboot system boot 2.6.9-1.667 Fri Feb 4 18:18 (00:02) reboot system boot 2.6.9-1.667 Fri Feb 4 15:20 (01:27) reboot system boot 2.6.9-1.667 Fri Feb 4 12:56 (00:49) reboot system boot 2.6.9-1.667 Thu Feb 3 21:08 (02:17) . . . wtmp begins Tue Feb 1 12:50:09 2005</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="NEWGRPREF"></A><BCLASS="COMMAND">newgrp</B></DT><DD><P>Change user's <ICLASS="FIRSTTERM">group ID</I> without logging out. This permits access to the new group's files. Since users may be members of multiple groups simultaneously, this command finds only limited use.</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>Kurt Glaesemann points out that the <ICLASS="FIRSTTERM">newgrp</I> command could prove helpful in setting the default group permissions for files a user writes. However, the <AHREF="system.html#CHGRPREF">chgrp</A> command might be more convenient for this purpose.</P></TD></TR></TABLE></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="TERMINALSSYS1"></A>Terminals</B></P><DL><DT><ANAME="TTYREF"></A><BCLASS="COMMAND">tty</B></DT><DD><P>Echoes the name of the current user's terminal. Note that each separate <ICLASS="FIRSTTERM">xterm</I> window counts as a different terminal.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>tty</B></TT> <TTCLASS="COMPUTEROUTPUT">/dev/pts/1</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="STTYREF"></A><BCLASS="COMMAND">stty</B></DT><DD><P>Shows and/or changes terminal settings. This complex command, used in a script, can control terminal behavior and the way output displays. See the info page, and study it carefully.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ERASE"></A><P><B>Example 16-2. Setting an <ICLASS="FIRSTTERM">erase</I> character</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # erase.sh: Using "stty" to set an erase character when reading input. 3 4 echo -n "What is your name? " 5 read name # Try to backspace 6 #+ to erase characters of input. 7 # Problems? 8 echo "Your name is $name." 9 10 stty erase '#' # Set "hashmark" (#) as erase character. 11 echo -n "What is your name? " 12 read name # Use # to erase last character typed. 13 echo "Your name is $name." 14 15 exit 0 16 17 # Even after the script exits, the new key value remains set. 18 # Exercise: How would you reset the erase character to the default value?</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="SECRETPW"></A><P><B>Example 16-3. <ICLASS="FIRSTTERM">secret password</I>: Turning off terminal echoing </B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # secret-pw.sh: secret password 3 4 echo 5 echo -n "Enter password " 6 read passwd 7 echo "password is $passwd" 8 echo -n "If someone had been looking over your shoulder, " 9 echo "your password would have been compromised." 10 11 echo && echo # Two line-feeds in an "and list." 12 13 14 stty -echo # Turns off screen echo. 15 16 echo -n "Enter password again " 17 read passwd 18 echo 19 echo "password is $passwd" 20 echo 21 22 stty echo # Restores screen echo. 23 24 exit 0 25 26 # Do an 'info stty' for more on this useful-but-tricky command.</PRE></TD></TR></TABLE><HR></DIV><P>A creative use of <BCLASS="COMMAND">stty</B> is detecting a user keypress (without hitting <BCLASS="KEYCAP">ENTER</B>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="KEYPRESS"></A><P><B>Example 16-4. Keypress detection</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # keypress.sh: Detect a user keypress ("hot keys"). 3 4 echo 5 6 old_tty_settings=$(stty -g) # Save old settings (why?). 7 stty -icanon 8 Keypress=$(head -c1) # or $(dd bs=1 count=1 2> /dev/null) 9 # on non-GNU systems 10 11 echo 12 echo "Key pressed was \""$Keypress"\"." 13 echo 14 15 stty "$old_tty_settings" # Restore old settings. 16 17 # Thanks, Stephane Chazelas. 18 19 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Also see <AHREF="variables2.html#TIMEOUT">Example 9-3</A>.</P><P><ANAME="TERMINALSREF"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN13777"></A><P><B>terminals and modes</B></P><P>Normally, a terminal works in the <ICLASS="FIRSTTERM">canonical</I> mode. When a user hits a key, the resulting character does not immediately go to the program actually running in this terminal. A buffer local to the terminal stores keystrokes. When the user hits the <BCLASS="KEYCAP">ENTER</B> key, this sends all the stored keystrokes to the program running. There is even a basic line editor inside the terminal.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>stty -a</B></TT> <TTCLASS="COMPUTEROUTPUT">speed 9600 baud; rows 36; columns 96; line = 0; intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; ... isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt</TT> </PRE></TD></TR></TABLE> </P><P>Using canonical mode, it is possible to redefine the special keys for the local terminal line editor. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat > filexxx</B></TT> <TTCLASS="USERINPUT"><B>wha<ctl-W>I<ctl-H>foo bar<ctl-U>hello world<ENTER></B></TT> <TTCLASS="USERINPUT"><B><ctl-D></B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat filexxx</B></TT> <TTCLASS="COMPUTEROUTPUT">hello world</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>wc -c < filexxx</B></TT> <TTCLASS="COMPUTEROUTPUT">12</TT> </PRE></TD></TR></TABLE> The process controlling the terminal receives only 12 characters (11 alphabetic ones, plus a newline), although the user hit 26 keys. </P><P>In non-canonical (<SPANCLASS="QUOTE">"raw"</SPAN>) mode, every key hit (including special editing keys such as <BCLASS="KEYCAP">ctl-H</B>) sends a character immediately to the controlling process.</P><P>The Bash prompt disables both <TTCLASS="OPTION">icanon</TT> and <TTCLASS="OPTION">echo</TT>, since it replaces the basic terminal line editor with its own more elaborate one. For example, when you hit <BCLASS="KEYCAP">ctl-A</B> at the Bash prompt, there's no <BCLASS="KEYCAP">^A</B> echoed by the terminal, but Bash gets a <BCLASS="KEYCAP">\1</B> character, interprets it, and moves the cursor to the begining of the line.</P><P><SPANCLASS="emphasis"><ICLASS="EMPHASIS">St閜hane Chazelas</I></SPAN></P></DIV></TD></TR></TABLE></DD><DT><ANAME="SETTERMREF"></A><BCLASS="COMMAND">setterm</B></DT><DD><P>Set certain terminal attributes. This command writes to its terminal's <TTCLASS="FILENAME">stdout</TT> a string that changes the behavior of that terminal.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>setterm -cursor off</B></TT> <TTCLASS="COMPUTEROUTPUT">bash$</TT> </PRE></TD></TR></TABLE> </P><P>The <BCLASS="COMMAND">setterm</B> command can be used within a script to change the appearance of text written to <TTCLASS="FILENAME">stdout</TT>, although there are certainly <AHREF="colorizing.html#COLORIZINGREF">better tools</A> available for this purpose.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 setterm -bold on 2 echo bold hello 3 4 setterm -bold off 5 echo normal hello</PRE></TD></TR></TABLE></P></DD><DT><ANAME="TSETREF"></A><BCLASS="COMMAND">tset</B></DT><DD><P>Show or initialize terminal settings. This is a less capable version of <BCLASS="COMMAND">stty</B>.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -