Linux systemd and systemctl tutorial

Greeting guys, In this tutorial we will talk about linux systemd and how to use and manage it using systemctl.
We will give brief introduction about systemd and its process id and will find using command pstree. Later in this tutorial we will see how to use systemctl commands to list services, to check whether service is enabled.disabled, started /stopped and will stop, start, enable/disable, restart and reload commands options. Come lets jump in

Systemd

Systemd is system and service manager for Linux OS and run as first process on boot (as PID 1). It initializes the system and manages user space services. Starts processes in parallel whereas init was starting process in serial. That means systemd is faster than init.  For systemd every resource which is known to systemd and know how to operate and manage is unit. This is important object of systemd tool how to deal. Information about these units are stored in configuration files called unit files.

systemd has its own logging system known as journal. It replaces the syslog daemon from sysVinit by using command journalctl.

You can check the all boot messages use  command journalctl –b

To follow the messages system logs use  journalctl –f it is similar as tail –f. (do it yourself)

[root@faraz ~]# journalctl

- Logs begin at Thu 2016-02-25 13:21:59 PKT, end at Thu 2016-02-25 14:07:30 PKT. --
Feb 25 13:21:59 localhost.localdomain systemd-journal[89]: Runtime journal is using 6.2M (max allowed 49.6M, trying to leave 74.4M free of 490.2M available ? current limit 49.6M
Feb 25 13:21:59 localhost.localdomain systemd-journal[89]: Runtime journal is using 6.2M (max allowed 49.6M, trying to leave 74.4M free of 490.2M available ? current limit 49.6M
Feb 25 13:21:59 localhost.localdomain kernel: Initializing cgroup subsys cpuset
Feb 25 13:21:59 localhost.localdomain kernel: Initializing cgroup subsys cpu
Feb 25 13:21:59 localhost.localdomain kernel: Initializing cgroup subsys cpuacct
Feb 25 13:21:59 localhost.localdomain kernel: Linux version 3.10.0-327.el7.x86_64 ([email protected]) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP
Feb 25 13:21:59 localhost.localdomain kernel: BIOS-e820: [mem 0x000000003fff0000-0x000000003fffffff] ACPI data
Feb 25 13:21:59 localhost.localdomain kernel: BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
Feb 25 13:21:59 localhost.localdomain kernel: NX (Execute Disable) protection: active
x00000fff] usable ==> reserved
Feb 25 14:06:05 faraz.test.server systemd[1]: Starting Location Lookup Service...
Feb 25 14:06:05 faraz.test.server dbus[17211]: [system] Successfully activated service 'org.freedesktop.GeoClue2'
Feb 25 14:06:05 faraz.test.server dbus-daemon[17211]: dbus[17211]: [system] Successfully activated service 'org.freedesktop.GeoClue2'
Feb 25 14:06:05 faraz.test.server systemd[1]: Started Location Lookup Service.
Feb 25 14:06:05 faraz.test.server polkitd[17273]: Registered Authentication Agent for unix-session:12 (system bus name :1.62 [/usr/bin/gnome-shell], object path /org/freedesktop
Feb 25 14:06:06 faraz.test.server systemd-logind[17246]: Removed session c1.
Feb 25 14:06:06 faraz.test.server systemd[1]: Removed slice user-42.slice.
Feb 25 14:06:06 faraz.test.server systemd[1]: Stopping user-42.slice.
Feb 25 14:06:10 faraz.test.server gnome-session[18924]: vmware-user: could not open /proc/fs/vmblock/dev
Feb 25 14:06:11 faraz.test.server gnome-session[18924]: Entering running state

Pstree

Command pstree display the all process in tree form. As you can see PID for systemd is 1.

[root@faraz ~]# pstree
systemd---ModemManager---2*[{ModemManager}]
         +-NetworkManager---dhclient
         ¦                +-2*[{NetworkManager}]
         +-abrt-dbus---{abrt-dbus}
         +-2*[abrt-watch-log]
         +-abrtd
         +-accounts-daemon---2*[{accounts-daemon}]
         +-alsactl
         +-at-spi-bus-laun---dbus-daemon---{dbus-daemon}
         ¦                 +-3*[{at-spi-bus-laun}]
         +-at-spi2-registr---{at-spi2-registr}
         +-atd
         +-auditd---audispd---sedispatch
         ¦        ¦         +-{audispd}
         ¦        +-{auditd}
         +-avahi-daemon---avahi-daemon
         +-bluetoothd
         +-caribou---2*[{caribou}]
         +-colord---2*[{colord}]

Systemctl

Systemctl is to control the state of systemd system and service manager. You can control your system with this command.

List all the units by using command systemctl or systemctl  list-units both commands are working same.

[root@faraz ~]# systemctl
UNIT                                                                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                                           loaded active running   Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:01.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged   VBOX_CD-ROM
sys-devices-pci0000:00-0000:00:03.0-net-enp0s3.device                       loaded active plugged   82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)
sys-devices-pci0000:00-0000:00:05.0-sound-card0.device                      loaded active plugged   82801AA AC'97 Audio Controller
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   VBOX_HARDDISK 1
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged   LVM PV CIG0oS-QFvE-vxjF-tHYG-Zz93-4Vvl-tSB6da on /dev/sda2 2
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged   VBOX_HARDDISK
sys-devices-platform-serial8250-tty-ttyS0.device                            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-platform-serial8250-tty-ttyS1.device                            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-platform-serial8250-tty-ttyS2.device                            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
sys-devices-platform-serial8250-tty-ttyS3.device                            loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3

List all the failed units by using command systemctl  –failed.

[root@faraz ~]# systemctl --failed
UNIT                LOAD   ACTIVE SUB    DESCRIPTION
? kdump.service       loaded failed failed Crash recovery kernel arming
? rngd.service        loaded failed failed Hardware RNG Entropy Gatherer Daemon
? xrdp-sesman.service loaded failed failed xrdp session manager
? xrdp.service        loaded failed failed xrdp daemon

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.

4 loaded units listed. Pass --all to see loaded but inactive units, too.

To show all installed unit files use  command systemctl list-unit-files. (Do it yourself)

To check the all active/failed services by using command systemctl list-units –t service. You can manage services after checking which service is active and inactive.

[root@faraz ~]# systemctl list-units -t service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
abrt-ccpp.service                  loaded active exited  Install ABRT coredump hook
abrt-oops.service                  loaded active running ABRT kernel log watcher
abrt-xorg.service                  loaded active running ABRT Xorg log watcher
abrtd.service                      loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service            loaded active running Accounts Service
alsa-state.service                 loaded active running Manage Sound Card State (restore and store)
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service
avahi-daemon.service               loaded active running Avahi mDNS/DNS-SD Stack
blk-availability.service           loaded active exited  Availability of block devices
bluetooth.service                  loaded active running Bluetooth service
colord.service                     loaded active running Manage, Install and Generate Color Profiles
crond.service                      loaded active running Command Scheduler
cups.service                       loaded active running CUPS Printing Service
dbus.service                       loaded active running D-Bus System Message Bus
firewalld.service                  loaded active running firewalld - dynamic firewall daemon
gdm.service                        loaded active running GNOME Display Manager
gssproxy.service                   loaded active running GSSAPI Proxy Daemon
iscsi-shutdown.service             loaded active exited  Logout off all iSCSI sessions on shutdown
? kdump.service                      loaded failed failed  Crash recovery kernel arming

Services in Linux Managed by systemctl

In previous version of linux services were  managed by service command but in latest version it is done by systemctl instead of service.

Systemctl is powerful command because this command you can not only start/stop/restart/reload but you can also check list of services on/off similarly like  chkconfig.

You can check status of service by option status , to start service use option start and to stop use option stop.

You can restart and reload service with restart/reload options. Below examples will show how to use these options with systemctl

 

[root@faraz ~]# systemctl status sshd
? sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2016-02-25 13:44:14 PKT; 5s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 1124 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 1124 (code=exited, status=0/SUCCESS)

Feb 25 13:23:12 faraz.test.server systemd[1]: Started OpenSSH server daemon.
Feb 25 13:23:12 faraz.test.server systemd[1]: Starting OpenSSH server daemon...
Feb 25 13:23:15 faraz.test.server sshd[1124]: Server listening on 0.0.0.0 port 122.
Feb 25 13:23:15 faraz.test.server sshd[1124]: Server listening on :: port 122.
Feb 25 13:38:14 faraz.test.server sshd[13901]: Accepted password for root from 192.168.40.112 port 56866 ssh2
Feb 25 13:44:13 faraz.test.server systemd[1]: Stopping OpenSSH server daemon...
Feb 25 13:44:14 faraz.test.server systemd[1]: Stopped OpenSSH server daemon.
[root@faraz ~]#
[root@faraz ~]# systemctl start sshd
[root@faraz ~]# systemctl status sshd
? sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2016-02-25 13:44:37 PKT; 2s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 14319 (sshd)
CGroup: /system.slice/sshd.service
+-14319 /usr/sbin/sshd -D

Feb 25 13:44:37 faraz.test.server systemd[1]: Started OpenSSH server daemon.
Feb 25 13:44:37 faraz.test.server systemd[1]: Starting OpenSSH server daemon...
Feb 25 13:44:37 faraz.test.server sshd[14319]: Server listening on 0.0.0.0 port 122.
Feb 25 13:44:37 faraz.test.server sshd[14319]: Server listening on :: port 122.
[root@faraz ~]#
[root@faraz ~]# systemctl stop sshd
[root@faraz ~]# systemctl status sshd
? sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2016-02-25 13:44:54 PKT; 1s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 14319 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 14319 (code=exited, status=0/SUCCESS)

Feb 25 13:44:37 faraz.test.server systemd[1]: Started OpenSSH server daemon.
Feb 25 13:44:37 faraz.test.server systemd[1]: Starting OpenSSH server daemon...
Feb 25 13:44:37 faraz.test.server sshd[14319]: Server listening on 0.0.0.0 port 122.
Feb 25 13:44:37 faraz.test.server sshd[14319]: Server listening on :: port 122.
Feb 25 13:44:54 faraz.test.server systemd[1]: Stopping OpenSSH server daemon...
Feb 25 13:44:54 faraz.test.server systemd[1]: Stopped OpenSSH server daemon.
[root@faraz ~]#
[root@faraz ~]# systemctl restart sshd
[root@faraz ~]# systemctl reload sshd
[root@faraz ~]# systemctl status sshd
? sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2016-02-25 13:57:22 PKT; 21s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 15445 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 15314 (sshd)
CGroup: /system.slice/sshd.service
+-15314 /usr/sbin/sshd -D

Feb 25 13:57:22 faraz.test.server systemd[1]: Started OpenSSH server daemon.
Feb 25 13:57:22 faraz.test.server systemd[1]: Starting OpenSSH server daemon...
Feb 25 13:57:22 faraz.test.server sshd[15314]: Server listening on 0.0.0.0 port 122.
Feb 25 13:57:22 faraz.test.server sshd[15314]: Server listening on :: port 122.
Feb 25 13:57:27 faraz.test.server sshd[15314]: Received SIGHUP; restarting.
Feb 25 13:57:27 faraz.test.server systemd[1]: Reloaded OpenSSH server daemon.
Feb 25 13:57:28 faraz.test.server sshd[15314]: Server listening on 0.0.0.0 port 122.
Feb 25 13:57:28 faraz.test.server sshd[15314]: Server listening on :: port 122.
[root@faraz ~]#

Now we will use systemctl to enable service instead of using chkconfig

To make service run at boot time we can make service enabled

To check if  service is enabled or disabled use option is-enabled.

Syntax systemctl option service-daeman-name.

[root@faraz ~]# systemctl is-enabled sshd
disabled
[root@faraz ~]#

To  enable service use below command

[root@faraz ~]# systemctl enable sshd
Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.
[root@faraz ~]#

To  disable service

[root@faraz ~]# systemctl disable sshd
Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.
[root@faraz ~]#

You can manage service on remote side by using ssh (secure shell) with –H option.

[root@faraz ~]# systemctl status sshd -H [email protected]
[email protected]'s password:
? sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2016-02-25 13:57:22 PKT; 5min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 15305 (sshd)

Like init command can switch targets/runlevels similarly systemctl can switch targets/runleves mode by using command systemctl isolate runlevel(number 0 to 6).target.
[root@faraz ~]# systemctl isolate runlevel1.target
[root@faraz ~]# systemctl isolate runlevel0.target or poweroff.target
[root@faraz ~]# systemctl isolate runlevel1.target or rescue.target
[root@faraz ~]# systemctl isolate runlevel(2,3,4).target or multi-user.target
[root@faraz ~]# systemctl isolate runlevel5.target or graphical.target
[root@faraz ~]# systemctl isolate runlevel6.target or reboot.target

List all the current targets by using command systemctl list-units –type=target.

[root@faraz ~]# systemctl list-units --type=target
UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
basic.target           loaded active active Basic System
cryptsetup.target      loaded active active Encrypted Volumes
getty.target           loaded active active Login Prompts
graphical.target       loaded active active Graphical Interface
local-fs-pre.target    loaded active active Local File Systems (Pre)
local-fs.target        loaded active active Local File Systems
multi-user.target      loaded active active Multi-User System
network-online.target  loaded active active Network is Online
network.target         loaded active active Network
nfs-client.target      loaded active active NFS client services
nss-lookup.target      loaded active active Host and Network Name Lookups
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target           loaded active active Paths
remote-fs-pre.target   loaded active active Remote File Systems (Pre)
remote-fs.target       loaded active active Remote File Systems
rpcbind.target         loaded active active RPC Port Mapper
slices.target          loaded active active Slices
sockets.target         loaded active active Sockets
swap.target            loaded active active Swap
sysinit.target         loaded active active System Initialization
timers.target          loaded active active Timers

To check your default target you can use  the command systemctl get-default.

You can also set the target by using command systemctl set-default graphical.target (do it yourself).

[root@faraz ~]# systemctl get-default
graphical.target
[root@faraz ~]#

We can also shutdown, reboot or suspend system by using systemctl command.

[root@faraz ~]# systemctl reboot
[root@faraz ~]# systemctl suspend
[root@faraz ~]# systemctl poweroff

To get the hostname and detail of about your Linux OS by using command hostnamectl.

[root@faraz ~]# hostnamectl
Static hostname: faraz.test.server
Icon name: computer-vm
Chassis: vm
Machine ID: 09c76cd99cd74900a5ac0a0ec93842e7
Boot ID: 6662d1aa1df0459bb0884b4db9ca1269
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.el7.x86_64
Architecture: x86-64
[root@faraz ~]#

In this tutorial we have discussed  about linux systemd and how to use manage it using systemctl.We also practiced how to use systemctl commands to list services and to manager service by starting/stopping/enabling/disabling and checking their status. We also examined lots of other commands like journalctl, systemctl, hostnamectl with many options.

I hope you enjoyed this article with deep learning in system commands. Keep learning with us.

Thanks

SUBSCRIBE OUR NEWSLETTER
I agree to have my personal information transfered to MailChimp ( more information )
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.