In the Linux File Command Master Guide, we discussed the basic information needed to leverage the most basic and most common Linux file commands. In this article, I will discuss the Linux system commands in the same way.
As we learned in the previous article, in the Linux/unix world, everything is considered a file, including system management commands. Individually for organizations, we discussed file commands in the first part of this series and will provide the most useful system diagnostics and maintenance commands in this article.
For file commands, we discuss some fairly simple and intuitive management commands here. It only describes the functionality of other, more complex commands, so that when you need to use them, you know that they exist and what they use. This article does not cover a detailed discussion of the rationale behind the use of each command, but we will discuss the most basic and useful commands used to monitor and manage printer services, file systems, disks, and processes.
For more information about super users
Many of the commands discussed here can be performed by regular users in their own files and processes. However, some commands, such as FDISK, require root permission to run.
For the integrity of your system, it is recommended that you avoid logging on to the system directly with "root". Instead, it is generally more appropriate to log on as a regular user and then perform su or execute the sux command from X window to run system administration functions. (Sux provides you with the X window environment and root permissions.) When you log on as root, you will always know that you are in this state because your prompt becomes "#".
Power Users can use sudo to selectively assign permissions that are typically reserved for root or other special users to a different regular user. The root user can define the actual and valid user and group identification number UID and GID to match the intended user's identity. This expected user can be (but not necessarily) the user who is allowed to run the command.
In other words, a superuser can allow another user to execute a command, but it can also be determined that any resulting file produced by the process should belong to the root user or to any other user specified by the superuser. When Sudo permissions are assigned, the user is only required to authenticate with his or her own password, without requiring the root user password.
General commands
Technically, Uname, diff, Date, sort, and uniq should be treated as file commands, but because they are often used to diagnose and maintain the system management aspects of routines, it is more meaningful to explain them here.
Uname
Uname provides general system information. When the-a option is specified, all available information is displayed:
$ uname-a
Linux Linux 2.4.18-4gb #1 Wed Mar 13:57:05 UTC 2002 i686 Unknown
Here we see the regular system information from left to right. Each piece of information has its own options (in parentheses below) that can be displayed separately:
The operating system name is Linux (-s).
The network node for this computer is named Linux (-N).
The operating system release version is 4.5.18-4GB (-R).
The operating system version is #1 Wed Mar 13:57:05 UTC 2002 (-V).
The machine type is i686 (-m).
The processor type (-P) here is unknown.
Similarly, Stty displays information about your terminal settings:
$ stty-a
Speed 38400 baud; Rows 40; Columns 103; line = 0;
Intr = ^c; Quit = ^/; erase = ^?; Kill = ^u; EOF = ^d; EOL =; Eol2 =; start = ^q;
stop = ^s; Susp = ^z; Rprnt = ^r; Werase = ^w; Lnext = ^v; flush = ^o; min = 1; Time = 0;
-parenb-parodd CS8-HUPCL-CSTOPB CREAD-CLOCAL-CRTSCTS-IGNBRK-BRKINT-IGNPAR-PARMRK-INPCK
-ISTRIP-INLCR-IGNCR ICRNL Ixon-ixoff-iuclc-ixany-imaxbel
OPOST-OLCUC-OCRNL Onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0
Isig Icanon iexten echo echoe echok-echonl-noflsh-xcase-tostop-echoprt echoctl Echoke
The second and third lines show the sequence of special characters and keys that will be called separately on the command line. For example, "^z" means that Cntrl-z sends a terminal rest in the system. The exit symbol is sent "^/", which is cntrl-/, and we can change it to "^x" as follows:
$ stty Quit ^x
In this case, you actually enter the ^ symbol instead of the Cntrl key. This change is only valid in the current window. In order for this change to take effect permanently, you need to enter the statement in your. bashrc file.
Diff and Date. As the name suggests, diff compares two files. It returns those lines that cause the file to be different and returns some obscure output about how to make the file the same:
$ diff Test.out Test2.out
0a1
> Another test.
Here we are told that we need to add the "Another test." Line to the test.out to make it similar to test2.out. The first character "0" is the line number to be edited in Test.out, and "a" means that the line should be added to the test.out to match the Test2.out's first line of "1". If we need to change test.out, the second character can also be "C", and if we need to remove some content from Test.out, the second character is "D".
You may want to make a file similar to another file to provide patches for some code. Diff is also often used as an analysis tool to verify that the output of a program or script-such as the SED script-is the expected output.
The date command can be used to display or set dates. If the user has superuser privileges, he or she can set the date by supplying a numeric string in the following command:
$ date 0101010004
This command sets the date to 2004 years (04) January 1 (0101) 1 o'clock in the morning (0100).
However, any user can display the date in the format that you want. Note that you need a plus sign "+" before the format sequence. If the expected output of this command is not present, try to enclose it in single quotes:
$ date + '%h%m%s '
204748
You can include spaces in the sequence, when you need to use double quotes:
$ date + "%H%m%s"
20 47 51
There are more than 30 different format sequences listed on the home page of the date, which can be combined as needed to get your final output format.
The output of date can also be used in scripts, such as:
$ Date-d ' 2 day '
Thu 19:02:02 EST 2004
The-D option indicates that date displays the time that is described in the string that followed. In the previous example, we asked for a date of two days from now (January 27, 2004). We can also ask for a date showing three days ago, with the order:
$ Date-d '-3 day '
Sun 20:56:59 EST 2004
Another example of a date's multiple features is the ability to output additional strings to it, providing a convenient way to generate a unique file name.
$ ls-l | Cat > $ (Date +ls_%d%b%y%h%m.out)
$ ls ls_*.out
Ls_04feb041633.out
Here, we pass the output of a long file list to cat, which transmits our output to a file called Ls_04feb041609.out. Notice that we used a variable for the filename, represented by the dollar sign "$". This file name is generated by attaching "Ls_" and ". Out" to the date in turn, in the format of days in the month (%d), the local month name abbreviation (%b), the last two digits of the year (%y), Hours (%H), and minutes (%m).
Sort and Uniq. You can sort the rows of a text file from a pipe or from a file that you specify from the command line. Unless otherwise specified, sort operates on the first column of the multiple-column output. By default, numeric columns are sorted alphabetically, unless the-n option is used.
The Sort column can be specified in two different formats. If you use the-k option, the position of the column is numbered starting from 1. You can also use the +POS1 format to start the numbering of columns from the first column number to zero. The following two commands produce the same output:
$ ls-l *.out | Sort +5-7
$ ls-l *.out | Sort-k 6,8
-rw-r--r--1 bluher users 375 Dec 5 2002 Fdisk.out
-rw-r--r--1 bluher users 2447 Dec 20:15 ls.out
-rw-r--r--1 Bluher Users 590 4 21:24 greps.out
-rw-r--r--1 Bluher users 4 21:25 sgrep.out
...
By default, the column delimiters for the sort command are spaces and tabs. You can use the-t SEP option to specify additional delimiters, where SEP is the separator symbol that you specify. The default order of sort is from lowest to highest, with the-r option to reverse the order.
Sort can be used with many different tasks. For example, it can be used to sort the output of a file (such as/etc/passwd), or to sort the output of the Du as we see below. When the-M option is used, sort merges the files and does not actually sort them. However, if you want to sort them, you should sort them before the file is merged.
Sort is often used with the Uniq command to delete identical rows from a sorted file. The Uniq command often follows the sort command in the pipeline, can also use the-C option to calculate the number of occurrences of a row, or use the-D option to report only exactly the same row:
$ sort-m Test.out Test2_sort.out | Uniq-c
1 Another test.
1 Testing Testing
2 This is a test.
Here we combine two files Test.out and test2_sort.out. We then use the-C option to transfer the output to Uniq, so that the final result is an alphabetical list, the exact same row is deleted, and a report of how often each line appears.
Printer Control
In most cases, the use of printer commands is relatively clear. Send the job to the printer by specifying a filename after the lpr command. If no file name is provided, standard input is assumed. Using the LPQ command, you can obtain status reports about jobs that are sent to the printer. When you specify the job number, LPRM deletes the job from the print spool queue for the row printer.
$ LPR Grep2.out
$ lpq
Printer:lp@linux ' lpdfilter drv=upp method=auto color=yes '
queue:1 printable Job
Server:pid 1929 Active
Unspooler:pid 1930 Active
Status:waiting for Subserver to exit at 21:24:14.637
Rank owner/id Class Job Files Size time
Active bluher@linux+928 A 928 grep2.out 779 21:24:14
Done Bluher A 877 (STDIN) 117258 13:09:53
$ LPRM 928
Printer Lp@linux:
Checking perms ' cfa928linux.local '
dequeued ' cfa928linux.local '
Here, when we send the job to the printer, we query its status. LPQ showed us that the job with number 877 was completed, and the job number we just sent was 928. We use the command LPRM 928 to remove the active job from the queue.
Disk information
Whether you are maintaining your own system or supporting an enterprise with many users, monitoring disk space is one of the more important responsibilities of your system administrator. The Du, DF, and FDISK commands provide a different view of the available disk space.
The DU command provides an assessment of the use of the file space for each directory. By default, the unit of measurement is the number of blocks, and the size of the block is usually determined when you install your system.
In the following example, the system is installed using a 1024-byte block. If no directory is specified, the du begins the calculation of the current directory. The-H option provides a value of size in an easy-to-read form:
$ du-h Documents
105k DOCUMENTS/JJ
573k Documents/john
682k Documents
You can change the output using the-a option, which indicates that Du displays the number of blocks for files and directories. Option--block-size=size indicates that Du displays the output in the number of blocks that are specified in size.
$ du--block-size=512 Documents
209 DOCUMENTS/JJ
1146 Documents/john
1363 Documents
By default, du also prints information recursively for each subdirectory. You can limit this operation by using the Max-depth=n option, where N equals the maximum number of levels reported:
$ du--max-depth=2.
$ du--max-depth=2 | Sort-n
4./.xemacs
4./public_html
/documents/jj.
573./documents/ian
682./documents
2420./jdevhome/system
8350./jdevhome/system9.0.3.1035
13483./jdevhome/mywork
24252./jdevhome
...
Du reports space usage for directories and subdirectories, while DF reports disk space usage for installed file systems:
$ df
FileSystem 1k-blocks Used Available use% mounted on
/dev/hda6 4195632 1983656 2211976 48%/
/dev/hda5 14607 3778 10075 28%/boot
/DEV/HDA9 937372 494028 443344 53%/home
/dev/hda8 6297248 3876880 2420368 62%/opt
Shmfs 256220 0 256220 0%/dev/shm
Here, DF reports the status of five file systems. Like DU, DF provides human-readable output using the-H option:
$ df-h
FileSystem Size Used Avail use% mounted on
/dev/hda6 4.0G 1.9G 2.1G 48%/
/dev/hda5 14M 3.7M 9.8M 28%/boot
/DEV/HDA9 915M 476M 440M 52%/home
/dev/hda8 6.0G 3.7G 2.3G 62%/opt
Shmfs 250M 0 250M 0%/dev/shm
The DF command also allows you to specify the size of the block using the-block-size=size option.
The fdisk command is a Linux partition Table Operation command. Unlike Du and DF, this command cannot be run without root user rights. If the-l option is specified, it can be used only for reporting purposes. It provides a complete view of your entire partitioned table, not just the extended Linux partitions you see with DF:
# fdisk-l
disk/dev/hda:240 heads, sectors, 1940 cylinders
Units = cylinders of 15120 * bytes
Device Boot Start End Blocks Id System
/DEV/HDA1 1 286 2162128+ C Win95 FAT32 (LBA)
/DEV/HDA2 * 1940 12496680 5 Extended
/DEV/HDA5 289 15088+ MB Linux
/dev/hda6 290 844 4195768+ Linux
/DEV/HDA7 845 983 1050808+ Linux swap
/DEV/HDA8 984 1816 6297448+ Linux
/DEV/HDA9 1817 1940 937408+ Linux
Process Monitoring
In a busy multiuser system, process monitoring is also an important responsibility of Linux system administrators. In this respect, PS and top are the two most useful commands.
The PS command provides a snapshot of the currently running process.
A lively and informative lesson
The following is the simplest form of PS:
$ ps
PID TTY Time CMD
3884 PTS/1 00:00:00 Bash
3955 PTS/2 00:00:00 More
3956 PTS/5 00:00:05 Sqlplus
The PID is the identification number of the process.
TTY is the terminal console to which the process belongs.
The time column is the total CPU times used by the process.
The CMD column lists the command lines that are being executed.
Use PS with the-EF option to return a complete list of all processes for all users in the system. If you pass the results of this PS command to grep, the result is easier to see. For example:
$ PS-EF | grep Oracle
UID PID PPID C stime TTY time CMD
Oracle 1633 1 0 13:58 00:00:00 ora_pmon_ora1
Oracle 1635 1 0 13:58 00:00:00 ora_dbw0_ora1
Oracle 1637 1 0 13:58 00:00:01 ora_lgwr_ora1
Oracle 1639 1 0 13:58 00:00:02 ora_ckpt_ora1
Oracle 1641 1 0 13:58 00:00:02 ora_smon_ora1
Oracle 1643 1 0 13:58 00:00:00 ora_reco_ora1
Oracle 1645 1 0 13:58 00:00:00 ora_cjq0_ora1
Oracle 1647 1 0 13:58 00:01:18 ora_qmn0_ora1
Oracle 1649 1 0 13:58 00:00:00 ora_s000_ora1
Oracle 1651 1 0 13:58 00:00:00 ora_d000_ora1
Note that when you pass the output of PS to the grep command, the column headings are not displayed as normally as they were used in the PS command itself. For illustration purposes, the caption is positioned above the output so that its contact is clearly displayed.
Although the UID usually refers to the identity of a numeric type, the user name is specified under the first column, marked as UID.
PPID is the identity number of the parent process. For the Oracle process, the identification number is 1-it is the ID of the INIT process (the parent process of all processes), because Oracle installed in this system is initiated as part of the logon process.
The column labeled C is the factor that the CPU uses to compute the execution priority.
Stime refers to the start time of a process.
The question mark indicates that these processes do not belong to any TTY because they are initiated by the system.
The following is another example of the PS command with some different options. Note that many of these columns are the same as when you execute PS with the-EF option:
$ ps aux
USER PID%cpu%mem VSZ RSS TTY STAT START time COMMAND
Bluh 4024 0.0 0.2 2240 1116 pts/1 S 20:59 0:00 su bluher
Bluh 4025 0.0 0.3 2856 1668 pts/1 S 20:59 0:00 Bash
Bluh 4051 0.0 0.2 2488 1504 PTS/1 R 21:01 0:00 PS aux
Bluh 4052 0.0 0.1 1636 pts/1 S 21:01 grep 0:00
In addition to providing important diagnostic information, the PS command is often used to find PID for runaway processes. For example, one of your users enters a Cartesian join in a sql*plus session. You can perform the following PS command to find out its PID:
$ PS-EF | grep bluher
...
Bluher 4068 4064 0 21:14 PTS/6 00:00:00 Less
Bluher 4112 3945 0 21:28 pts/5 00:08:42 sqlplus
...
You can then terminate it by using the following command:
$ kill 4112
Regular users can see all the system processes, but they can only terminate the processes they own.
Ps only provides you with a snapshot of the current process. To see the most active processes in real time, use top. Top provides process information in real time. It also has an interactive state that allows the user to enter commands, such as n followed by 5 or 10 digits. The result is to indicate that top shows 5 or 10 of the most active processes. Top continues to run until you press "Q" to exit top.
The following is part of the top display:
8:14pm up Wuyi min, 7 users, load average:0.02, 0.03, 0.05
Processes:87 sleeping, 2 running, 0 zombie, 7 stopped
CPU states:6.4% User, 3.5% system, 0.0% Nice, 89.9% idle
mem:512440k av, 392288K used, 120152K free, 0K shrd, 55668K Buff
swap:1050800k av, 0K used, 1050800K free 236128K Cached
PID USER PRI NI SIZE RSS SHARE STAT%cpu%mem time COMMAND
1289 Root 0 78008 11M 1796 R 3.7 2.2 0:49 X
1965 Bluher 0 12244 11M 10456 S 1.9 2.3 0:01 Kdeinit
1986 Bluher 0 996 996 768 R 1.5 0.1 0:00 Top
1803 Bluher 0 16960 16M 13508 S 0.9 3.3 0:03 Kdeinit
1804 Bluher 0 16964 16M 13508 S 0.9 3.3 0:03 Kdeinit
1800 Bluher 0 12260 11M 10076 S 0.1 2.3 0:04 Kdeinit
1 Root 0 188 S 0.0 0.0 0:04 Init
...
off
It seems that the discussion should be concluded with the shutdown order. Like many of the commands discussed in this article and in the previous article, there is usually a GUI-style alternative to shutting down your system. However, when you need to shut down manually, you can use the following command to close immediately:
$ shutdown-h Now
You can manually reboot the system by using the following command:
$ shutdown-r Now
Note that both commands require a specified time. You can specify "now" or use the-t option with a time number of seconds.
We've covered a lot of content in both articles, but we definitely can't cover everything that Linux might have. There are a lot of commands we haven't discussed, and we don't see more command options. But here we want you to at least find the tools you need to start managing your Linux system.
Author Introduction
Sheryl Calish is a Blue Heron Consulting Oracle developer who specializes in Linux. She is also the Chairman of the Fund of the Florida Oracle Users Group and the sales chair of Ioug Linux SIG.