Difference between revisions of "Linux systemd"
Line 105: | Line 105: | ||
# systemctl unmask tmp.mount | # systemctl unmask tmp.mount | ||
== Control and Manage Sockets using | == Control and Manage Sockets using <code>systemctl</code> == | ||
List all available system sockets | List all available system sockets | ||
# systemctl list-unit-files --type=socket | # systemctl list-unit-files --type=socket | ||
Line 125: | Line 125: | ||
# systemctl unmask cups.socket | # systemctl unmask cups.socket | ||
... | == CPU Utilization (Shares) of a Service == | ||
<source lang=bash> | |||
systemctl show -p CPUShares httpd.service | |||
CPUShares=1024 #<- it's default value | |||
systemctl set-property httpd.service CPUShares=1000 #set a CPUShare | |||
</source> | |||
When you set CPUShare for a service, a directory with the name of service is created (httpd.service.d) which contains a file 90-CPUShares.conf which contains the CPUShare Limit information. | |||
== Check all the configuration details of a service == | |||
<source lang=bash> | |||
systemctl show httpd | |||
</source> | |||
== Analyze critical chain for a services == | |||
<source lang=bash> | |||
systemd-analyze critical-chain NetworkManager.service | |||
The time after the unit is active or started is printed after the "@" character. | |||
The time the unit takes to start is printed after the "+" character. | |||
NetworkManager.service +215ms | |||
└─dbus.service @4.264s | |||
└─basic.target @4.246s | |||
└─sockets.target @4.246s | |||
└─snapd.socket @4.243s +2ms | |||
└─sysinit.target @4.242s | |||
└─apparmor.service @1.437s +2.079s | |||
└─local-fs.target @1.432s | |||
└─run-user-1001-gvfs.mount @29.537s | |||
└─run-user-1001.mount @21.950s | |||
└─swap.target @1.356s | |||
└─dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap @1.345s +10ms | |||
└─dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.device @1.344s | |||
</source> | |||
== Get a list of dependencies for a service == | |||
<source lang=bash> | |||
systemctl list-dependencies NetworkManager.service | |||
● ├─dbus.socket | |||
● ├─system.slice | |||
● ├─network.target | |||
● └─sysinit.target | |||
● ├─apparmor.service | |||
● ├─blk-availability.service | |||
● ├─dev-hugepages.mount | |||
● ├─dev-mqueue.mount | |||
● ├─keyboard-setup.service | |||
● ├─kmod-static-nodes.service | |||
● ├─lvm2-lvmetad.socket | |||
● ├─lvm2-lvmpolld.socket | |||
... | |||
</source> | |||
== List control groups <code>-cgls</code> hierarchically == | |||
<source lang=bash> | |||
$ systemd-cgls | |||
Control group /: | |||
-.slice | |||
├─user.slice | |||
│ ├─user-1001.slice | |||
│ │ ├─session-2.scope | |||
│ │ │ ├─ 1882 gdm-session-worker [pam/gdm-password] | |||
│ │ │ ├─ 2451 /usr/bin/gnome-keyring-daemon --daemonize --login | |||
│ │ │ ├─ 2455 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu | |||
│ │ │ ├─ 2457 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1001/gdm/Xauthority -background none -noreset -keeptty -verbose 3 | |||
│ │ │ ├─ 2513 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu | |||
│ │ │ ├─ 2592 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu | |||
│ │ │ ├─ 2618 /usr/bin/gnome-shell | |||
│ │ │ ├─ 2640 /usr/bin/pulseaudio --start --log-target=syslog | |||
... | |||
</source> | |||
== List control group <code>-cgtop</code> - CPU, memory, Input and Output sorted by == | |||
<source lang=bash> | |||
systemd-cgtop | |||
Control Group Tasks %CPU Memory Input/s Output/s | |||
/ 1204 98.4 10.4G - - | |||
/system.slice 118 - - - - | |||
/system…anager.service 3 - - - - | |||
/system…anager.service 4 - - - - | |||
/system…daemon.service 3 - - - - | |||
</source> | |||
== Start rescue mode == | |||
<source lang=bash> | |||
$ systemctl rescue | |||
Broadcast message from root@tecmint on pts/0 (Wed 2019-07-26 11:31:18 BST): | |||
The system is going down to rescue mode NOW! | |||
</source> | |||
== Enter into emergency mode == | |||
<source lang=bash> | |||
$ 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 into default mode | |||
<source lang=bash> | |||
</source> | |||
== List current run levels in use == | |||
<source lang=bash> | |||
$ systemctl get-default | |||
graphical.target | |||
#Runlevel 0 : Shut down and Power off the system. | |||
#Runlevel 1 : Rescue?Maintainance Mode. | |||
#Runlevel 3 : multiuser, no-graphic system. | |||
#Runlevel 4 : multiuser, no-graphic system. | |||
#Runlevel 5 : multiuser, graphical system. | |||
#Runlevel 6 : Shutdown and Reboot the machine. | |||
</source> | |||
== How to start Runlevel 5 aka graphical mode == | |||
<source lang=bash> | |||
$ systemctl isolate runlevel5.target #/or | |||
$ systemctl isolate graphical.target | |||
</source> | |||
== Set default Runlevel == | |||
<source lang=bash> | |||
# systemctl set-default runlevel3.target #multiuser | |||
# systemctl set-default runlevel5.target #graphical | |||
</source> | |||
== Reboot, suspend, sleep == | |||
<source lang=bash> | |||
systemctl reboot | halt | suspend | hibernate | hybrid-sleep | |||
</source> | |||
= Special services = | = Special services = |
Revision as of 15:32, 27 July 2019
Manage Systemd Services and Units using Systemctl
Systemctl is a systemd utility which is responsible for Controlling the systemd system and service manager.
Systemd is a collection of system management daemons, utilities and libraries which serves as a replacement of System V init daemon. Systemd functions as central management and configuration platform for UNIX like system.
In the Linux Ecosystem Systemd has been implemented on most of the standard Linux Distribution with a few exception. Systemd is the parent Process of all other daemons oftenly but not always.
Check versions, binaries and libraries
$ systemd --version #Ubuntu 18.04 systemd 237 +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid $ whereis systemd systemd: /usr/lib/systemd /bin/systemd /etc/systemd /lib/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz $ whereis systemctl systemctl: /bin/systemctl /usr/share/man/man1/systemctl.1.gz
Is systemd running?
ps -eaf | grep [s]ystemd
Analyze
Analyze systemd boot process
# systemd-analyze
Analyze time taken by each process at boot
# systemd-analyze blame
Analyze critical chain at boot
# systemd-analyze critical-chain
Important: Systemctl accepts services (.service), mount point (.mount), sockets (.socket) and devices (.device) as units
List units
systemctl list-unit-files #List all the available units systemctl list-units #List all running units systemctl --failed #List all failed units systemctl is-enabled crond.service #Check if a Unit (cron.service) is enabled systemctl status firewalld.service #Check whether a Unit or Service is running
Control and Manage Services Using Systemctl
List all services (including enabled and disabled)
systemctl list-unit-files --type=service
How do I start, restart, stop, reload and check the status of a service (httpd.service) in Linux
systemctl start httpd.service systemctl restart httpd.service systemctl stop httpd.service systemctl reload httpd.service systemctl status httpd.service
Note: When we use commands like start, restart, stop and reload with systemctl, we will not get any output on the terminal, only status command will print the output
How to active a service and enable or disable a service at boot time (auto start service at system boot)
systemctl is-active httpd.service systemctl enable httpd.service systemctl disable httpd.service
How to mask (making it impossible to start) or unmask a service (httpd.service)
systemctl mask httpd.service systemctl unmask httpd.service
How to a Kill a service using systemctl command
systemctl kill httpd systemctl status httpd
Control and Manage Mount Points using Systemctl
List all system mount points
# systemctl list-unit-files --type=mount
How do I mount, unmount, remount, reload system mount points and also check the status of mount points on the system
# systemctl start tmp.mount # systemctl stop tmp.mount # systemctl restart tmp.mount # systemctl reload tmp.mount # systemctl status tmp.mount
How to active, enable or disable a mount point at boot time (auto mount at system boot)
# systemctl is-active tmp.mount # systemctl enable tmp.mount # systemctl disable tmp.mount
How to mask (making it impossible to start) or unmask a mount points in Linux
# systemctl mask tmp.mount # systemctl unmask tmp.mount
Control and Manage Sockets using systemctl
List all available system sockets
# systemctl list-unit-files --type=socket
How do I start, restart, stop, reload and check the status of a socket (example: cups.socket) in Linux
# systemctl start cups.socket # systemctl restart cups.socket # systemctl stop cups.socket # systemctl reload cups.socket # systemctl status cups.socket
How to active a socket and enable or disable at boot time (auto start socket at system boot)
# systemctl is-active cups.socket # systemctl enable cups.socket # systemctl disable cups.socket
How to mask (making it impossible to start) or unmask a socket (cups.socket)
# systemctl mask cups.socket # systemctl unmask cups.socket
systemctl show -p CPUShares httpd.service CPUShares=1024 #<- it's default value systemctl set-property httpd.service CPUShares=1000 #set a CPUShare
When you set CPUShare for a service, a directory with the name of service is created (httpd.service.d) which contains a file 90-CPUShares.conf which contains the CPUShare Limit information.
Check all the configuration details of a service
systemctl show httpd
Analyze critical chain for a services
systemd-analyze critical-chain NetworkManager.service The time after the unit is active or started is printed after the "@" character. The time the unit takes to start is printed after the "+" character. NetworkManager.service +215ms └─dbus.service @4.264s └─basic.target @4.246s └─sockets.target @4.246s └─snapd.socket @4.243s +2ms └─sysinit.target @4.242s └─apparmor.service @1.437s +2.079s └─local-fs.target @1.432s └─run-user-1001-gvfs.mount @29.537s └─run-user-1001.mount @21.950s └─swap.target @1.356s └─dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap @1.345s +10ms └─dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.device @1.344s
Get a list of dependencies for a service
systemctl list-dependencies NetworkManager.service ● ├─dbus.socket ● ├─system.slice ● ├─network.target ● └─sysinit.target ● ├─apparmor.service ● ├─blk-availability.service ● ├─dev-hugepages.mount ● ├─dev-mqueue.mount ● ├─keyboard-setup.service ● ├─kmod-static-nodes.service ● ├─lvm2-lvmetad.socket ● ├─lvm2-lvmpolld.socket ...
List control groups -cgls
hierarchically
$ systemd-cgls Control group /: -.slice ├─user.slice │ ├─user-1001.slice │ │ ├─session-2.scope │ │ │ ├─ 1882 gdm-session-worker [pam/gdm-password] │ │ │ ├─ 2451 /usr/bin/gnome-keyring-daemon --daemonize --login │ │ │ ├─ 2455 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu │ │ │ ├─ 2457 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1001/gdm/Xauthority -background none -noreset -keeptty -verbose 3 │ │ │ ├─ 2513 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu │ │ │ ├─ 2592 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu │ │ │ ├─ 2618 /usr/bin/gnome-shell │ │ │ ├─ 2640 /usr/bin/pulseaudio --start --log-target=syslog ...
List control group -cgtop
- CPU, memory, Input and Output sorted by
systemd-cgtop Control Group Tasks %CPU Memory Input/s Output/s / 1204 98.4 10.4G - - /system.slice 118 - - - - /system…anager.service 3 - - - - /system…anager.service 4 - - - - /system…daemon.service 3 - - - -
Start rescue mode
$ systemctl rescue Broadcast message from root@tecmint on pts/0 (Wed 2019-07-26 11:31:18 BST): The system is going down to rescue mode NOW!
Enter into 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 into default mode <source lang=bash>
List current run levels in use
$ systemctl get-default graphical.target #Runlevel 0 : Shut down and Power off the system. #Runlevel 1 : Rescue?Maintainance Mode. #Runlevel 3 : multiuser, no-graphic system. #Runlevel 4 : multiuser, no-graphic system. #Runlevel 5 : multiuser, graphical system. #Runlevel 6 : Shutdown and Reboot the machine.
How to start Runlevel 5 aka graphical mode
$ systemctl isolate runlevel5.target #/or $ systemctl isolate graphical.target
Set default Runlevel
# systemctl set-default runlevel3.target #multiuser # systemctl set-default runlevel5.target #graphical
Reboot, suspend, sleep
systemctl reboot | halt | suspend | hibernate | hybrid-sleep
Special services
# Restart networking systemctl restart systemd-networkd