Systemctl and Systemd on CentOS 7

Introduction to Systemd

Systemd is a system and service manager for Linux operating systems which is the new standard for CentOS 7, RHEL 7, Fedora, ArchLinux. Most Linux distros seem to be going towards adding Systemd as a standard and replace the old init.d. By design it’s backwards compatible with SysVinit scripts, and introduces new features like on-demand activation of processes, parallel startup for system services upon boot or even support for system state snapshots. systemctl it the core tool used to manage Systemd services and units.

Systemd introduces a new concept called systemd units. These units are represented by unit configuration files located in following directories:

/usr/lib/systemd/system/
/run/systemd/system/
/etc/systemd/system/

Each of these folders contain different Systemd units. For example, the first one contains units distributed by RPMs, like sshd while the last one contains units created and managed by the system administrator, i.e: Network Manager.

Manage Services

The main purpose of any init system is to manage services and initialize services post kernel boot. systemctl is quite simple to use here and uses regular start, stop, status commands. The full syntax to start a service would be:

systemctl start name.service

systemctl is smart enough to determine what kind of application we’re dealing with as such this should suffice:

systemctl start name

The same applies for stop and restart commands as well.

Listing Services

The following command will list all the currently loaded service units:

systemctl list-units --type service

List all installed service units and determine their state(ENABLED/DISABLED):

systemctl list-unit-files --type service

Enable, Disable, Mask Services

Enabling a service means that it will start automatically upon next system reboot. Running the enable command will create the necessary symlink from the /usr/lib/systemd/system/ folder to the /etc/systemd/system/ one. For example, enabling nginx will have the following output:

[root@web01 ~]# systemctl enable nginx.service
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.service'

In order to disable nginx and don’t start it on boot time:

[root@web01 ~]# systemctl disable nginx.service
rm '/etc/systemd/system/multi-user.target.wants/nginx.service'

which will remove the symlink. We can even make any service and prevent it from being started by any other services by running:

[root@web01 ~]# systemctl mask nginx.service
ln -s '/dev/null' '/etc/systemd/system/nginx.service'

which will create a symlink from /etc/systemd/system/nginx.service to /dev/null

Power Management

systemctl is able to manage the machine’s state as well which means that you can reboot, shutdown, power-off, suspend, hibernate and so on:

  • systemctl reboot – Reboot the system
  • systemctl poweroff – Power-off the system
  • systemctl suspend – Suspend the system
  • systemctl hibernate – Put the system into hibernation

Conclusions

systemctl allows you to full control your systemd instance. It’s a really powerful tool and this is just a basic guide on what you can achieve using it.

More details about systemd and systemctl can be found here.