Posts CentOS7 Systemctl Cheat Sheet
Post
Cancel

CentOS7 Systemctl Cheat Sheet

Since CentOS7, command service has been changed to systemctl. For a while I was very repel to it cause I was new and not used to it at all. Though that service is still usable in CentOS7 but after I thought, what the hell, why not learn and get used to something new? Therefore, this article is produced as reference, for me and anyone else who is also struggling to learn and unfamiliar with systemctl like me.

Before we start, please note that systemctl not only can manage service but also path, slice, snapshot, socket, swap, target and timer. And they are called unit files.

Gathering systemd information

First of all, we can use help to list all the usages.

1
systemctl -h

systemctl

It is important to know/list out all the installed unit files.

1
systemctl list-unit-files

systemctl

static means it can’t be start or stop like a service. static unit files are mostly a “one time job” or is depended by other unit files.

Unit files name can be added for particular query.

1
systemctl list-unit-files sshd.service

systemctl

Listing out the status of all the unit files.

1
systemctl list-units

systemctl

Listing out the status of a particular unit file.

1
systemctl list-units sshd.service

systemctl

Both systemctl list-units and systemctl list-unit-files can be appended with –type for particular type of unit file.

1
2
systemctl list-units --type=target
systemctl list-unit-files --type=path

systemctl

Among all the unit file types, socket has it’s own command to list out.

1
systemctl list-sockets

systemctl

Show a unit’s dependencies.

1
systemctl list-dependencies crond.service

systemctl

Working with services

Commands to start, stop and showing status.

1
2
3
systemctl stop crond
systemctl status crond
systemctl start crond

systemctl
systemctl

Commands to reload, restart.

1
2
systemctl reload crond
systemctl restart crond

systemctl

Commands to enable and disable service at system start up.

1
2
systemctl disable crond
systemctl enable crond

systemctl
systemctl

From the image we learn that to start or stop a service is actually just putting or removing a symlink of the unit file at /etc/systemd/system/multi-user.target.wants/ directory.

A quick peep of /etc/systemd/system/multi-user.target.wants/ directory.
systemctl

Checkout the current runlevel where multi-user.target equals to runlevel init 3.

1
systemctl get-default

systemctl

Commands to change runlevel.

1
systemctl set-default graphical.target

systemctl

We can tell from the message that the runlevel is also a symlink linked to default.target.
systemctl

Viewing log messages

Lastly, command to view all service logs.

1
journalctl

systemctl

To view log of particular service.

1
journalctl -u crond.services

systemctl

To view the real time log. The image shows the real time log pop out when I try to login the demo server.

1
journalctl -f

systemctl

REFERENCES:
systemd Cheat Sheet for Red Hat Enterprise Linux 7 - Red Hat Customer Portal

This post is licensed under CC BY 4.0 by the author.