systemd configuration in linux

systemd configuration

Systemd recommended management process, compared to the use supervisord systemd provides system-level support.

First, the system management

Systemd is not a command, but a set of commands, involving all aspects of system administration.

Service type in / usr / lib / systemd / system /

centos7 default/usr/lib/systemd/system/

The default is ubuntu 18/lib/systemd/system/

  • systemd startup services are stored in this directory, different service types have different file extensions. Therefore, the type of service can be distinguished by file extension
.service General Type of Service. Mainly: system services, including local services, and network services required for the server itself, etc., most often used to this type of service. It is also the most common type
.socket Internal services data exchange service socket: The main IPC is to transfer information socket file function. This type of service is generally socket file monitoring transmission of information, when information is transmitted to the service via this link to the socket file, then it is transmitted according to the state of the user's request to the corresponding daemon, if the daemon does not start, start the daemon then transmits the user's type of service required sokcet generally less frequently used, therefore usually be a slight delay at boot time to start (and therefore not very common). More generally used for local services, such as many of our software is a graphical interface to operate the program data exchange via socket (which is earlier in this super daemon xinted similar portion)
.target Execution environment types: In fact, a group of collection unit, such as a collection of multi-user.target is actually a bunch of services. That, in fact, choose to perform multi-user.target perform a bunch of other .service or (and) .socket services such
.mount .automount File system mount-related services: For example, automatically mount from the network, NFS file system mount file systems such as highly correlated with process management
.path Detecting a specific type of file or directory: Some services need to detect specific directories from the queue to provide services, such as the most common printing service is to start printing by detecting the print queue directory, which is on the type of service required to support .path
.timer Service loop execution: This service is somewhat similar to anacrontab, but the initiative is provided by systemd, more flexible than anacrontab

Other profiles

  • / Etc / sysconfig / directory:Almost all the services will be initialized some options settings are written to this directory. For example: mandb to be updated man page index, parameters need to be added to writes mandb under which this directory. Set network services is written in the / etc / sysconfig / network-scripts / directory within this
  • / Var / lib / directory:Some services will incur data writes its data into / var / lib / directory. For example: MariaDB database management system database is written by default / var / lib / mysql / directory
  • / Run / directory:We placed a lot of cache daemon, including the lock file and PID files

1.1 systemctl

systemctlIt is the main command Systemd for management systems.

# Reboot the system
$ sudo systemctl reboot

 # Turn off the system, cut off the power
$ sudo systemctl poweroff

 # CPU stops working
$ sudo systemctl halt

 # Pause system
$ sudo systemctl suspend

 # Put the system into hibernation
$ sudo systemctl hibernate

 # Put the system into hibernation Interactive
$ sudo systemctl hybrid-sleep

 # Boot into rescue status (single-user state)
$ sudo systemctl rescue

1.2 systemd-analyze

systemd-analyzeCommand is used to view the start time consuming.

# View start consuming
$ systemd-analyze                                                                                       

 # Check each service start time-consuming
$ systemd-analyze blame

 # Show startup process stream waterfall
$ systemd-analyze critical-chain

 # Display start specified service stream
$ systemd-analyze critical-chain atd.service

1.3 hostnamectl

hostnamectlCommand is used to view information about the current host.

# Display information about the current host
$ hostnamectl

 # Set the host name.
$ sudo hostnamectl set-hostname rhel7

1.4 localectl

localectlCommand for viewing locale.

# View locale
$ localectl

 # Set the localization parameters.
$ sudo localectl set-locale LANG=en_GB.utf8
$ sudo localectl set-keymap en_GB

1.5 timedatectl

timedatectlCommand is used to view the current time zone setting.

# View the current time zone settings
$ timedatectl

 # Displays all available time zones
$ timedatectl list-timezones                                                                                   

 # Set the current time zone
$ sudo timedatectl set-timezone America/New_York
$ sudo timedatectl set-time YYYY-MM-DD
$ sudo timedatectl set-time HH:MM:SS

1.6 loginctl

loginctlCommand is used to view the user currently logged on.

# List the current session
$ loginctl list-sessions

 # Lists the currently logged on user
$ loginctl list-users

 # List displays information about a user's
$ loginctl show-user ruanyf

Two, Unit

2.1 Meaning

Systemd can manage all system resources. Different resources collectively referred to as Unit (unit).

Unit is divided into a total of 12 kinds.

  • Service unit: System Services
  • Target unit: Unit consisting of a plurality of
  • Device Unit: hardware
  • Mount point of the file system: Mount Unit
  • Automount Unit: automatic mount point
  • Path Unit: file or path
  • Scope Unit: Systemd not initiated by an external process
  • Slice Unit: Process Group
  • Snapshot Unit: Systemd snapshot, you can cut back to a snapshot
  • Socket Unit: interprocess communication socket
  • Swap Unit: swap file
  • Timer Unit: Timer

systemctl list-unitsCommand Unit can view all current system.

# List Unit running
$ systemctl list-units

 # List all the Unit, including the configuration file is not found or fails to start
$ systemctl list-units --all

 # List all Unit is not running
$ systemctl list-units --all --state=inactive

 # List all failed to load the Unit
$ systemctl list-units --failed

 # List all running, type service of Unit
$ systemctl list-units --type=service

2.2 Unit status

systemctl statusCommand to view the system status and the status of the individual Unit.

# Display system status
$ systemctl status

 # Display the status of the individual Unit
$ sysystemctl status bluetooth.service

 Unit # displays the status of a remote host
$ systemctl -H centos@k8smaster status httpd.service

Unit Management

For users, the most commonly used is the following commands to start and stop the Unit (main service).

# Start a service immediately
$ sudo systemctl start apache.service

 # Stop a service immediately
$ sudo systemctl stop apache.service

 # Restart a service
$ sudo systemctl restart apache.service

 # Kill all child processes a service
$ sudo systemctl kill apache.service

 # Reload a service profile
$ sudo systemctl reload apache.service

 # Override all modified configuration files
$ sudo systemctl daemon-reload

 # Display all the underlying parameters of a Unit
$ systemctl show httpd.service

 # Value of the specified property of a Unit
$ systemctl show -p CPUShares httpd.service

 # Set the properties of a Unit
$ sudo systemctl set-property httpd.service CPUShares=500

Dependencies

There is a dependency between the Unit: A depends on B, it means that when you start Systemd A, but will start to B.

systemctl list-dependenciesCommand lists all the dependencies of a Unit.

$ systemctl list-dependencies nginx.service

State profiles

systemctl list-unit-filesCommand lists all profiles.

# List all profiles
$ systemctl list-unit-files

 # List specified types of configuration files
$ systemctl list-unit-files --type=service

This command will output a list.

$ systemctl list-unit-files

UNIT FILE              STATE
chronyd.service        enabled
[email protected]         static
[email protected]     disabled

This list shows the status of each profile, a total of four.

  • enabled: Start link has been established
  • disabled: No link established start
  • static: This profile is not[Install]Section (not executed), only as dependent on other profiles
  • masked: The configuration file is prohibited to establish links start

Once modify the configuration file, you should let SystemD reload the configuration file, then restart, otherwise the changes will not take effect.

$ sudo systemctl daemon-reload
$ sudo systemctl restart httpd.service

Configuration file is an ordinary text file that can be opened with a text editor.

systemctl catCommand to view the contents of the configuration file.

$ systemctl cat atd.service

[Unit]
Description=ATD daemon

[Service]
Type=forking
ExecStart=/usr/bin/atd

[Install]
WantedBy=multi-user.target

Block profile

[Unit]Usually the first block block the configuration file, the metadata defined for Unit, and the arrangement relationship with other Unit. Its main fields are as follows.

  • Description: Short description
  • Documentation: Document Address
  • Requires: Current Unit Unit dependent other, if they are not running, the current Unit fail to start
  • Wants: Other Unit Unit with the current, if they are not running, the current failure Unit will not start
  • BindsTo:versusRequiresSimilarly, it specifies Unit If you quit, it will cause the current Unit stop running
  • Before: If the field specified Unit should start, you must start after the current Unit
  • After: If the field specified Unit should start, you must start before the current Unit
  • Conflicts: Unit specified here can not run simultaneously with the current Unit
  • Condition...: Unit is currently running conditions must be met, or will not run
  • Assert...: Unit is currently running conditions must be met, otherwise it will fail to start newspaper

[Install]Usually the last block of the configuration file used to define how to start, and whether the boot. Its main fields are as follows.

  • WantedBy: It is the value of one or more Target, the current Unit activation (enable) the symbolic link will be placed/etc/systemd/systemBelow Target directory name +.wantsSubdirectory suffix consisting of
  • RequiredBy: It is the value of one or more Target, the current Unit activated, the symbolic link will be placed/etc/systemd/systemBelow Target directory name +.requiredSubdirectory suffix consisting of
  • Alias: Current Unit can be used to start an alias
  • Also: Current Unit when activated (enable), will be activated at the same time other Unit

[Service]Block used to configure Service Only Service Unit have this type of block. Its main fields are as follows.

  • Type: Define the start of the process behavior. It has the following values.
  • Type=simple: Default, executionExecStartSpecified command to start the main process
  • Type=forking: Create a way to fork child process from the parent process, created after the parent process exits immediately
  • Type=oneshot: A one-time process, Systemd will wait for the current service exit, and then continue down
  • Type=dbus: Current service via D-Bus start
  • Type=notify: Currently the services are started, informsSystemdAnd then continue down
  • Type=idle: If there are other task is finished, the current service will not run
  • ExecStart: Start current service command
  • ExecStartPre: Command executed before the start of the current service
  • ExecStartPost: Command executed after starting the current service
  • ExecReload: Command to execute when the current service restart
  • ExecStop: Command to execute when to stop the current service
  • ExecStopPost: After the stop command execution of its services
  • RestartSec: Automatic restart seconds of the current service interval
  • Restart: Define what circumstances Systemd will automatically restart the current service, including the possible valuesalways(Always restart),on-successon-failureon-abnormalon-aborton-watchdog
  • TimeoutSec: Definition of Systemd wait before stopping the current service in seconds
  • Environment: Specifies the environment variables

Unit configuration file of the complete field list, please refer toOfficial Documents

Target

When you start your computer, you need to start a lot of Unit. If every time you start, must all be stated in this Unit need to start which is obviously very inconvenient. Systemd solution is Target.

Simply put, Target Unit is a group that contains many relevant Unit. When I start a Target, Systemd all starts inside the Unit. In this sense, the concept is similar to Target "status points" to start a Target like to start some kind of state.

traditionalinitInside startup mode, the concept RunLevel, with the role Target is very similar. The difference is, RunLevel are mutually exclusive, it is impossible RunLevel multiple simultaneous start, but more Target can be started simultaneously.

# View the current system of all Target
$ systemctl list-unit-files --type=target

 Unit # View all contained in a Target
$ systemctl list-dependencies multi-user.target

 The default Target start at # View
$ systemctl get-default

 # Set the default Target at startup
$ sudo systemctl set-default multi-user.target

 # When switching Target, the default process does not shut down before the start of a Target,
 # Systemctl isolate command to change this behavior,
 # Closed before a Target which after all does not belong to a Target process
$ sudo systemctl isolate multi-user.target

Log Management

Systemd unified management of all Unit startup log. Benefits is that you can only usejournalctlA command to see all log (kernel log and application log). Log configuration file is/etc/systemd/journald.conf

journalctlPowerful, use very much.

# View all logs (by default, save only the start of this journal)
$ sudo journalctl

 # Display the kernel log (not displaying application logs)
$ sudo journalctl -k

 # View Log The boot system
$ sudo journalctl -b
$ sudo journalctl -b -0

 # View the logs once started (need to change settings)
$ sudo journalctl -b -1

 # View the specified time log
$ sudo journalctl --since="2012-10-30 18:17:16"
$ sudo journalctl --since "20 min ago"
$ sudo journalctl --since yesterday
$ sudo journalctl --since "2015-01-10" --until "2015-01-11 03:00"
$ sudo journalctl --since 09:00 --until "1 hour ago"

 # Show the last 10 lines of the log tail
$ sudo journalctl -n

 # Specified number of rows to display the log tail
$ sudo journalctl -n 20

 # Real-time scrolling display the latest log
$ sudo journalctl -f

 # View the specified service log
$ sudo journalctl /usr/lib/systemd/systemd

 # Display the log process
$ sudo journalctl _PID=1

 # View the path of a script log
$ sudo journalctl /usr/bin/bash

 # Display the user's log
$ sudo journalctl _UID=33 --since today

 # View a log of Unit
$ sudo journalctl -u nginx.service
$ sudo journalctl -u nginx.service --since today

 # Latest real-time scrolling display a log of Unit
$ sudo journalctl -u nginx.service -f

 Unit # merge multiple log display
$ journalctl -u nginx.service -u php-fpm.service --since today

 # Display priority (and above level) of the log, a total of eight
# 0: emerg
# 1: alert
# 2: crit
# 3: err
# 4: warning
# 5: notice
# 6: info
# 7: debug
$ sudo journalctl -p err -b

 # Default paging log output, - no-pager instead of the normal standard output
$ sudo journalctl --no-pager

 # JSON format (single) output
$ sudo journalctl -b -u nginx.service -o json

 # JSON format (multi-line) output, better readability
$ sudo journalctl -b -u nginx.serviceqq
 -o json-pretty

 # Show disk space occupied by the log
$ sudo journalctl --disk-usage

 # Specify the maximum space occupied by the log file
$ sudo journalctl --vacuum-size=1G

 How long will specify a log file #
$ sudo journalctl --vacuum-time=1years

Quote:

https://www.freedesktop.org/software/systemd/man/systemd.unit.html

http://www.jinbuguo.com/systemd/systemctl.html

Intelligent Recommendation

Linux from scratch (LFS11.0) Build LFS system - SystemD use and configuration

Basic Settings The /etc/systemd/system.conf file contains a set of options for controlling the basic features of the SystemD. All entries in the default file are commented and indicate the default. Yo...

[Linux] [Systemd] Linux integrated SystemD service

Integrate SystemD service, use the SystemCTL instruction to start and stop service and view. step: Write the .Service file. Place the service file in/usr/lib/systemd/system or/etc/systemd directory Sy...

Systemd configuration and use

Brief introduction Systemd is controlled by managing unit to control the function of booting services, booting levels. In the/usr/lib/systemd/system directory, various unit files are included, the ser...

Linux service Systemd introduction

Systemctl common command introduction Systemctl is a systemd tool that is primarily responsible for controlling the systemd system and service manager. Systemd is a collection of system management dae...

More Recommendation

Into the Linux systemd startup process

Go to The way Linux systems are started is a bit complicated, and there is always a need for optimization. The traditional Linux system boot process is mainly handled by the well-known init process (a...

Systemd for Linux systems

Zigong asked for benevolence. Zi Yan: "If a worker wants to do something good, he must first sharpen his weapon. He is a state, a sage of his doctor, and a benevolent friend."Confucius(Sprin...

On the resources to control Linux systemd

On the resources to control Linux systemd This article documents a cgroup subdirectory loss problem, and simply explore the resource control mechanism of Linux systemd. Symptom We hope to pull through...

Frog learning Linux-systemd

1, the alternative systemd init In the latest version of CentOS CentOS 7 series, we have begun to use alternative systemd init system management as a service. The first process in 7 CentOS PID1 from i...

linux systemd timer

Systemd as a Linux system start-up, powerful. First, the regular tasks The so-called regular tasks, is the future of one or more time points, scheduled tasks to be performed, such as e-mail received o...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top