The simplest and most concise Systemd tutorial, which takes 10 minutes

Source: Internet
Author: User

The simplest and most concise Systemd tutorial, which takes 10 minutes

Linux 101: the most effective use of Systemd

Why?

  • Understand the significant changes in the release of modern Linux;
  • Let's see how Systemd replaces SysVinit;
  • Deal with unit and new journal logs.

Lennart Poettering, author of Systemd, has long been accustomed to receiving such emails, personal attacks, and death threats. The Red Hat employee angered the nature of the FOSS community (http://tinyurl.com/poorlennart) on Google + and said with grief and disappointment: "That's a really disgusting place ". He highlighted Linus Torvalds's harsh post on the mailing list and condemned the kernel leader to set the tone for online discussions, making personal attacks and depression the norm.

But why does Poettering suffer so much hate? Why should such an open-source software developer endure such anger? The answer lies in the importance of his software. In most distributions, Systemd is the first program initiated by the Linux kernel and plays multiple roles. It starts system services, processes user logins, and executes some tasks at specific times. There are many more. It continues to grow and gradually becomes a "basic system" of Linux-providing all the tools required for system startup and release maintenance.

Currently, Systemd is quite controversial in the following aspects: it evades some established Unix traditions, such as plain text log files; it is regarded as a "big unification" project, trying to take over everything; it is also an important innovation behind our operating system. However, most mainstream releases have accepted (or are about to accept) it, so it will survive. It is also advantageous: Faster startup, simpler management of dependent service programs, and powerful and secure log systems.

Therefore, in this tutorial, we will explore the features of Systemd and show you how to use these features most effectively. Even if you are not a fan of this software at the moment, you can at least better understand and adapt to it after reading this article.

This not serious cartoon comes from http://tinyurl.com/m2e7mv8, which shapes Systemd into a violent animal and swallowed up everything it passes. The comments of most critics are not as soft as the doll.

Startup and service

Most mainstream releases either adopt Systemd or will be used in the next release (such as Debian and Ubuntu ). In this tutorial, we use a preview version of Fedora 21, which is already an excellent lab site of Systemd, the commands and precautions should be the same. This is one of the highlights of Systemd: It eliminates many subtle and trivial differences between different releases.

Enterps ax | grep systemdIn the first line, the number 1 indicates that the process number is 1, that is, it is the first program initiated by the Linux kernel. Therefore, once the kernel detects the hardware and organizes the memory, it runs/usr/lib/systemd/systemdExecutable program, which initiates other programs in sequence. (In the days when there is no Systemd, the kernel will run/sbin/initLater, the program will run other startup scripts in the system named SysVinit .)

The core of Systemd isUnitIt is a configuration file that contains information about services (programs running in the background), devices, mount points, and other aspects of the operating system. One of the goals of Systemd is to simplify the interaction between these things. Therefore, if a program needs to be created at a mount point or start running after a device is connected, systemd makes it quite easy to operate. (In the days when there is no Systemd, it is rather ugly to deploy these things with scripts .) To list all units in your Linux system, enter the following command:

  1. systemctllist-unit-files

Now,systemctlIt is the main tool for interacting with Systemd. It has many options. In the cell list, you will notice some formatting: enabled cells are displayed in green and disabled cells are displayed in red. Units marked as "static" cannot be directly enabled. They are the objects on which other units depend. To restrict the output list to contain only services, run the following command:

  1. systemctllist-unit-files --type=service

Note that a unit is displayed as "enabled", which does not mean that the corresponding service is running, but can only be enabled. To obtain information about a specific service, run the following command to obtain the information about a specific service:

  1. systemctl status gdm.service

This command provides a lot of useful information: a service description, the location of the unit configuration file, the start time, and the process number, and its affiliated CGroups (used to limit the resource overhead of each group of processes ).

If you want to view/usr/lib/systemd/system/gdm.serviceUnit configuration file, you can see various options, including the binary file to be run (The Line "ExecStart ), other conflicting units (that is, the units that cannot enter the operation at the same time), and the units that need to be executed in this unit (the line "After ). Some units have additional dependency options, such as "Requires" (necessary dependencies) and "Wants" (optional dependencies ).

Another interesting option here is:

  1. Alias=display-manager.service

After you start the agentsystemctl status display-manager.serviceTo view its status. When you know thatShow hypervisorThis option is useful when you are running and want to do something for it, but you do not care about whether it is a running agent, such as the display manager, KDM, XDM, or other display manager.

Run the systemctl status command followed by a unit name to check the corresponding service.

"Target" is locked

If you/usr/lib/systemd/systemInput in DirectorylsCommand, you will see a variety.target.Startup target(Target) is a way to aggregate multiple units so that they can be started at the same time. For example, for most Unix-like operating systems, there is a "multi-user" status, which means that the system has been successfully started and the background service is running, and have prepared one or more users to log on and work-at least in text mode. (Other statuses include the single-user status used for management, and the reboot status used for machine shutdown .)

If you open the multi-user.target file, you may expect a list of units to be started. However, you will find that this file is almost empty-in fact, a service will use the WantedBy option to make itself a dependency for the startup target. So if you open the avahi-daemon.service, NetworkManager. service, and other. service files, you will see this line in the Install segment:

  1. WantedBy=multi-user.target

Therefore, switching to a Multi-User Startup target will enable the units that contain the preceding statements. Other startup targets are available (for example, emergency.tar get provides an emergency shell and halt.tar get is used for machine shutdown). You can easily switch between them in the following ways:

  1. systemctl isolate emergency.target

In many ways, these operations are similarRunning level(Runlevel), such as text-mode multi-user.target depends on the 3th runtime level, graphical.tar get gets depends on the 5th runtime level, reboot.tar get is similar to 6th runtime level, and so on.

Compared with traditional scripts, the Unit configuration file may seem unfamiliar, but not difficult to understand.

Enable and stop

Now you may have been thinking: we have read so much, but still haven't seen how to stop and start the service! There is a reason for this. From the external perspective, Systemd may be very complicated and cannot be controlled like a beast. Therefore, before you start to play with it, it is necessary to look at how it works from a macro perspective. The commands used to manage services are very simple:

  1. systemctl stop cups.service
  2. systemctl start cups.service

(If a unit is disabled, You can firstsystemctl enableAdd the unit name to enable it. This method creates a symbolic link for the Unit and places it in.wantsDirectory.wantsDirectory in/etc/systemd/systemFolder .)

Two other useful commands are as follows:systemctl restartAndsystemctl reload, Followed by the unit name. The latter is used to re-load the Unit's configuration file. Most of Systemd has good documentation, so you can view the Manual (man systemctl) Understand the details of each command.

Timer unit: replace Cron

In addition to system initialization and service management, Systemd also highlights other aspects. To a large extent, it can complete cron work, and it can be said that it is in a more flexible way (with more readable syntax ). Cron is a program that executes tasks at specified intervals, such as clearing temporary files and refreshing cache.

If you enter/usr/lib/systemd/systemDirectory, you will see multiple.timerFile. UselessTo view these files, you will find that.serviceAnd.targetThe file has a similar structure, but the difference is that[Timer]. For example:

  1. [Timer]
  2. OnBootSec=1h
  3. OnUnitActiveSec=1w

The OnBootSec option tells Systemd to start the unit one hour after the system starts. The second option means that the unit is started once a week since then. You can set a large number of timer options.man systemd.timeView the complete list.

The Time Precision of Systemd is one minute by default. That is to say, it will run the unit within one minute of the set time, but not necessarily accurate to that second. This is based on power management, but if you need a timer that has no latency and is accurate to milliseconds, you can add the following line:

  1. AccuracySec=1us

In addition, the WakeSystem option (which can be set to true or false) determines whether the timer can wake up a machine in sleep state.

There is a graphic interface program called Systemd, even if it has not been actively maintained for many years.

Log File: Ask journald

The second major part of Systemd is journal. This is a log system, similar to syslog, but also has some significant differences. If you are a fan of the Unix log management mode, be prepared to get angry: This is a binary log, so you cannot use the conventional command line text processing tool to parse it. This design had unexpectedly aroused heated debate on the Internet, but it did have some advantages. For example, logs can be organized more systematically with more metadata, so information can be filtered out more easily based on executable file names and process numbers.

To view the entire journal, enter the following command:

  1. journalctl

Like many other Systemd commands, this command leads the outputlessProgram, so you can use the space key to scroll down, Type/(Slash) search, and other familiar shortcut keys. You can also see a little color here, such as red warnings and error messages.

The preceding command outputs a lot of information. To restrict the output of only the started message, run the following command:

  1. journalctl-b

This is where Systemd shines! Do you want to view all messages since the last startup? Try journalctl-B-1. The last time? Replace-1 with-2. So since a specific time, such as October 24, 2014?

  1. journalctl-b --since=”2014-10-2416:38”

Even if you are sorry for Binary logs, it is still a useful feature and it is much easier for many system administrators to build similar filters than to write regular expressions.

We can query logs accurately based on specific time. Can we use specific programs? For a unit, try this:

  1. journalctl-u gdm.service

(Note: This is a good way to view the logs generated by X server .) According to the specific process number?

  1. journalctl _PID=890

You can even request to view only the messages generated by an executable file:

  1. journalctl/usr/bin/pulseaudio

If you want to limit the output message to a certain priority, you can use the-p option. If this parameter is set to 0, only urgent messages will be displayed (that is, it is time to pray to $ DEITY) (LCTT Note: $ DEITY is a computer humor, DEITY refers to the "God" in a broad sense, and $ prefix indicates that this is a variable. If it is 7, all messages, including debugging messages, will be displayed. Please refer to the Manual (man journalctl) Get

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.