Linux Command proficiency Guide

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

Linux Command proficiency Guide
Author: Sheryl calish

Introduction to system commands for Linux beginners

In the "Linux file command proficiency Guide", we discussed the basic information required to take advantage of the most basic and commonly used Linux file commands. In this article, I will discuss Linux system commands in the same way.

In the previous article, we learned that in the Linux/Unix world, everything is seen 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, suchFdisk, You 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 executeSuOr execute from X WindowSUXCommand to run the system management function. (SUXProvides 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 useSudoAnd selectively assign permissions that are usually reserved by the root user 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.SudoOnce a permission is assigned, you only need to use your own password for verification, instead of the root user password.

Common commands

Technically speaking, uname, diff, date, sortAndUniqIt should be seen as file commands, but since they are often used to diagnose and maintain the system management aspects of routines, they are more meaningful here.

Uname

UnameProvides 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 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,SttyDisplay 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 = <undef>; eol2 = <undef>; 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.DiffIt is also often used as an analysis tool to verify whether the output of a program or script, such as sed script, is the expected output.

DateThe 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.

DateCan 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 

DateAnother example of multiple features is the ability to output strings to them, 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 operations on the line of a text file from an MPS queue or from a file specified by the command line.SortOperation. 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.

SortColumns 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--r-- 1 bluher users 375 Dec 5 2002 fdisk.out -rw-r--r-- 1 bluher users 2447 Dec 28 20:15 ls.out -rw-r--r-- 1 bluher users 590 Jan 4 21:24 greps.out -rw-r--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.SortThe default order is from the lowest to the highest. The-r option can be used to reverse the order.

SortIt can be used for many different tasks. For example, it can be used to sort the file output (such as/etc/passwd), or, as we can see belowDu. When the-M option is used,SortMerging files does not actually sort them. However, if you want to sort them, you should sort them before file merging.

SortOftenUniqCommand to delete identical rows from sorted files.UniqCommands often follow in pipelinesSortAfter the command, you can also use the-C option to calculate the number of times a row appears, or use the-D option to only report identical rows:

$ sort -m test.out test2_sort.out | uniq -c 1 Another test. 1 Testing 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 outputUniqIn this way, the final result is a list in alphabetical order. The identical rows are deleted with a report on the frequency of occurrence of each row.

Printer Control

In most cases, the use of printer commands is relatively clear. InLPRSpecify the file name after the command to send the job to the printer. If no file name is provided, standard input is used. UseLpqCommand to obtain the status report about the job sent to the printer. When you specify a job number,LprmDelete 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 $ lprm 928 Printer lp@linux: checking perms 'cfA928linux.local' dequeued 'cfA928linux.local' 

Here, when we send a job to the printer, We query its status.LpqThe job number 877 has been completed, and the job number we just sent is 928. We use the commandLPR 928Delete an 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.Du, DFAndFdiskThe command provides different views of available disk space.

DuCommand to evaluate 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 specifiedDuStart to calculate the current directory. The-H option provides the size value in an easy-to-understand form:

$ du -h Documents 105k Documents/jj 573k Documents/John 682k Documents 

You can use the-A option to change the output, which indicatesDuDisplays the number of files and directories. Option -- block-size = sizeDuThe output is displayed based on the number of bytes specified by size.

$ du --block-size=512 Documents 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 ... 

DuReports the space usage of directories and subdirectories, whileDFReport disk space usage of the installed file system:

$ 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,DFThe status of the five file systems is reported. AndSimilar to DU, DFUse 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 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 

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

FdiskThe command is a partition table Operation Command in Linux. AndDuAndDFDifferent, you cannot run this command 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 justDFSee extended LINUX partition:

# 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,PSAndTopIs the two most useful commands.

PSCommand to provide a snapshot of the currently running process.

Vivid and informative course

Below isPSThe simplest form:

$ 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-EfOptionalPSReturns the complete list of all processes of all users in the system. If youPSThe command result is sentGrep. 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 youPSOutputGrepThe column title is notPSThe command itself is displayed normally. 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.

Below isPSAnother example of a command with some different options. Note that many of the columns and executions contain-EfOptionalPSThe columns are the same:

$ 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 600 pts/1 S 21:01 0:00 grep bluher 

In addition to providing important diagnostic information,PSCommands are often used to find out the PID of a runaway process. For example, one of your users enters a Cartesian join in the SQL * Plus session. You can perform the followingPSCommand to find 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 ... 

Then you can use the following command to terminate it:

$ kill 4112 

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

PSOnly snapshots of the current process are provided for you. To view the most active processes in real time, you can useTop. Top provides process information in real time. It also has an Interactive status that allows users to enter commands, suchNFollowed by numbers 5 or 10. The result indicates that top displays 5 or 10 of the most active processes.TopContinue running until you press "Q" to exitTopSo far.

The following is the top part:

8:14pm 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: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 18 0 78008 11M 1796 R 3.7 2.2 0:49 X 1965 bluher 20 0 12244 11M 10456 S 1.9 2.3 0:01 kdeinit 1986 bluher 18 0 996 996 768 R 1.5 0.1 0:00 top 1803 bluher 20 0 16960 16M 13508 S 0.9 3.3 0:03 kdeinit 1804 bluher 20 0 16964 16M 13508 S 0.9 3.3 0:03 kdeinit 1800 bluher 19 0 12260 11M 10076 S 0.1 2.3 0:04 kdeinit 1 root 20 0 220 220 188 S 0.0 0.0 0:04 init ... 

Close

It seems thatShutdownCommand to end this discussion. 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.

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.