SYSTEMCTL Command Complete Guide

Source: Internet
Author: User

Systemctl is a systemd tool that is primarily responsible for controlling SYSTEMD systems and service managers.

Systemd is a collection of systems management daemons, tools, and libraries that supersede the System V initial process. The SYSTEMD feature is for centralized management and configuration of Unix-like systems.

In the Linux ecosystem, SYSTEMD is deployed in most of the standard Linux distributions, with only a few distributions yet to be deployed. SYSTEMD is usually the parent process of all other daemons, but not always.

Managing Linux Services with Systemctl

This article is intended to illustrate "How to control systems and services" on systems running SYSTEMD.


SYSTEMD first experience and Systemctl Foundation


1. First check if the SYSTEMD is installed on your system and determine which version is currently installed

# SYSTEMD--version
SYSTEMD 215
+pam +audit +selinux +ima +sysvinit +libcryptsetup +gcrypt +acl +xz-seccomp-apparmor

It is clear from the example above that we have installed the 215 version of SYSTEMD.


2. Check the installation location of binaries and library files for Systemd and Systemctl

# Whereis Systemd
Systemd:/usr/lib/systemd/etc/systemd/usr/share/systemd/usr/share/man/man1/systemd.1.gz
# Whereis Systemctl
Systemctl:/usr/bin/systemctl/usr/share/man/man1/systemctl.1.gz


3. Check if the SYSTEMD is running

# Ps-eaf | grep [S]ystemd
Root 10016:27?00:00:00/usr/lib/systemd/systemd--switched-root--system--deserialize 23
Root 4441016:27?00:00:00/usr/lib/systemd/systemd-journald
Root 4691016:27?00:00:00/USR/LIB/SYSTEMD/SYSTEMD-UDEVD
Root 5551016:27?00:00:00/usr/lib/systemd/systemd-logind
Dbus 5561016:27?00:00:00/bin/dbus-daemon--system--address=systemd:--nofork--nopidfile--systemd-activation

Note: SYSTEMD is run as a parent process (pid=1). In the PS command output with the (-e) parameter above, select All processes, (-a) Select all processes except the leading of the session, and use the (-f) parameter to output a complete list of formats (that is,-eaf).

Also note the brackets and the remainder of the example in the previous example. The square bracket expression is part of the character class expression of grep.


4. Analyzing the SYSTEMD startup process

# Systemd-analyze
Startup finished in487ms (kernel) +2.776s (INITRD) +20.229s (userspace) =23.493s


5. Analyze the time spent by each process at startup

# Systemd-analyze Blame
8.565s Mariadb.service
7.991s Webmin.service
6.095s Postfix.service
4.311s Httpd.service
3.926s Firewalld.service
3.780s Kdump.service
3.238s Tuned.service
1.712s Network.service
1.394s Lvm2-monitor.service
1.126s Systemd-logind.service
....


6. Analyze the critical chain at startup

# Systemd-analyze Critical-chain
The time after the unit is active or started are printed after the "@" character.
The time the unit takes to start was printed after the "+" character.
Multi-user.target @20.222s
└─mariadb.service @11.657s+8.565s
└─network.target @11.168s
└─network.service @9.456s+1.712s
└─networkmanager.service @8.858s+596ms
└─firewalld.service @4.931s+3.926s
└─basic.target @4.916s
└─sockets.target @4.916s
└─dbus.socket @4.916s
└─sysinit.target @4.905s
└─systemd-update-utmp.service @4.864s+39ms
└─auditd.service @4.563s+301ms
└─systemd-tmpfiles-setup.service @4.485s+69ms
└─rhel-import-state.service @4.342s+142ms
└─local-fs.target @4.324s
└─boot.mount @4.286s+31ms
└─[email protected]\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\x2d85d19608096
└─dev-disk-by\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\[email protected]

Important: Systemctl accepts a service (. Service), Mount Point (. mount), socket interface (. socket), and device (. device) as a unit.


7. List all available units

# Systemctl List-unit-files
UNIT FILE State
Proc-sys-fs-binfmt_misc.automount Static
Dev-hugepages.mount Static
Dev-mqueue.mount Static
Proc-sys-fs-binfmt_misc.mount Static
Sys-fs-fuse-connections.mount Static
Sys-kernel-config.mount Static
Sys-kernel-debug.mount Static
Tmp.mount disabled
Brandbot.path disabled
.....


8. List all running units

# Systemctl List-units
UNIT LOAD ACTIVE SUB DESCRIPTION
Proc-sys-fs-binfmt_misc.automount Loaded Active Waiting Arbitraryexecutablefileformatsfilesyste
Sys-devices-pc ... 0-1:0:0:0-block-sr0.device Loaded Active plugged Vbox_cd-rom
Sys-devices-pc...:00:03.0-net-enp0s3.device loaded active plugged pro/1000 MT desktopadapter
Sys-devices-pc ... 00:05.0-sound-card0.device loaded active plugged 82801AA AC ' $ Audio Controller
Sys-devices-pc...:0:0-block-sda-sda1.device Loaded Active plugged Vbox_harddisk
Sys-devices-pc...:0:0-block-sda-sda2.device loaded active plugged LVM PV qzyo3l-qyal-urua-cjuk-pljo-qktx-vgbq8
Sys-devices-pc ... 0-2:0:0:0-block-sda.device Loaded Active plugged Vbox_harddisk
Sys-devices-pl ... Erial8250-tty-ttys0.device Loaded Active PLUGGED/SYS/DEVICES/PLATFORM/SERIAL8250/TTY/TTYS0
Sys-devices-pl ... Erial8250-tty-ttys1.device Loaded Active plugged/sys/devices/platform/serial8250/tty/ttys1
Sys-devices-pl ... Erial8250-tty-ttys2.device Loaded Active Plugged/sys/devices/platform/serial8250/tty/ttys2
Sys-devices-pl ... Erial8250-tty-ttys3.device Loaded Active PLUGGED/SYS/DEVICES/PLATFORM/SERIAL8250/TTY/TTYS3
Sys-devices-virtual-block-dm\x2d0.device Loaded Active plugged/sys/devices/virtual/block/dm-0
Sys-devices-virtual-block-dm\x2d1.device Loaded Active plugged/sys/devices/virtual/block/dm-1
Sys-module-configfs.device Loaded Active Plugged/sys/module/configfs
...


9. List all failed units

# Systemctl--failed
UNIT LOAD ACTIVE SUB DESCRIPTION
Kdump.service loaded failed failed Crash recovery kernel arming
LOAD =reflects Whether the unit definition was properly loaded.
ACTIVE =the High-level unit activation state, i.e. generalization of SUB.
SUB =the Low-level Unit activation state, values depend on unit type.
1 loaded units listed. Pass--all to see loaded but inactive units, too.
To show all installed unit files use ' Systemctl list-unit-files '.


10. Check if a cell (such as Cron.service) is enabled

# Systemctl is-enabled Crond.service
Enabled


11. Check if a cell or service is running

# SYSTEMCTL Status Firewalld.service
firewalld.service-firewalld-dynamic Firewall daemon
Loaded:loaded (/usr/lib/systemd/system/firewalld.service; enabled)
Active:active (running) since tue2015-04-2816:27:55 ist;34min ago
Main pid:549 (FIREWALLD)
Cgroup:/system.slice/firewalld.service
└─549/usr/bin/python-es/usr/sbin/firewalld--nofork--nopid
apr2816:27:51 tecmint systemd[1]:starting firewalld-dynamic Firewall daemon ...
apr2816:27:55 tecmint systemd[1]:started firewalld-dynamic Firewall daemon.


Using Systemctl to control and manage services


12. List all services (both enabled and disabled)

# Systemctl List-unit-files--type=service
UNIT FILE State
Arp-ethers.service disabled
Auditd.service enabled
[Email protected] disabled
Blk-availability.service disabled
Brandbot.service Static
Collectd.service disabled
Console-getty.service disabled
Console-shell.service disabled
Cpupower.service disabled
Crond.service enabled
Dbus-org. Fedoraproject.FirewallD1.service enabled
....


How to start, restart, stop, reload, and check the status of services (such as Httpd.service) in Linux

# Systemctl Start Httpd.service
# systemctl Restart Httpd.service
# Systemctl Stop Httpd.service
# Systemctl Reload Httpd.service
# SYSTEMCTL Status Httpd.service
Httpd.service-theapache HTTP Server
Loaded:loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active:active (running) since tue2015-04-2817:21:30 ist;6s ago
Process:2876execstop=/bin/kill-winch ${mainpid} (code=exited, status=0/success)
Main pid:2881 (httpd)
Status: "Processing requests ..."
Cgroup:/system.slice/httpd.service
├─2881/usr/sbin/httpd-dforeground
├─2884/usr/sbin/httpd-dforeground
├─2885/usr/sbin/httpd-dforeground
├─2886/usr/sbin/httpd-dforeground
├─2887/usr/sbin/httpd-dforeground
└─2888/usr/sbin/httpd-dforeground
apr2817:21:30 tecmint systemd[1]:startingtheapache HTTP Server ...
Apr2817:21:30 tecmint httpd[2881]: AH00558:httpd:Couldnot reliably determine the server ' s fully q...ssage
APR 17:21:30 tecmint systemd[1]: Started the Apache HTTP Server.
Hint:some lines were ellipsized, use-l to show on full.

Note: When we use Systemctl's start,restart,stop and reload commands, we do not get any output from the terminal, only the status command can print the output.


14. How to activate the service and enable or disable the service at startup (that is, start the service automatically when the system starts)

# Systemctl Is-active Httpd.service
# Systemctl Enable Httpd.service
# systemctl Disable Httpd.service


15. How to block (let it not start) or display services (such as Httpd.service)

# Systemctl Mask Httpd.service
Ln-s '/dev/null '/etc/systemd/system/httpd.service '
# Systemctl unmask Httpd.service
RM '/etc/systemd/system/httpd.service '


16. Kill the service using the SYSTEMCTL command

# Systemctl Kill httpd
# SYSTEMCTL Status httpd
Httpd.service-theapache HTTP Server
Loaded:loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active:failed (Result:exit-code) since tue2015-04-2818:01:42 ist;28min ago
Main pid:2881 (code=exited, status=0/success)
Status: "Total requests:0; Current requests/sec:0; Current traffic:0 b/sec "
Apr2817:37:29 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
Apr2817:37:29 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
apr2817:37:39 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
apr2817:37:39 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
apr2817:37:49 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
apr2817:37:49 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
apr2817:37:59 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
apr2817:37:59 tecmint systemd[1]: Httpd.service:Got notification message from the PID 2881, but recepti...bled.
Apr2818:01:42 tecmint systemd[1]: Httpd.service:control process exited, code=exited status=226
apr2818:01:42 Tecmint Systemd[1]:unit Httpd.service entered failed state.
Hint:some lines were ellipsized,use-l to show on full.


Using Systemctl to control and manage mount points


17. List all system mount points

# Systemctl List-unit-files--type=mount
UNIT FILE State
Dev-hugepages.mount Static
Dev-mqueue.mount Static
Proc-sys-fs-binfmt_misc.mount Static
Sys-fs-fuse-connections.mount Static
Sys-kernel-config.mount Static
Sys-kernel-debug.mount Static
Tmp.mount disabled


18. Mount, Unmount, re-mount, reload the system mount point and check the status of the mount point in the system

# Systemctl Start Tmp.mount
# Systemctl Stop Tmp.mount
# systemctl Restart Tmp.mount
# Systemctl Reload Tmp.mount
# SYSTEMCTL Status Tmp.mount
Tmp.mount-temporarydirectory
loaded:loaded (/usr/lib/systemd/system/tmp.mount; disabled)
Active:active (mounted) since tue2015-04-2817:46:06 ist;2min48s ago
Where:/tmp
What:tmpfs
Docs:man:hier (7)
Http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
Process:3908execmount=/bin/mount tmpfs/tmp-t tmpfs-o mode=1777,strictatime (code=exited, Status=0/SUCCESS)
apr2817:46:06 tecmint systemd[1]:mountingtemporarydirectory ...
apr2817:46:06 tecmint systemd[1]: tmp.mount:directory/tmp to mount over isnot empty, mounting anyway.
apr2817:46:06 Tecmint systemd[1]:mountedtemporarydirectory.


19. Activate, enable, or disable mount points at startup (automatically mount when system starts)

# Systemctl Is-active Tmp.mount
# Systemctl Enable Tmp.mount
# systemctl Disable Tmp.mount


20. Block (Let it not enable) or visible mount point in Linux

# Systemctl Mask Tmp.mount
Ln-s '/dev/null '/etc/systemd/system/tmp.mount '
# Systemctl unmask Tmp.mount
RM '/etc/systemd/system/tmp.mount '


Using Systemctl to control and manage socket interfaces


21. List all available system socket interfaces

# Systemctl List-unit-files--type=socket
UNIT FILE State
Dbus.socket Static
Dm-event.socket enabled
Lvm2-lvmetad.socket enabled
Rsyncd.socket disabled
Sshd.socket disabled
Syslog.socket Static
Systemd-initctl.socket Static
Systemd-journald.socket Static
Systemd-shutdownd.socket Static
Systemd-udevd-control.socket Static
Systemd-udevd-kernel.socket Static
Unit files listed.


22. Start, restart, stop, reload, and check the status of the socket in Linux

# Systemctl Start Cups.socket
# systemctl Restart Cups.socket
# Systemctl Stop Cups.socket
# Systemctl Reload Cups.socket
# SYSTEMCTL Status Cups.socket
Cups.socket-cups printingservicesockets
Loaded:loaded (/usr/lib/systemd/system/cups.socket; enabled)
Active:active (listening) since tue2015-04-2818:10:59 ist;8s ago
Listen:/var/run/cups/cups.sock (Stream)
apr2818:10:59 tecmint systemd[1]:starting CUPS printingservicesockets.
apr2818:10:59 tecmint systemd[1]:listening on CUPS printingservicesockets.


23. Activate the socket interface at startup and enable or disable it (self-booting at system startup)

# Systemctl Is-active Cups.socket
# Systemctl Enable Cups.socket
# systemctl Disable Cups.socket


24. Shield (make it unable to start) or display socket interface

# Systemctl Mask Cups.socket
Ln-s '/dev/null '/etc/systemd/system/cups.socket '
# Systemctl unmask Cups.socket
RM '/etc/systemd/system/cups.socket '


CPU utilization (sub-quota) of the service


25. Get CPU credits for a current service (such as httpd)

# systemctl Show-p cpushares Httpd.service
cpushares=1024

Note: The default CPU allocation share for each service = 1024, you can increase/decrease the CPU allocation share of a process.


26. Limit the CPU allocation share for a service (Httpd.service) to cpushares/

# systemctl Set-property Httpd.service cpushares=2000
# systemctl Show-p cpushares Httpd.service
cpushares=2000

Note: When you set cpushares for a service, a directory named after the service name is automatically created (such as Httpd.service), which contains a file called 90-cpushares.conf, which contains cpushare restricted information, you can view the file in the following ways:

# vi/etc/systemd/system/httpd.service.d/90-cpushares.conf
[Service]
cpushares=2000


27. Check all configuration details for a service

# Systemctl Show httpd
Id=httpd.service
Names=httpd.service
Requires=basic.target
Wants=system.slice
Wantedby=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target Multi-user.target
After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice
Description=theapache HTTP Server
loadstate=loaded
Activestate=active
Substate=running
Fragmentpath=/usr/lib/systemd/system/httpd.service
....


28. Analyze the critical chain of a service (HTTPD)

# Systemd-analyze Critical-chain Httpd.service
The time after the unit is active or started are printed after the "@" character.
The time the unit takes to start was printed after the "+" character.
Httpd.service +142ms
└─network.target @11.168s
└─network.service @9.456s+1.712s
└─networkmanager.service @8.858s+596ms
└─firewalld.service @4.931s+3.926s
└─basic.target @4.916s
└─sockets.target @4.916s
└─dbus.socket @4.916s
└─sysinit.target @4.905s
└─systemd-update-utmp.service @4.864s+39ms
└─auditd.service @4.563s+301ms
└─systemd-tmpfiles-setup.service @4.485s+69ms
└─rhel-import-state.service @4.342s+142ms
└─local-fs.target @4.324s
└─boot.mount @4.286s+31ms
└─[email Protected]\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\[email protected]+149ms
└─dev-disk-by\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\[email protected]


29. Get a list of dependencies for a service (HTTPD)

# Systemctl List-dependencies Httpd.service
Httpd.service
├─system.slice
└─basic.target
├─firewalld.service
├─microcode.service
├─rhel-autorelabel-mark.service
├─rhel-autorelabel.service
├─rhel-configure.service
├─rhel-dmesg.service
├─rhel-loadmodules.service
├─paths.target
├─slices.target
│├─-.slice
│└─system.slice
├─sockets.target
│├─dbus.socket
....


30. List control groups by rank

# Systemd-cgls
├─1/usr/lib/systemd/systemd--switched-root--system--deserialize 23
├─user.slice
│└─user-0.slice
│└─session-1.scope
│├─2498 sshd: [Email protected]/0
│├─2500-bash
│├─4521 Systemd-cgls
│└─4522 Systemd-cgls
└─system.slice
├─httpd.service
│├─4440/usr/sbin/httpd-dforeground
│├─4442/usr/sbin/httpd-dforeground
│├─4443/usr/sbin/httpd-dforeground
│├─4444/usr/sbin/httpd-dforeground
│├─4445/usr/sbin/httpd-dforeground
│└─4446/usr/sbin/httpd-dforeground
├─polkit.service
│└─721/USR/LIB/POLKIT-1/POLKITD--no-debug
....


31. List control groups by CPU, memory, input, and output

# Systemd-cgtop
PATHTASKS%CPU memoryinput/s output/s
/831.0437.8m--
/system.slice-0.1---
/system.slice/mariadb.service 20.1---
/system.slice/tuned.service 10.0---
/system.slice/httpd.service 60.0---
/system.slice/networkmanager.service 1----
/system.slice/atop.service 1----
/system.slice/atopacct.service 1----
/system.slice/auditd.service 1----
/system.slice/crond.service 1----
/system.slice/dbus.service 1----
/system.slice/firewalld.service 1----
/system.slice/lvm2-lvmetad.service 1----
/system.slice/polkit.service 1----
/system.slice/postfix.service 3----
/system.slice/rsyslog.service 1----
/system.slice/system-getty.slice/[email protected] 1----
/system.slice/systemd-journald.service 1----
/system.slice/systemd-logind.service 1----
/system.slice/systemd-udevd.service 1----
/system.slice/webmin.service 1----
/USER.SLICE/USER-0.SLICE/SESSION-1.SCOPE3----


Control system operating level


32. Start the System rescue mode

# Systemctl Rescue
Broadcast message from [e-mail protected] on pts/0 (wed2015-04-2911:31:18 IST):
The system is going down to rescue mode now!


33. Enter Emergency mode

# SYSTEMCTL Emergency
Welcome to emergency mode! After logging in, type ' JOURNALCTL-XB ' to view
System logs, "Systemctl reboot" to reboot, "systemctl default" to try again
To boot Intodefault mode.


34. List the currently used run level

# Systemctl Get-default
Multi-user.target


35. Start the operation level 5, that is, the graphics mode

# Systemctl Isolate Runlevel5.target
Or
# Systemctl Isolate Graphical.target


36. Start Run Level 3, which is multi-user mode (command line)

# Systemctl Isolate Runlevel3.target
Or
# Systemctl Isolate Multiuser.target


36. Set the default run level for multi-user mode or graphics mode

# Systemctl Set-default Runlevel3.target
# Systemctl Set-default Runlevel5.target


37. Restart, stop, suspend, hibernate the system or bring the system into mixed sleep

# Systemctl reboot
# Systemctl Halt
# Systemctl Suspend
# Systemctl Hibernate
# Systemctl Hybrid-sleep

For those who do not know what the operating level is, explain the following.

Runlevel 0: Shutting down the system
Runlevel 1: Rescue? Maintenance Mode
Runlevel 3: Multi-user, no graphics system
Runlevel 4: Multi-user, no graphics system
Runlevel 5: Multi-user, graphical system
Runlevel 6: Close and restart the machine


That's it. Stay connected and make comments. Don't forget to give us some valuable feedback in the comments below. Like us, share with us, seek to spread.

SYSTEMCTL Command Complete Guide

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.