Detailed description of systemd, systemd

Source: Internet
Author: User

Detailed description of systemd, systemd

CentOS 7 replaces SysV with systemd. The objective of Systemd is to replace the init system that has been used since the Unix era. It is compatible with the startup scripts of SysV and LSB, and can effectively guide loading services during process startup.

Systemd has the following features:

  • Support for parallel Tasks
  • Both socket-type and D-Bus-type activation services are used;
  • Start daemon on demand );
  • Use Linux cgroups to monitor processes;
  • Snapshots and system recovery are supported;
  • Maintain mount points and automatic mount points;
  • Services are precisely controlled based on dependencies.

Systemd basic tools
The main command for viewing and controlling systemd is systemctl. This command can be used to view system status and manage systems and services. For details, see man 1 systemctl.

TIPS: adding-H <user name >@< Host Name> to the systemctl parameter can remotely control other machines. This process uses the ssh link.

Note: systemadm is the official graphic frontend of systemd.

Analyze System Status
Output activation unit:

$ systemctl

The following command is equivalent:

$ systemctl list-units

Output failed units:

$ systemctl --failed

All available unit files are stored in the/usr/lib/systemd/system/and/etc/systemd/system/directories (the latter has a higher priority ). View All installed services:

$ systemctl list-unit-files

Unit of use
A unit configuration file can be described as one of the following: System Service (. service), mount point (. mount), sockets (. sockets, system device, swap partition/file, start target, file system path, and timer managed by systemd. For more information, see man 5 systemd. unit.

When you use a systemctl control unit, you usually need to use the full name of the unit file, including the extension (such as sshd. service ). However, some units can be abbreviated in systemctl.

  • If there is no extension, systemctl treats the extension as. service by default. For example, netcfg and netcfg. service are equivalent.
  • The mount point is automatically converted to the corresponding. mount unit. For example,/home is equivalent to home. mount.
  • The device is automatically converted to the corresponding. device unit, so/dev/sda2 is equivalent to dev-sda2.device.

Activate now unit:

# Systemctl start <unit>

Stop Unit now:

# Systemctl stop <unit>

Restart unit:

# Systemctl restart <unit>

Command Unit re-read Configuration:

# Systemctl reload <unit>

Output unit running status:

$ Systemctl status <unit>

Check whether the unit is configured to start automatically:

$ Systemctl is-enabled <unit>

Automatic activation unit:

# Systemctl enable <unit>

Note: If the service does not have an Install section, it generally means that it should be automatically called through other services. If you need to install it manually, you can directly connect to the Service as follows (replace foo with the actual service name ):

# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/

Cancel automatic activation unit:

# Systemctl disable <unit>

Display the unit manual page (which must be provided by the unit file ):

# Systemctl help <unit>

Reload systemd to scan for new or changed units:

# systemctl daemon-reload

Power Management
You can use power management only after installing polkit.

If you are logging on to a local mongoemd-logind user session, and there are no other active sessions currently, the following command can be executed without the root permission. Otherwise (for example, if another user logs on to a tty, systemd automatically requests the root password.

Restart:

$ systemctl reboot

Exit the system and stop the power supply:

$ systemctl poweroff

Standby:

$ systemctl suspend

Sleep:

$ systemctl hibernate

Hybrid sleep mode (both sleep to the hard disk and standby ):

$ systemctl hybrid-sleep

Native systemd configuration file

Note: you may need to manually create some files. All files have permissions of 644 root, the owner group root.

Virtual console
You can use the/etc/vconsole. conf file or localectl to configure the virtual console, including the keyboard layout and console font. For details, visit the console font and keyboard layout.

Hardware clock
The default hardware clock of systemd is Coordinated Universal Time (UTC ).

TIPS: We recommend that you use the NTP service to synchronize the hardware clock online.

When the hardware clock is set to local
When you set the hardware clock to a local clock (not recommended ):

# timedatectl set-local-rtc true

Reset to UTC:

# timedatectl set-local-rtc false

If you set the local time, It is troublesome to process the timeout. If the timeout value is adjusted when the instance is shut down, the next time the instance is started, a problem occurs. The latest kernel reads time directly from the real-time clock chip (RTC) without hwclock. The kernel treats the time read from RTC as UTC. Therefore, if the hardware time is local, the system start time is incorrect and will be corrected soon. This may cause some problems (especially when the time goes backwards ).

If the Windows operating system is installed at the same time (when local is used by default), RTC is usually set to local. Windows can also process UTC, and you need to modify the registry. We recommend that you use UTC for Windows, not for Linux. After Windows uses UTC, remember to Disable Windows time synchronization to Prevent Windows from setting hardware time incorrectly. As mentioned above, Linux can use the NTP service to synchronize hardware clocks online.

Kernel module
Please visit Kernel modules

File System mounting
By default, the system automatically checks and mounts partitions before starting a service that needs to mount a specific partition. The Network File System (such as NFS and Samba) set in/etc/fstab can work normally without configuration. systemd will ensure that the Network File System is mounted after the network connection is ready.

For details, see man 5 systemd. mount.

LVM
If an LVM volume that is not activated through initramfs is installed, you need to start the lvm-monitoring Service (provided by the lvm2 package ):

# systemctl enable lvm-monitoring

ACPI Power Management
See Power Management

Temporary Files
/Usr/lib/tmpfiles. d/and/etc/tmpfiles. the files in d/describe how to create, clean, and delete temporary files and directories in systemd-tmpfiles, which are usually stored in/run and/tmp. The configuration file name is/etc/tmpfiles. d/<program>. conf. The configuration here can overwrite the configuration with the same name in the/usr/lib/tmpfiles. d/directory.

Temporary files are usually provided together with service files to generate the files and directories required by the daemon. For example, the Samba service requires the directory/run/samba to exist and set the correct permission bit, as shown in the following figure:

/usr/lib/tmpfiles.d/samba.confD /run/samba 0755 root root

In addition, temporary files can be used to write certain content to a specific file at startup. For example, to disable system wakeup from a USB device, you can use echo USBE>/proc/acpi/wakeup to use the old/etc/rc. local file, but now you can do this:

/etc/tmpfiles.d/disable-usb-wake.confw /proc/acpi/wakeup - - - - USBE

For more information, see man 5 tmpfiles. d.

Note: This method cannot add parameters to the configuration file in/sys, because systemd-tmpfiles-setup may run before loading the relevant module. In this case, you must first use modinfo <Module name> to confirm the required parameters and set the parameters in a file in/etc/modprobe. d. In addition, you can use udev rules to set properties when the device is ready.

Compile your own. service file
The cell file of systemd is inspired by the XDG Desktop Entry. desktop file, and its initial origin is the. ini file in Windows.

Process dependency
When using systemd, You can correctly compile the unit configuration file to solve the dependency. In typical cases, unit A requires Unit B to run before unit A starts. In this case, add Requires = B and After = B to the [Unit] section of Unit A configuration file. If this dependency is optional, you can add Wants = B and After = B. Note that Wants = and Requires = do not mean After =, that is, if the After = option is not set, the two units will be started in parallel.

Dependencies are usually used on services rather than targets. For example, network.tar get is generally introduced by a service that configures Network Interfaces. Therefore, you can arrange custom units after the service because network.tar get has been started.

Startup Mode
When writing a custom service file, you can select several different service startup methods. You can set the startup mode by using the Type = parameter in the [Service] section of the configuration file. For detailed parameter descriptions, see man systemd. service.

  • Type = simple (default): systemd determines that the service will be started immediately. The service process does not fork. If the service is to start other services, do not use this type of startup unless the service is socket-activated.
  • Type = forking: systemd determines that the service is successfully started when the service process fork and the parent process exits. For conventional daemon, use this type of daemon unless you are sure that this startup method cannot meet your needs. Use this Startup type to specify PIDFile = at the same time so that systemd can track the main process of the service.
  • Type = oneshot: This option applies to services that run only one task and then exit immediately. You may need to set RemainAfterExit = yes at the same time so that systemd still considers the service to be active after the service process exits.
  • Type = policy: it is the same as Type = simple, but the service will send a signal to systemd after it is ready. The implementation of this notification is provided by the libsystemd-daemon.so.
  • Type = ready: If this mode is enabled, systemd considers the Service ready when the specified BusName appears on the worker system bus.

Modify an existing unit file
To change the unit file provided by the software package, first create/etc/systemd/system/<unit name>. d/directory (such as/etc/systemd/system/httpd. service. d/), and then put *. conf file, where you can add or reset parameters. The parameter priority set here is higher than the original unit file. For example, to add an additional dependency, create such a file:

/Etc/systemd/system/<unit>. d/customdependency. conf [Unit] Requires = <New dependency> After = <New dependency>

Run the following command to make the change take effect:

# Systemctl daemon-reload # systemctl restart <unit>

In addition, copy the old unit file from/usr/lib/systemd/system/to/etc/systemd/system/and modify the file to achieve the same effect. The priority of the cell files in the/etc/systemd/system/directory is always higher than that in the/usr/lib/systemd/system/directory. Note: When the unit files in/usr/lib/are changed due to software package upgrade, the custom unit files in/etc/are not updated synchronously. In addition, you have to execute systemctl reenable <unit> to manually re-enable the unit. Therefore, we recommend that you use the *. conf method.

TIPS: You can use the systemd-delta command to check which unit files are overwritten and modified.

Vim syntax highlighting support for unit configuration files
You can install the vim-systemd package from the official repository to enable the unit configuration file to support syntax highlighting under Vim.

Target)
Runlevel is an old concept. Currently, systemd introduces a concept similar to the startup-level function-target ). Unlike the startup level represented by numbers, each target has a name and unique features, and multiple targets can be enabled at the same time. Some targets inherit services from other targets and start new services. Systemd provides some targets that mimic the sysvinit startup level, and can still use the old telinit startup level command switch.
Get current target

Do not use the runlevel command:

$ systemctl list-units --type=target

Create a new target
In Fedora, startup levels 0, 1, 3, 5, and 6 are assigned for specific purposes and all correspond to a objective of systemd. However, there is no good method for porting User-Defined startup levels (2, 4. Similar functions can be implemented based on the original startup level, create a new target/etc/systemd/system/<New Target> (refer to/usr/lib/systemd/system/graphical.tar get ), create/etc/systemd/system/<New Target>. the wants directory. Add a link to the additional service (to the unit file in/usr/lib/systemd/system ).

Target table

SysV startup level Objective of Systemd Note
0 Runlevel0.target, poweroff.tar get Interrupt System (halt)
1, s, single Runlevel1.target, rescue.tar get Single User Mode
2, 4 Runlevel2.target, runlevel4.target, multi-user.target User-Defined startup level, usually identified as Level 3.
3 Runlevel3.target, multi-user.target Multiple users without graphic interfaces. You can log on through a terminal or network.
5 Runlevel5.target, graphical.tar get Multi-user, graphical interface. Inherit Level 3 services and start the graphic interface service.
6 Runlevel6.target, reboot.tar get Restart
Emergency Emergency.tar get Emergency mode (Emergency shell)

Switch the startup level/Target
In systemd, the start level is accessed through the "target unit. Use the following command to switch:

# systemctl isolate graphical.target

This command does not affect the next startup. It is equivalent to telinit 3 or telinit 5.

Modify the default startup level/Target
The target for starting the instance is default.tar get, which is linked to graphical.tar get by default (roughly equivalent to the original startup Level 5 ). You can change the default startup level through kernel parameters:

Tip: the extension. target can be omitted.

  • Systemd.unit1_multi-user.tar get (roughly equivalent to level 3)
  • Systemd.unit1_rescue.tar get (roughly equivalent to level 1)

Another method is to modify default.tar get. You can modify it through systemctl:

# systemctl enable multi-user.target

The Command Execution status is shown by systemctl: The Link/etc/systemd/system/default.tar get is created, pointing to the new default startup level. This method is valid only when the target configuration file contains the following content:

[Install]Alias=default.target

At present, multi user.target1_graphical.tar get contains this content.

Logs
Systemd provides its own logging system, called journal. It uses systemd logs without additional installation of Log Service (syslog ). Command for reading logs:

# journalctl

By default (when Storage = is set to auto in the file/etc/systemd/journald. conf), log records will be written to/var/log/journal /. This directory is part of the systemd package. If it is deleted, systemd does not automatically create it until the directory is re-created the next time the software package is upgraded. If this directory is missing, systemd writes the log records to/run/systemd/journal. This means that logs will be lost after the system is restarted.

Filter output
Journalctl can filter the output based on specific fields, for example:

Show all logs after this start:

# journalctl -b

However, what you are more concerned about is not the log after this startup, but the log at the last startup (for example, the system just crashed ). This feature is not yet available and is being discussed in systemd-devel@lists.freedesktop.org.

The current compromise is:

# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac

The preceding command outputs all the startup information for the current day. Note that if there are many logs, the command execution time will be long.

Dynamic Tracking of the latest information:

# journalctl -f

Display All messages of a specific program:

 # journalctl /usr/lib/systemd/systemd

Display All messages of a specific process:

# journalctl _PID=1

Display All messages of a specified unit:

# journalctl -u netcfg

For more information, see man journalctl, man jouremd. journal-fields.

Log Size Limit
If logs are retained by the preceding operations, the default log size is up to 10% of the file system capacity. That is, if/var/log/journal is stored in the root partition of 50GiB, logs can store up to 5 gib of data. You can modify SystemMaxUse in/etc/systemd/journald. conf to specify the maximum limit. For example, the maximum log size is 50MiB:

SystemMaxUse=50M

For more information, see man journald. conf.

Use with syslog
Systemd provides socket/run/systemd/journal/syslog To be compatible with traditional log services. All system information is passed in. To make the traditional log service work, you need to have the service link to the socket instead of/dev/log (official description ). Syslog-ng in the Arch software warehouse already contains the required configuration.

Set boot syslog-ng:

 # systemctl enable syslog-ng

Troubleshooting
Shutdown/restart very slowly
If the shutdown is particularly slow (or even the same as a crash), it is likely that a service that refuses to quit is playing a strange role. Systemd waits for a while and then tries to kill it. Read this article to check whether you are the victim of this issue.

No log record for short-term processes
If journalctl-u foounit. service does not display any output of a short-term process, use PID instead. For example, if the systemd-modules-load.service fails, first use systemctl status into EMD-modules-load to query its PID (for example, 123) and then retrieve the log journalctl-B _ PID = 123 related to the PID. The log metadata (such as _ SYSTEMD_UNIT and _ COMM) of the running process is collected in the/proc directory in disorder. To fix this problem, you must modify the kernel so that it can provide the above data through a socket connection. This process is similar to SCM_CREDENTIALS.

Diagnose startup Problems
Use the following Kernel Parameter boot: systemd. log_level = debug systemd. log_target = kmsg log_buf_len = 1 M

Do not dump memory when the program crashes
To use the old kernel dump, create the following file:

/etc/sysctl.d/49-coredump.confkernel.core_pattern = corekernel.core_uses_pid = 0

Then run:

# /usr/lib/systemd/systemd-sysctl

You may also need to execute "unlimit" to set the file size:

$ ulimit -c unlimited

Original article: https://blog.linuxeye.com/400.html

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.