Chapter 13. System and Administrative Commands

The startup and shutdown scripts in /etc/rc.d 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.

Users and Groups

users

Show all logged on users. This is the approximate equivalent of who -q.

groups

Lists the current user and the groups she belongs to. This corresponds to the $GROUPS internal variable, but gives the group names, rather than the numbers.

 bash$ groups
 bozita cdrom cdwriter audio xgrp
 
 bash$ echo $GROUPS
 501
chown, chgrp

The chown command changes the ownership of a file or files. This command is a useful method that root 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. [1]

 root# chown bozo *.txt
 
 	      

The chgrp command changes the group ownership of a file or files. You must be owner of the file(s) as well as a member of the destination group (or root) to use this operation.
   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).

useradd, userdel

The useradd administrative command adds a user account to the system and creates a home directory for that particular user, if so specified. The corresponding userdel command removes a user account from the system [2] and deletes associated files.

Note

The adduser command is a synonym for useradd and is usually a symbolic link to it.

usermod

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.

groupmod

Modify a given group. The group name and/or ID number may be changed using this command.

id

The id 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 $UID, $EUID, and $GROUPS internal Bash variables.

 bash$ id
 uid=501(bozo) gid=501(bozo) groups=501(bozo),22(cdrom),80(cdwriter),81(audio)
 
 bash$ echo $UID
 501
Note

The id command shows the effective IDs only when they differ from the real ones.

Also see Example 9-5.

who

Show all users logged on to the system.

 bash$ who
 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
 	      

The -m gives detailed information about only the current user. Passing any two arguments to who is the equivalent of who -m, as in who am i or who The Man.

 bash$ who -m
 localhost.localdomain!bozo  pts/2    Apr 27 17:49
 	      

whoami is similar to who -m, but only lists the user name.

 bash$ whoami
 bozo
 	      

w

Show all logged on users and the processes belonging to them. This is an extended version of who. The output of w may be piped to grep to find a specific user and/or process.

 bash$ w | grep startx
 bozo  tty1     -                 4:22pm  6:41   4.47s  0.45s  startx
logname

Show current user's login name (as found in /var/run/utmp). This is a near-equivalent to whoami, above.

 bash$ logname
 bozo
 
 bash$ whoami
 bozo

However...

 bash$ su
 Password: ......
 
 bash# whoami
 root
 bash# logname
 bozo
Note

While logname prints the name of the logged in user, whoami gives the name of the user attached to the current process. As we have just seen, sometimes these are not the same.

su

Runs a program or script as a substitute user. su rjones starts a shell as user rjones. A naked su defaults to root. See Example A-15.

sudo

Runs a command as root (or another user). This may be used in a script, thus permitting a regular user to run the script.

   1 #!/bin/bash
   2 
   3 # Some commands.
   4 sudo cp /root/secretfile /home/bozo/secret
   5 # Some more commands.

The file /etc/sudoers holds the names of users permitted to invoke sudo.

passwd

Sets, changes, or manages a user's password.

The passwd command can be used in a script, but should not be.


Example 13-1. Setting a new password

   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

The passwd command's -l, -u, and -d options permit locking, unlocking, and deleting a user's password. Only root may use these options.

ac

Show users' logged in time, as read from /var/log/wtmp. This is one of the GNU accounting utilities.

 bash$ ac
         total       68.08
last

List last logged in users, as read from /var/log/wtmp. This command can also show remote logins.

For example, to show the last few times the system rebooted:

 bash$ last reboot
 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
newgrp

Change user's group ID without logging out. This permits access to the new group's files. Since users may be members of multiple groups simultaneously, this command finds little use.

Terminals

tty

Echoes the name of the current user's terminal. Note that each separate xterm window counts as a different terminal.

 bash$ tty
 /dev/pts/1
stty

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.


Example 13-2. Setting an erase character

   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 # Warning: Even after the script exits, the new key value remains set.
  16 
  17 exit 0


Example 13-3. secret password: Turning off terminal echoing

   1 #!/bin/bash
   2 
   3 echo
   4 echo -n "Enter password "
   5 read passwd
   6 echo "password is $passwd"
   7 echo -n "If someone had been looking over your shoulder, "
   8 echo "your password would have been compromised."
   9 
  10 echo && echo  # Two line-feeds in an "and list".
  11 
  12 stty -echo    # Turns off screen echo.
  13 
  14 echo -n "Enter password again "
  15 read passwd
  16 echo
  17 echo "password is $passwd"
  18 echo
  19 
  20 stty echo     # Restores screen echo.
  21 
  22 exit 0

A creative use of stty is detecting a user keypress (without hitting ENTER).


Example 13-4. Keypress detection

   1 #!/bin/bash
   2 # keypress.sh: Detect a user keypress ("hot keyboard").
   3 
   4 echo
   5 
   6 old_tty_settings=$(stty -g)   # Save old settings.
   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

Also see Example 9-3.

setterm

Set certain terminal attributes. This command writes to its terminal's stdout a string that changes the behavior of that terminal.

 bash$ setterm -cursor off
 bash$
 	      

The setterm command can be used within a script to change the appearance of text written to stdout, although there are certainly better tools available for this purpose.

   1 setterm -bold on
   2 echo bold hello
   3 
   4 setterm -bold off
   5 echo normal hello

tset

Show or initialize terminal settings. This is a less capable version of stty.

 bash$ tset -r
 Terminal type is xterm-xfree86.
 Kill is control-U (^U).
 Interrupt is control-C (^C).
 	      

setserial

Set or display serial port parameters. This command must be run by root user and is usually found in a system setup script.

   1 # From /etc/pcmcia/serial script:
   2 
   3 IRQ=`setserial /dev/$DEVICE | sed -e 's/.*IRQ: //'`
   4 setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ

getty, agetty

The initialization process for a terminal uses getty or agetty to set it up for login by a user. These commands are not used within user shell scripts. Their scripting counterpart is stty.

mesg

Enables or disables write access to the current user's terminal. Disabling access would prevent another user on the network to write to the terminal.

Tip

It can be very annoying to have a message about ordering pizza suddenly appear in the middle of the text file you are editing. On a multi-user network, you might therefore wish to disable write access to your terminal when you need to avoid interruptions.

wall

This is an acronym for "write all", i.e., sending a message to all users at every terminal logged into the network. It is primarily a system administrator's tool, useful, for example, when warning everyone that the system will shortly go down due to a problem (see Example 17-2).

 bash$ wall System going down for maintenance in 5 minutes!
 Broadcast message from bozo (pts/1) Sun Jul  8 13:53:27 2001...

 System going down for maintenance in 5 minutes!
 	      

Note

If write access to a particular terminal has been disabled with mesg, then wall cannot send a message to it.

dmesg

Lists all system bootup messages to stdout. Handy for debugging and ascertaining which device drivers were installed and which system interrupts in use. The output of dmesg may, of course, be parsed with grep, sed, or awk from within a script.

 bash$ dmesg | grep hda
 Kernel command line: ro root=/dev/hda2
 hda: IBM-DLGA-23080, ATA DISK drive
 hda: 6015744 sectors (3080 MB) w/96KiB Cache, CHS=746/128/63
 hda: hda1 hda2 hda3 < hda5 hda6 hda7 > hda4
 	      

Information and Statistics

uname

Output system specifications (OS, kernel version, etc.) to stdout. Invoked with the -a option, gives verbose system info (see Example 12-5). The -s option shows only the OS type.

 bash$ uname -a
 Linux localhost.localdomain 2.2.15-2.5.0 #1 Sat Feb 5 00:13:43 EST 2000 i686 unknown
 
 bash$ uname -s
 Linux
arch

Show system architecture. Equivalent to uname -m. See Example 10-26.

 bash$ arch
 i686
 
 bash$ uname -m
 i686
lastcomm

Gives information about previous commands, as stored in the /var/account/pacct file. Command name and user name can be specified by options. This is one of the GNU accounting utilities.

lastlog

List the last login time of all system users. This references the /var/log/lastlog file.

 bash$ lastlog
 root          tty1                      Fri Dec  7 18:43:21 -0700 2001
 bin                                     **Never logged in**
 daemon                                  **Never logged in**
 ...
 bozo          tty1                      Sat Dec  8 21:14:29 -0700 2001
 
 
 
 bash$ lastlog | grep root
 root          tty1                      Fri Dec  7 18:43:21 -0700 2001
 	      

Caution

This command will fail if the user invoking it does not have read permission for the /var/log/lastlog file.

lsof

List open files. This command outputs a detailed table of all currently open files and gives information about their owner, size, the processes associated with them, and more. Of course, lsof may be piped to grep and/or awk to parse and analyze its results.

 bash$ lsof
 COMMAND    PID    USER   FD   TYPE     DEVICE    SIZE     NODE NAME
 init         1    root  mem    REG        3,5   30748    30303 /sbin/init
 init         1    root  mem    REG        3,5   73120     8069 /lib/ld-2.1.3.so
 init         1    root  mem    REG        3,5  931668     8075 /lib/libc-2.1.3.so
 cardmgr    213    root  mem    REG        3,5   36956    30357 /sbin/cardmgr
 ...
 	      

strace

Diagnostic and debugging tool for tracing system calls and signals. The simplest way of invoking it is strace COMMAND.

 bash$ strace df
 execve("/bin/df", ["df"], [/* 45 vars */]) = 0
 uname({sys="Linux", node="bozo.localdomain", ...}) = 0
 brk(0)                                  = 0x804f5e4
 ...
 	    

This is the Linux equivalent of truss.

nmap

Network port scanner. This command scans a server to locate open ports and the services associated with those ports. It is an important security tool for locking down a network against hacking attempts.

   1 #!/bin/bash
   2 
   3 SERVER=$HOST                           # localhost.localdomain (127.0.0.1).
   4 PORT_NUMBER=25                         # SMTP port.
   5 
   6 nmap $SERVER | grep -w "$PORT_NUMBER"  # Is that particular port open?
   7 #              grep -w matches whole words only,
   8 #+             so this wouldn't match port 1025, for example.
   9 
  10 exit 0
  11 
  12 # 25/tcp     open        smtp

free

Shows memory and cache usage in tabular form. The output of this command lends itself to parsing, using grep, awk or Perl. The procinfo command shows all the information that free does, and much more.

 bash$ free
                 total       used       free     shared    buffers     cached
   Mem:         30504      28624       1880      15820       1608       16376
   -/+ buffers/cache:      10640      19864
   Swap:        68540       3128      65412

To show unused RAM memory:

 bash$ free | grep Mem | awk '{ print $4 }'
 1880
procinfo

Extract and list information and statistics from the /proc pseudo-filesystem. This gives a very extensive and detailed listing.

 bash$ procinfo | grep Bootup
 Bootup: Wed Mar 21 15:15:50 2001    Load average: 0.04 0.21 0.34 3/47 6829
lsdev

List devices, that is, show installed hardware.

 bash$ lsdev
 Device            DMA   IRQ  I/O Ports
 ------------------------------------------------
 cascade             4     2 
 dma                          0080-008f
 dma1                         0000-001f
 dma2                         00c0-00df
 fpu                          00f0-00ff
 ide0                     14  01f0-01f7 03f6-03f6
 ...
 	      

du

Show (disk) file usage, recursively. Defaults to current working directory, unless otherwise specified.

 bash$ du -ach
 1.0k    ./wi.sh
 1.0k    ./tst.sh
 1.0k    ./random.file
 6.0k    .
 6.0k    total
df

Shows filesystem usage in tabular form.

 bash$ df
 Filesystem           1k-blocks      Used Available Use% Mounted on
 /dev/hda5               273262     92607    166547  36% /
 /dev/hda8               222525    123951     87085  59% /home
 /dev/hda7              1408796   1075744    261488  80% /usr
stat

Gives detailed and verbose statistics on a given file (even a directory or device file) or set of files.

 bash$ stat test.cru
   File: "test.cru"
   Size: 49970        Allocated Blocks: 100          Filetype: Regular File
   Mode: (0664/-rw-rw-r--)         Uid: (  501/ bozo)  Gid: (  501/ bozo)
 Device:  3,8   Inode: 18185     Links: 1    
 Access: Sat Jun  2 16:40:24 2001
 Modify: Sat Jun  2 16:40:24 2001
 Change: Sat Jun  2 16:40:24 2001
 	      

If the target file does not exist, stat returns an error message.

 bash$ stat nonexistent-file
 nonexistent-file: No such file or directory
 	      

vmstat

Display virtual memory statistics.

 bash$ vmstat
    procs                      memory    swap          io system         cpu
 r  b  w   swpd   free   buff  cache  si  so    bi    bo   in    cs  us  sy id
 0  0  0      0  11040   2636  38952   0   0    33     7  271    88   8   3 89
 	    

netstat

Show current network statistics and information, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain
 
 bash$ echo $HOSTNAME
 localhost.localdomain

Similar to the hostname command are the domainname, dnsdomainname, nisdomainname, and ypdomainname commands. Use these to display or set the system DNS or NIS/YP domain name. Various options to hostname also perform these functions.

hostid

Echo a 32-bit hexadecimal numerical identifier for the host machine.

 bash$ hostid
 7f0100

Note

This command allegedly fetches a "unique" serial number for a particular system. Certain product registration procedures use this number to brand a particular user license. Unfortunately, hostid only returns the machine network address in hexadecimal, with pairs of bytes transposed.

The network address of a typical non-networked Linux machine, is found in /etc/hosts.

 bash$ cat /etc/hosts
 127.0.0.1               localhost.localdomain localhost

As it happens, transposing the bytes of 127.0.0.1, we get 0.127.1.0, which translates in hex to 007f0100, the exact equivalent of what hostid returns, above. There exist only a few million other Linux machines with this identical hostid.

sar

Invoking sar (System Activity Reporter) gives a very detailed rundown on system statistics. The Santa Cruz Operation (SCO) released sar as Open Source in June, 1999.

This command is not part of the base Linux distribution, but may be obtained as part of the sysstat utilities package, written by Sebastien Godard.

 bash$ sar
 Linux 2.4.9 (brooks.seringas.fr) 	09/26/03

10:30:00          CPU     %user     %nice   %system   %iowait     %idle
10:40:00          all      2.21     10.90     65.48      0.00     21.41
10:50:00          all      3.36      0.00     72.36      0.00     24.28
11:00:00          all      1.12      0.00     80.77      0.00     18.11
Average:          all      2.23      3.63     72.87      0.00     21.27

14:32:30          LINUX RESTART

15:00:00          CPU     %user     %nice   %system   %iowait     %idle
15:10:00          all      8.59      2.40     17.47      0.00     71.54
15:20:00          all      4.07      1.00     11.95      0.00     82.98
15:30:00          all      0.79      2.94      7.56      0.00     88.71
Average:          all      6.33      1.70     14.71      0.00     77.26
            
readelf

Show information and statistics about a designated elf binary. This is part of the binutils package.

 bash$ readelf -h /bin/bash
 ELF Header:
   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
   Class:                             ELF32
   Data:                              2's complement, little endian
   Version:                           1 (current)
   OS/ABI:                            UNIX - System V
   ABI Version:                       0
   Type:                              EXEC (Executable file)
   . . .
size

The size [/path/to/binary] command gives the segment sizes of a binary executable or archive file. This is mainly of use to programmers.

 bash$ size /bin/bash
    text    data     bss     dec     hex filename
  495971   22496   17392  535859   82d33 /bin/bash
 	      

System Logs

logger

Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger. n, such as routing tables and active connections. This utility accesses information in /proc/net (Chapter 28). See Example 28-3.

netstat -r is equivalent to route.

 bash$ netstat
 Active Internet connections (w/o servers)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 Active UNIX domain sockets (w/o servers)
 Proto RefCnt Flags       Type       State         I-Node Path
 unix  11     [ ]         DGRAM                    906    /dev/log
 unix  3      [ ]         STREAM     CONNECTED     4514   /tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4513
 . . .
uptime

Shows how long the system has been running, along with associated statistics.

 bash$ uptime
 10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27
hostname

Lists the system's host name. This command sets the host name in an /etc/rc.d setup script (/etc/rc.d/rc.sysinit or similar). It is equivalent to uname -n, and a counterpart to the $HOSTNAME internal variable.

 bash$ hostname
 localhost.localdomain