Reading Notes: laruence's Linux Private food-Basic Learning (third edition) (Chapter 13-17)

Source: Internet
Author: User
Tags dmesg

 
Chapter 2 bash shell Learning

Chapter 2 Linux Account Management and ACL permission settings

/Etc/shadow file


  1. Su usage

Su summarized the usage as follows:

To completely switch to the new user environment, you must use "Su-username" or "Su-l username 』, will be converted into the new user environment together with the path/user/mail variables;

If you only want to execute the root command once, you can use the "Su-c" command string "method for processing;

When you use root to switch to any user, and you need to enter the password of the new user;

Although it is very convenient to use Su, the disadvantage is that when my host is in a multi-person environment, if you want to use Su to switch to the root identity, so everyone needs to know the root password, so that too many people know the password may flow out, it is not appropriate! What should I do? Processing through sudo!

  1. Sudo

Compared with Su, you need to know the new user password (usually the root password). To execute sudo, you only need your own password! You can even set sudo without a password! Because sudo allows you to execute commands as other users (generally using the root identity), not all users can execute sudo, instead, only users in/etc/sudoers can execute the sudo command.

Chapter 1 Disk Quota (quota) and advanced File System Management

Chapter 4 program management

  • Ps command
  1. PS: Obtain the program operation status at a certain time point

  2. Only observe your own bash-related programs: PS-l

    The overall program operation of the system is very large, but if you use PS-L, only the programs related to your operating environment (BASH) are listed,

    That is, the parent program at the top will be your own bash and will not be extended to the INIT program! Let's take a look:

  • F: Process flags indicates the summary permission of the program. Common numbers include:

    If the value is 4, the permission of the program is root;

    If the value of O is 1, it indicates that this subroutine is only copied (fork) but not actually executed (EXEC ).

  • S: indicates the state of the Program (STAT). The main states are:

    O r (running): The program is running;

    O s (sleep): The program is currently in sleep state (idle), but can be awakened (signal ).

    O d: sleep state that cannot be awakened. Usually this program may be waiting for I/O (ex> Print)

    O t: Stop, which may be in the work control (background pause) or traced status;

    O z (zombie): zombie state. The program has been terminated but cannot be removed out of memory.

  • UID/Pid/ppid: indicates "PID Number of the program owned by the UID/PID Number of the Program/PID Number of the parent program of the program 』
  • C: indicates the CPU usage. Unit: percentage;
  • PRI/Ni: Abbreviation of priority/Nice, indicating the priority of the Program executed by the CPU. The smaller the value, the faster the program is executed by the CPU.
  • ADDR/sz/wchan: both of them are related to the memory. ADDR is the kernel function, which indicates the part of the program in the memory. If it is a running program, generally, "-"/SZ indicates the amount of memory used by the program./wchan indicates whether the program is currently running. Similarly, if "-" indicates that the program is running.
  • TTY: the location of the login terminal. If it is a process logon, use the dynamic Terminal interface (pts/N );
  • Time: the CPU time used. Note that the time consumed by the Program for CPU operation is not the system time;
  • CMD: the abbreviation of command. What is the command that triggers the program.

In the PS-L output message you see, it indicates: "The bash program belongs to the user whose uid is 0 and the state is sleep ), the reason for sleep is that it triggers PS (the State is run. The PID of this program is 13639, the priority is 75, the terminal interface obtained by Bash is pts/1, and the operation status is waiting (wait ).』 Is this clear enough?

  1. Observe all programs in the system: PS aux

    You will realize that PS-L and PS aux display items are not the same! In the project displayed by PS aux, each field has the following meanings:

  • User: which user account does the process belong?
  • PID: The program identifier of the process.
  • % CPU: Percentage of CPU resources used by the process;
  • % Mem: Percentage of physical memory occupied by the process;
  • Vsz: virtual memory used by the process (Kbytes)
  • RSS: the fixed amount of memory occupied by the process (Kbytes)
  • TTY: The process operates on that terminal. If it is not related to the terminal, it is displayed ?, In addition, the tty1-tty6 is the login program on the machine, if the PTS/0 and so on, it indicates the program connected to the host by the network.
  • Stat: Current State of the program. The State is displayed as the s flag of PS-l (R/S/T/Z)
  • Start: the time when the process is triggered;
  • Time: the actual CPU usage time of the process.
  • Command: What is the actual command of the program?

In general, PS aux will sort and display according to the PID order. We will explain it with the line of the PID of 13639! Meaning of this row

It means that the bash PID executed by root is 13639, and the percentage of memory capacity occupied is 0.2%, and the State is sleep (s). This program starts

The moving time is, and the obtained terminal environment is pts/1 .』 What PS aux sees is actually the same program!

  1. Top: dynamically observe program changes

    Compared with PS, you can obtain the state of the program at a time point. Top can continuously detect the state of the program operation! The usage is as follows:

    Top is also a good tool for program observation! However, unlike the static output of PS, the top program can continuously monitor the running state of the entire system program. By default, each update of program resources takes 5 seconds. However, you can use-D to modify the resource. Top is divided into two screens. The above picture shows the resource usage status of the entire system. There are basically six rows in total, and the displayed content is in sequence:

  • The first line (top...) shows the following information:

    O the current time, that is, the project at 17:03:09;

    O the time that has elapsed since the startup, that is, the project up 7 days;

    O number of users who have logged on to the system, that is, the 1 user project;

    O system average workload in 1, 5, 15 minutes. The batch we learned in Chapter 1 is working with a load of less than 16th! It indicates 1, 5, 15 minutes, and the system is responsible for running several programs (work) on average. The smaller the number, the more idle the system is. If the number is higher than 1, you must pay attention to whether your system program is too complicated!

  • Line 2 (tasks...): displays the total number of current programs and the status of individual programs (running, sleeping, stopped, zombie ). Note that the value of zombie is not 0! Let's take a look at whether the process has become a zombie?
  • Row 3 (CPUs...): displays the overall CPU load. Can each project be used? Note that % wa represents I/O wait. Generally, your system will slow down because I/O has a big problem! So pay attention to the CPU resources consumed by this project! In addition, if it is a multi-core device, you can press the number key "1" to switch to the load rate with the same CPU.
  • Line 4 and Line 5: displays the current usage of physical memory and virtual memory (MEM/SWAP. Again, we should note that the usage of SWAp should be minimized! If swap is used heavily, it indicates that the physical memory of the system is sufficient!
  • Row 6: displays the status of the command entered in the top program.

For the top half of the screen, it is the resources used by each process. Note that:

PID: the ID of each process!

User: the user to which the process belongs;

PR: Abbreviation of priority. The execution sequence of the program is prioritized. The smaller the value, the earlier the program is executed;

Ni: Short for nice, which is related to priority. It is executed earlier and earlier;

% CPU: CPU usage;

% Mem: memory usage;

Time +: The sum of CPU usage time;

Top by default, CPU usage (% CPU) is used as the focus of sorting. If you want to sort by memory usage, you can press "M". If you want to reply, press "p. If you want to leave top, press Q! If you want to output the top result to an archive, you can do this:

  • System resource observation

    In addition to the system program, we must also check some system resources! For example, we can use top to view many system resources.

(1) Free: Observe memory usage

Take a closer look, my system has about 725 MB of physical memory, and my swap has about 1 GB, so when I use free-m to display it in Mbytes, the above information appears. The row mem shows the amount of physical memory, while the swap shows the amount of virtual memory. Total is the total amount, used is the used amount, and free is the remaining available amount. The following shared/buffers/cached is used as a buffer and cache in the amount used.

Taking a closer look at the output of Example 1, our Linux testing host is very ordinary and has no work at all. However, my thing memory is almost exhausted! However, at least 132 MB is used for buffer memory (buffers) and 287 MB is used for cache (cached). That is to say, the system is "very efficient to use all the memory 』, the purpose is to accelerate the system's access efficiency!

Many server friends will ask this question: "My system is very easy. Why is the memory used up ?』 Now? It is normal to be used up! But pay attention to the amount of swap. In general, it is recommended that swap not be used, especially swap should not be used more than 20%. If you realize that the usage of swap exceeds 20%, it is better to buy physical memory for insertion! Because the performance of swap is much inferior to the physical memory, and the system will use swap, it is definitely because of the physical memory and enough!

TIPS:

To speed up the system performance, Linux caches the most commonly used or recently used file data. In this way, when the system wants to use this file in the future, it is directly retrieved from the memory without re-reading the hard disk. Of course, the speed is faster! Therefore, it is normal that the physical memory is used up!

Uname: Query System and core information

  1. Uptime: Observe the system startup time and workload

This command is simple! Shows how long the current system has been started, and the average load of 1, 5, 15 minutes. Remember top? That's right! This uptime can display the top line of the top screen!

  1. Netstat: tracks network or slot files

    This netstat is also very fun. In fact, this command is often used in network monitoring. It also needs to be understood in program management! The execution of this command is as follows: basically, the output of netstat is divided into two parts: the network and the program of the system:

  1. Dmesg: Analyze core messages

When the system is turned on, the core will detect the system hardware. Whether or not some of your hardware has been caught is related to the detection at this time. However, these detection processes must not be displayed on the screen, that is, they flash on the screen! Can we capture the core detection information? Yes. Use dmesg!

All core detection messages, whether at startup or during system operation, are recorded in a protected area in the memory as long as they are generated by the core. The dmesg command can read the messages in this segment! Because there are too many messages, you can add this pipeline command "| more" during execution to pause the screen!

Let's use Example 2 to find out what the hard disk format of my host is! That's right! You can also check whether the network card can be found! The network card code is eth. Therefore, enter dmesg | grep-I ETH directly.

(6) vmstat: detects system resource changes

If you want to dynamically understand the operation of system resources, you can play with vmstat! Vmstat can detect the "CPU/memory/disk input/output status" and so on. If you want to know which part of a busy system is the most tiring, you can use vmstat for analysis. Below are common options and parameter descriptions:

  • The meaning of/proc /*

In fact, the so-called programs we mentioned earlier are all in the memory! The data in the memory is written to the/proc/* directory. So, of course, we can directly observe the files in the/proc directory!

  • Query files with enabled or enabled by executing programs

(1) Fuser: Identify the program using the file (or file system)

Sometimes I want to know how many files my program has opened during this startup, so I can use Fuser to observe it! For example, if the system prompts "device is busy" when you detach the file system, it indicates that the file system is busy, indicating that a program is better at using the file system! You can use Fuser to track data! The Fuser syntax is a bit like this:

(2) lsof: list the file names opened by the program

Compared with Fuser, files or devices are used to find programs that use the files or devices. Conversely, how can we find out the files and devices opened or used by a program? Call! That is, use lsof ~

(3) pidof: Find the PID of a program being executed

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.