CentOS7/RHEL7 systemd
Directory
1. Why is systemd?
(1) about Linux Service Management
(2) Advantages and Disadvantages of SysV init
(3) UpStart Improvement
(4) the birth of systemd
(5) Why can systemd be quickly started?
2. Introduction to SysV init
(1) What is SystemV?
(2) Run level of SysV init
(3) SysV init running sequence
(4) SysV init and system shutdown
(5) Management and control functions of SysV init
3. Features of systemd
(1) What problems does systemd solve?
(2) Where is the dispute over systemd?
(3) systemd can completely end the Service Process
4. systemd features of CentOS 7
(1) function of enabling socket Service
(2) Activation of inter-process communication persistence
(3) device activation
(4) file path persistence
(5) system status Snapshot
(6) Mount and automatic mount point management
(7) Parallel lightning startup
(8) unit logic simulation check
(9) backward compatible with SysV init
5. How to analyze and measure the start speed of systemd
(1) view the detailed startup time consumed by each service
(2) view the Service Tree table with high time consumption
(3) Print analysis charts and other commands
6. backward compatible with CentOS 7 systemd
(1) systemd has limited support for the running level.
(2) systemd does not support personalized commands like init scripts.
(3) systemd does not support service communication that has not been started from systemd.
(4) systemd can only stop running services.
(5) The system service information cannot be read from the standard output device.
(6) systemd does not inherit any context.
(7) SysV init script dependency
(8) Timeout Mechanism
7. systemd Service Management
(1) What is a unit?
(2) systemd Service Management
(3) view service details
8. Use systemd target
(1) how do I know what process services a target needs?
(2) target and running level
(3) target Management
9. Shut down, pause, and sleep the system
10. Manage the remote system through systemd
11. Create and modify the systemd unit file
(1) unit file Overview
(2) understand the unit file structure
(3) create a custom unit file
(4) Example of creating emacs. service:
(5) Example of creating the second sshd service
(6) modify an existing unit file
(7) Extend the default unit configuration file configuration
12. Unit instantiation
13. vnc server Configuration
1. Why is systemd?
(1) about Linux Service Management
The process from Linux system startup to service provision is as follows: first, the machine is powered on, then GRUB is loaded through MBR or UEFI, then the kernel is started, the service is started through the kernel, and then external services are started.
SysV init UpStart systemd is mainly used to solve the problem of Service Guidance management.
Tip: for the spelling of systemd, the official statement is systemd, neither Syetemd nor systemD.
(2) Advantages and Disadvantages of SysV init
SysV init is the earliest solution. It starts different service sets based on different running levels. Services are controlled by scripts and executed sequentially.
The advantages of the SysV init solution are:
Simple and easy to understand;
Depending on shell script control, the threshold for writing service scripts is relatively low.
Disadvantages:
The service is started sequentially, And the startup process is slow;
You cannot start the service as needed. For example, you usually want to enable the USB control service when inserting a USB flash disk, which can save system resources.
(3) UpStart Improvement
To solve the plug-and-play problem of system services, UpStart came into being. In CentOS6 systems, SysV init and UpStart coexist. UpStart mainly solves the plug-and-play problem of services. The UpStart solution is to group related services. Services in the group are started sequentially, and services in the group are started concurrently.
(4) the birth of systemd
The SysV init service was slow to start. It was not a problem in the past, especially in the past when the Linux system was mainly on the server system, and it was rare to restart it once a year later. Some server optical hardware detection takes more than five minutes, and the system starts very quickly.
However, with the advent of the mobile Internet, the slow startup of the SysV init service becomes more and more prominent. Many mobile devices are based on linux kernels, such as Android. Mobile devices are started frequently. It is obviously unacceptable to wait for the service sequence to start each time. systemd is designed to solve this problem.
The design idea of systemd is as follows:
Start the service as quickly as possible;
Minimize system resource usage.
(5) Why can systemd be quickly started?
Systemd uses parallel methods to start services. Unlike SysV init, it is executed sequentially, which greatly saves the system startup time.
When using parallel start, the biggest difficulty is to solve the dependencies between services. The solution of systemd is to use a buffer pool-like method. For example, if a service is dependent on TCP, the TCP port of the dependent service will be checked at startup. systemd caches the TCP port requests first. After the dependent server is started, send the request to the service so that the two services can communicate with each other. The same process communication between the D-BUS is also such a principle, directory mounting is to let the service that the directory is mounted, to the real access to the directory, to the real operation.
2. Introduction to SysV init
SysV init is a systemV-style init system. As its name suggests, it originated from the SystemV UNIX series. It provides higher flexibility than the BSD-style init system. Is a popular UNIX init system that has been used by various Linux distributions for decades.
(1) What is SystemV?
SystemV, also known as AT&T SystemV, is one of the many Unix operating systems. It was initially developed by AT&T and released for the first time in 1983. A total of four major versions of SystemV are released: versions 1, 2, 3, and 4. SystemV Release4, or SVR4, is the most successful version and becomes the source of Common UNIX features, such as "SysV initialization script" (/etc/init. d) used to control system startup and shutdown. SystemV Interface Definition (SVID) is a standard Definition of how SystemV works.
(2) Run level of SysV init
SysV init uses the term runlevel to define the "Reservation running mode ". SysV init checks whether the '/etc/inittab' file contains the 'initdefault' item. To tell whether the init system has a default running mode. If there is no default running mode, you will enter the system console and manually decide the running mode to enter.
The Running Mode in SysV init describes the running Modes of various system reservations. There are usually eight running modes, namely, the running mode 0 to 6 and S or s.
Each Linux release version has different definitions for the running mode. However, 0, 1, and 6 have been unanimously agreed by everyone:
0 Shutdown
1 single user mode
6. Restart
The work scope of various running modes is usually defined in the/etc/inittab file. For example, RedHat defines runlevel3 and 5. Run Mode 3 initializes the system to shell mode on the Character interface; run mode 5 initializes the system to GUI mode. For the command line interface or GUI, the running Modes 3 and 5 are complete and formal compared with other running modes, and the computer can complete the tasks required by the user. Modes 1 and S are often used for troubleshooting and recovery after system faults.
Obviously, the initialization preparation is different for the processes that need to be initialized in these different running modes. For example, run Mode 3 does not need to start the X system. You only need to specify the mode to enter, and SysV init is responsible for executing all the initialization work required for this mode.
(3) SysV init running sequence
SysV init uses scripts, file naming rules, and soft links to implement different runlevels. First, SysV init needs to read the/etc/inittab file. Analyze the content of this file and obtain the following configuration information:
Runlevel to be entered by the system;
Capture the definition of the key combination;
Define the power fail/restore script;
Start getty and the virtual console;
After obtaining the configuration information, SysV init performs the following steps in sequence to initialize the system as the reserved runlevelX:
/Etc/rc. d/rc. sysinit
/Etc/rc. d/rc and/etc/rc. d/rcX. d/(X indicates the running level 0-6)
/Etc/rc. d/rc. local
XDisplayManager (if needed)
1) rc. sysinit Script Function
First, run rc. sysinit to execute some important system initialization tasks. In RHEL5 of RedHat (RHEL6 already uses UpStart), rc. sysinit mainly performs the following tasks:
Activate udev and selinux;
Set the kernel parameters defined in/etc/sysctl. conf;
Set the system clock;
Load keymaps;
Activate swap partition;
Set the Host Name (hostname );
Root partition check and remount;
Activate RAID and LVM devices;
Enable disk quota;
Check and mount all file systems;
Clear expired locks and PID files;
2) rc. d script
After completing the above work, SysV init starts to run the/etc/rc. d/rc Script. Based on different runlevels, the rc Script opens the rcX. d directory (X is runlevel) corresponding to runlevel, and finds and runs all the startup scripts stored in this directory. Each runlevelX has a directory named/etc/rc. d/rcX. d.
Many different scripts are stored in these directories. Scripts whose names start with "S" are the scripts that should be run at startup. The numbers following "S" define the execution sequence of these scripts. The scripts in the/etc/rc. d/rcX. d directory are actually some soft link files, and the actual script files are stored in the/etc/init. d directory. As follows:
Scripts in the rc5.d directory
[Root @ www ~] # Ll/etc/rc5.d/
Lrwxrwxrwx1rootroot16Sep42008K02dhcdbd-> ../init. d/dhcdbd
... (Omitted in the middle )....
Lrwxrwxrwx1rootroot14Sep42008K91capi-> ../init. d/capi
Lrwxrwxrwx1rootroot23Sep42008S00microcode_ctl->.../init. d/microcode_ctl
Lrwxrwxrwx1rootroot22Sep42008S02lvm2-monitor-> ../init. d/lvm2-monitor
... (Omitted in the middle )....
Lrwxrwxrwx1rootroot17Sep42008S10network-> ../init. d/network
... (Omitted in the middle )....
Lrwxrwxrwx1rootroot11Sep42008S99local->.../rc. local
Lrwxrwxrwx1rootroot16Sep42008S99smartd-> ../init. d/smartd
... (Omitted below )....
After all the initialization scripts are executed. Run the/etc/rc. d/rc. local script in SysV init.
Rc. local is a place for users to customize Linux settings. You can put the things you want to set and start on your own here. There are generally more than one user of a LinuxServer, so you have to consider this.
(4) SysV init and system shutdown
SysV init not only initializes the system, but also closes the system. When the system is shut down, in order to ensure data consistency, you need to end and clean up in order with caution.
For example, you should stop the service that has read and write operations on the file system, and then umount the file system. Otherwise, data will be lost.
This sort of control also relies on/etc/rc. d/rcX. d/The naming rules of all scripts in the directory are controlled. All scripts starting with K in the directory will be called when the system is disabled, the numbers after the letter K define their execution sequence.
These scripts are responsible for securely stopping services or other close operations.
(5) Management and control functions of SysV init
In addition, after the system is started, the administrator needs to manage and control the started processes. The SysV init package contains a series of tools to control startup, run, and close all other programs.
Halt stops the system.
Init is the init process entity of SysV init. It runs as pid1 and is the parent process of all user processes. The main role is to use the/etc/inittab file to create a process during startup.
Killall5 is the killall command of System V. Sends signals to other processes except their own session processes. Therefore, the current shell cannot be killed.
The last trace/var/log/wtmp file (or the file specified by the-f option) shows the logon status of all users since the file was created.
Lastb is similar to last. By default, the/var/log/btmp file is used to display all failed logon attempts.
Mesg controls access to user terminals by other users.
Pidof identifies the process ID (pid) of the program and outputs it to the standard output device.
Poweroff is equal to shutdown-h-p, or telinit0. Shut down the system and cut off the power supply.
Reboot is equivalent to shutdown-r or telinit6. Restart the system.
Runlevel reads the system's logon record file (usually/var/run/utmp) and outputs the previous and current system running level to the standard output device.
Shutdown terminates the system in a safe way. All users logging on will receive a notification that the system is about to terminate and are not allowed to log on again.
Sulogin is called by init when the system enters the single-user mode. When you receive the-B Option passed by the START loader, init ALSO CALLS sulogin.
Telinit is actually a connection of init, which is used to transmit single-character parameters and signals to init.
Utmpdump displays the content of the/var/run/utmp file to the standard output device in a user-friendly format.
Wall sends messages to all login users with information permissions.
Different Linux distributions have developed some auxiliary tools based on these basic SysV init tools to simplify the management of the init system. For example, the RHEL of RedHat developed the initscripts software package based on SysV init, which contains a large number of startup scripts (such as rc. also provides command line tools such as service and chkconfig, and even a set of graphical interfaces to manage the init system. Other Linux distributions also have their own initscript or other init software packages to simplify the management of SysV init.
As long as you understand the SysV init mechanism, in the simplest system with only SysV init, you can directly call the script to start and stop the service, manually create inittab and create a soft connection to complete these tasks. Therefore, it is most important to understand the basic principles and commands of SysV init. You can even develop a set of management tools.
For more details, please continue to read the highlights on the next page: