Linux Command Application Guide

Source: Internet
Author: User
Tags month name superuser permission oracle developer

In the Linux/Unix world, everything is regarded as a file, including system management commands. For organizations alone, we have discussed file commands in the first part of this series and will provide the most useful system diagnostic and maintenance commands in this article.

For file commands, we have discussed some simple and intuitive management commands in detail here. It only describes the functions of other complex commands, so that you can know the existence and purpose of these commands when you need to use them. This article does not discuss in detail the basic principles behind each command, but we will discuss the most basic and useful commands, these commands are used to monitor and manage Printer services, file systems, disks, and processes.

More information about Super Users

Many of the commands discussed here can be executed by regular users in their own files and processes. However, some commands, such as fdisk, must have the root permission to run.

To ensure the integrity of your system, we recommend that you do not directly log on to the system as "root. On the contrary, it is generally better to log on as a regular user, and then run the su or sux command from X Window to run the system management function. (Sux provides you with the X Window environment and root permissions .) When you log on as a root user, you always know that you are in this status because your prompt changes to "#".

Super Users can use sudo to selectively assign permissions that are usually reserved by root users or other special users to a different regular user. The root user can define actual and valid user and group id uid and GID to match the expected user ID. This expected user can be (but does not have to) the user who is allowed to run the command.

In other words, a Super User can allow other users to execute a command, but it can also determine that any result file generated by the process should belong to the root user or any other user specified by the Super User. Once the sudo permission is assigned, you only need to use your own password for authentication, instead of the root user password.

Common commands

Technically, uname, diff, date, sort, and uniq should be considered as file commands, but they are more meaningful here because they are often used to diagnose and maintain the system management of routines.

Uname

Uname provides general system information. When the-A option is specified, all available information is displayed:

$ Uname-

Linux 2.4.18-4 GB #1 wed Mar 27 13:57:05 UTC 2002 i686 unknown

Here we can see the general system information from left to right. Each piece of information has its own options (in the parentheses below), which can be displayed separately:

The operating system name is Linux (-S ).

The network node name of the local machine is Linux (-N ).

The OS version is 4.5.18-4 GB (-R ).

The operating system version is #1 wed Mar 27 13:57:05 UTC 2002 (-V ).

The machine type is i686 (-m ).

Here, the processor type (-P) is unknown.

Similarly, stty displays information about your terminal settings:

$ Stty-

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 special characters and key sequences that will be called in the command line. For example, "^ Z" means the CNTRL-Z sends a terminal break in the system. The exit symbol is sent with "^/", that is, cntrl-/, and we can change it to "^ X", as shown below:

$ Stty quit ^ x

In this case, you must enter the ^ symbol instead of the cntrl key. This change only takes effect in the current window. To make this change take effect permanently, enter this statement in your. bashrc file.

Diff and date. As the name suggests, diff compares two files. It returns the rows that lead to different files and the output with obscure meanings about how to make the files the same:

$ Diff test. Out test2.out

0A1

> Another test.

Here we are told that we need to add the line "another test." To test. out so that it is similar to test2.out. The first character "0" is in test. "A" indicates that the row should be added to test. to match the first line "1" of test2.out. If we need to change test. the second character can also be "C. the second character is "D ".

You may want to make a file similar to another file to patch some code. Diff is often used as an analysis tool to verify whether the output of a program or script, such as sed script, is the expected output.

The date command can be used to display or set a date. If the user has the superuser permission, he or she can set the date by providing a numeric string in the following command:

$ Date 0101010004

This command sets the date to (0101), January 1, 0100 ).

However, any user can use the required format to display the date. Note that a plus sign "+" is required before the format sequence ". If the expected output of this command is not displayed, try to include it in single quotes:

$ Date + '% H % m % s'

204748

You can include spaces in the sequence. In this case, you need to use double quotation marks:

$ Date + "% H % m % s"

20 47 51

There are over 30 different format sequences listed on the date homepage. These sequences can be combined as needed to get your final output format.

Date output can also be used in scripts, such:

$ Date-D '2 Day'

Thu Jan 29 19:02:02 est 2004

The-D option indicates that date displays the time described in the subsequent string. In the above example, we asked to display the date two days from now on (January 1, January 27, 2004. We can also ask to display the date three days ago. The command is:

$ Date-d '-3 Day'

Sun Jan 25 20:56:59 est 2004

Another example of multiple functions of date is the ability to output 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 send the output of a long file list to CAT, which transfers our output to a file named ls_04feb041609.out. Note that we use a variable for the file name, represented by the dollar sign "$. Sort "LS _" and ". out "is appended to the date, in the format of day (% d) in the month, abbreviation of local month name (% B), last two digits of the Year (% Y), hour (% H) and minute (% m), thus generating this file name.

Sort and uniq. You can perform the sort operation on the lines of text files from the MPs queue or from the files specified by the command line. Unless otherwise specified, sort operates on the first column of Multi-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 the-K option is used, the column position starts from 1. You can also use the + pos1 format to numbered a column starting from the first column number to zero. The following two commands generate the same output:

$ LS-L *. Out | sort + 5-7

$ LS-L *. Out | sort-K 6, 8

-RW-r -- 1 bluher users 375 Dec 5 2002 fdisk. Out

-RW-r -- 1 bluher users 2447 Dec 28 ls. Out

-RW-r -- 1 bluher users 590 Jan 4 greps. Out

-RW-r -- 1 bluher users 78 Jan 4 21:25 sgrep. Out

...

By default, the column Separator of the sort command is space and tab. You can use the-T Sep option to specify other separators, where SEP is the separator you specified. The default sequence of sort is from the lowest to the highest, and the order can be reversed using the-r option.

Sort can be used for many different tasks. For example, it can be used to sort the output of files (such as/etc/passwd), or sort the output of DU as we can see below. When the-M option is used, sort merges the files without sorting them. However, if you want to sort them, you should sort them before file merging.

Sort is often used with the uniq command to delete identical lines from sorted files. The uniq command is often followed by the sort command in the pipeline. You can also use the-C option to calculate the number of occurrences of a line, or use the-D option to only report identical rows:

$ Sort-M test. Out test2_sort.out | uniq-C

1 another test.

1 Testing

2 This is a test.

Here we merge two files: Test. Out and test2_sort.out. Then we use the-C option to transmit the output to uniq, so that the final result is a list in alphabetical order, and the identical rows are deleted, it also contains a report on the frequency of occurrence of each line.

Printer Control

In most cases, the use of printer commands is relatively clear. You can specify a file name after the LPR command to send the job to the printer. If no file name is provided, standard input is used. Use The lpq command to obtain status reports about jobs sent to the printer. When you specify a job number, lprm deletes the job from the print queue of the row printer background.

$ 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

$ LPR 928

Printer LP @ Linux:

Checking perms 'cfa928linux. local'

Dequeued 'cfa928linux. local'

Here, when we send a job to the printer, We query its status. Lpq shows us that the job number 877 has been completed, and the job number we just sent is 928. Run lprm 928 to delete the active job from the queue.

Disk Information

Whether you maintain your system or support enterprises with many users, monitoring disk space is one of the more important responsibilities of the system administrator. The Du, DF, and fdisk commands provide different views of available disk space.

The du command evaluates the usage of the file space in each directory. By default, the unit of measurement is the number of blocks. The block size is usually determined when you install your system.

In the following example, the system uses a 1024-byte block for installation. If no directory is specified, du starts to calculate the current directory. The-H option provides the size value in an easy-to-understand form:

$ Du-H documents ents

105 K documents/jj

573 K documents/John

682 K documents
 

You can use the-A option to change the output. This option indicates the number of files and directories displayed by Du. Option -- block-size = size indicates that DU displays the output based on the number of blocks in the specified byte size.

$ Du -- block-size = 512 documents ents

209 documents/jj

1146 documents/John

1363 documents

By default, du recursively prints information for each sub-directory. You can use the max-depth = n option to limit this operation, where n is equal to the maximum level reported:

$ Du -- Max-depth = 2.

$ Du -- Max-depth = 2 | sort-n

4./. xemacs

4./public_html

105./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 of directories and subdirectories, while DF reports disk space usage of 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 the five file systems. Similar to DU, DF uses the-H option to provide human-readable output:

$ DF-H

Filesystem size used avail use % mounted on

/Dev/hda6 4.0g 1.9g 2.1g 48%/

/Dev/hda5 14 m 3.7 m 9.8 m 28%/boot

/Dev/hda9 915 M 476 m 440 m 52%/home

/Dev/hda8 6.0g 3.7g 2.3g 62%/OPT

Shmfs 250 m 0 250 m 0%/dev/SHM

The DF command also allows you to use the-block-size = Size Option to specify the block size.

The fdisk command is a partition table Operation Command in Linux. Unlike du and DF, this command cannot be run without the root user permission. If the-L option is specified, it can be used only for reporting purposes. It provides a complete view of your entire partition table, not just the extended LINUX partition that you see using DF:

# Fdisk-l

Disk/dev/hda: 240 heads, 63 sectors, 1940 cylinders.

Units = cylinders of 15120*512 bytes

Device boot start end blocks ID system

/Dev/hda1 1 286 2162128 + C Win95 FAT32 (LBA)

/Dev/hda2*288 1940 12496680 5 extended

/Dev/hda5 288 289 15088 + 83 Linux

/Dev/hda6 290 844 4195768 + 83 Linux

/Dev/hda7 845 983 1050808 + 82 Linux swap

/Dev/hda8 984 1816 6297448 + 83 Linux

/Dev/hda9 1817 1940 937408 + 83 Linux

Process Monitoring

In a busy multi-user system, process monitoring is also an important responsibility of the Linux system administrator. In this regard, PS and top are two of the most useful commands.

The ps command provides a snapshot of the currently running process.

Vivid and informative course

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

PID is the ID of a process.

Tty is the terminal console to which the process belongs.

The Time column is the total CPU time used by the process.

The cmd column lists the command lines being executed.

Use the PS with the-Ef option to return the complete list of all processes of all users in the system. If you send the result of this ps command to grep, the result is easier to view. For example:

$ PS-Ef | grep Oracle

Uid pid ppid C stime tty time cmd

Oracle 1633 1 0? 00:00:00 ora_pmon_ora1.

Oracle 1635 1 0? 00:00:00 ora_dbw0_ora1.

Oracle 1637 1 0? 00:00:01 ora_lgwr_ora1.

Oracle 1639 1 0? 00:00:02 ora_ckpt_ora1.

Oracle 1641 1 0? 00:00:02 ora_smon_ora1.

Oracle 1643 1 0? 00:00:00 ora_reco_ora1.

Oracle 1645 1 0? 00:00:00 ora_cjq0_ora1.

Oracle 1647 1 0? 00:01:18 ora_qmn0_ora1.

Oracle 1649 1 0? 00:00:00 ora_s000_ora1.

Oracle 1651 1 0? 00:00:00 ora_d000_ora1.

Note: When you send PS output to the grep command, the column title is not displayed normally as it is in use by the ps command itself. For ease of instruction, the title is located above the output so that the contact is clearly displayed.

Although uid is usually an exponential identifier, the user name specified in the first column is marked as uid.

Ppid is the ID of the parent process. For an oracle process, the ID here is 1-it is the ID of the INIT process (the parent process of all processes, because Oracle installed in the system is started as part of the login process.

The column labeled C is a factor used by the CPU to calculate the execution priority.

Stime refers to the start time of the process.

The question mark indicates that these processes do not belong to any tty because they are started by the system.

The following is another example of the ps command with some different options. Note: Many of the columns are the same as those when executing the ps command with the-Ef option:

$ PS aux

User PID % CPU % mem vsz RSS tty stat Start Time Command

Bluh 4024 0.0 0.2 2240 pts/1 s Su bluher

Bluh 4025 0.0 0.3 2856 1668 pts/1 s bash

Bluh 4051 0.0 0.2 2488 1504 pts/1 R p.m. PS aux

Bluh 4052 0.0 0.1 1636 pts/1 s grep bluher

In addition to providing important diagnostic information, the ps command is often used to identify the PID of a runaway process. For example, one of your users enters a Cartesian join in the SQL * Plus session. You can run the following ps command to find its PID:

$ PS-Ef | grep bluher

...

Bluher 4068 4064 0 00:00:00 pts/6 less

Bluher 4112 3945 0 00:08:42 pts/5 sqlplus

...

Then you can use the following command to terminate it:

$ Killed 4112

Regular users can see all system processes, but they can only terminate their own processes.

PS only provides snapshots of the current process. To view the most active processes in real time, use top. Top provides process information in real time. It also has an Interactive status, allowing users to enter commands, such as numbers 5 or 10 after N. The result indicates that top displays 5 or 10 of the most active processes. Top continues until you press "Q" to exit top.

The following is the top part:

PM up 51 min, 7 users, load average: 0.02, 0.03, 0.05

96 processes: 87 sleeping, 2 running, 0 zombie, 7 stopped

CPU states: 6.4% user, 3.5% system, 0.0% nice, 89.9% idle

Mem: 512440 k av, 392288 K used, 120152 K free, 0 K shrd, 55668 K buff

Swap: 1050800 k av, 0 K used, 1050800 K free 236128 K cached

PID user pri Ni size RSS share stat % CPU % mem Time Command

1289 root 18 0 78008 11 m 1796 R 3.7 2.2 X

1965 bluher 20 0 12244 11 m 10456 s 1.9 0: 01 kdeinit

1986 bluher 18 0 996 996 R 768 1.5 0: 00 top

1803 bluher 20 0 16960 16 m 13508 s 0.9 0: 03 kdeinit

1804 bluher 20 0 16964 16 m 13508 s 0.9 0: 03 kdeinit

1800 bluher 19 0 12260 11 m 10076 s 0.1 0: 04 kdeinit

1 root 20 0 220 220 s 188 0.0 init

...

Close

It seems that we should end this discussion with the shutdown command. Like many commands discussed in this article and the previous article, there is usually a GUI-based alternative that can shut down your system. However, you can use the following command to disable it immediately:

$ Shutdown-H now

You can use the following command to manually restart the system:

$ Shutdown-R now

Note that both commands must specify the time. You can specify "now" or use the-T option with a time in seconds.

We have discussed a lot in these two articles, but we cannot cover all the content that Linux may have. We have not discussed many other commands, but we have not seen more Command Options. However, here we want you to find at least the minimum tools required to start managing Linux systems.

Sheryl calish is an Oracle Developer who specializes in Linux at Blue Heron consulting. She is also the fund chairman of Central Florida oracle users group and the Sales Chairman of ioug Linux Sig.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.