Awesome ! systemd Commands to Manage Linux System

Systemd is the new system and service manager for Linux. It is a replacement for init system and can manage system startup and services. It starts up and supervises the entire system. In article we are using centos 7.0 installed with systemd 216 version and the latest version is available for download from freedesktop.org.

With new player in town, PID 1 is occupied by "systemd" and can be seen from pstree command as well:

[[email protected] ~]# pstree

pstree command

Lets explore what systemd is capable of and what possibilities we have with the new replacement for sysVinit.

1. Faster startup

The sysvinit starts the processes serially, one at a time. Systemd starts services in parallel and starts only those services which are actually required, reducing the boot time significantly.
You can get the boot process duration with the following command:

[[email protected] ~]# systemd-analyze

systemd-analyze

The command systemd-analyze time also shows the same information.

[[email protected] ~]# systemd-analyze time

systemd-analyze time

If you want to print a list of all running units, the blame option to systemd-analyze command can provide you with that, ordered by the time taken to initialize.

[[email protected] ~]# systemd-analyze blame

systemd-analyze blame

The above screen shows only a small number of processes, you can scroll through the list with arrows just like in less pager.

2. The systemctl command

The systemctl command is the most talked command that comes with systemd. You can manage a whole lot of your system with this command. Let's explore this command before going any further:

2.1 List Units

systemctl command without any option lists all the running units. The list-units switch also does the same.

[[email protected] ~]# systemctl

or

[[email protected] ~]# systemctl list-units

systemctl list-units

2.2 Listing failed units

The failed units can be listed with --failed switch.

[[email protected] ~]# systemctl --failed

Failed Units

You will see the use of systemctl command at many places in this article.

3. Managing services

Let us now see how services can be managed with systemd.

3.1 Active services

All the active services can be checked with the following command:

[[email protected] ~]# systemctl list-units -t service

Active Services

3.2 Service status

In the sysvinit, we could use the "service" command to manage the services, but with systemd, the systemctl command is used to manage services. In ordwer to see whether a service is running or not, we can use the systemctl command like this:

[[email protected] ~]# systemctl status dnsmasq

Service status

3.3 Start a service

To start a service, again we use the systemctl command as:

[[email protected] ~]# systemctl start dnsmasq

As opposed to service command, this command does not give any output. But of course, we can check the status of the service once again to confirm that its started successfully:

Start a service

3.4 Stopping a service

Now you are smart enough and already know the command to stop a service with systemd:

[[email protected] ~]# systemctl stop dnsmasq

Stop a service

3.5 Restart a service

Similarly, restarting a service is managed using 'systemctl restart ':

[[email protected] ~]# systemctl restart dnsmasq

Restart a service

3.6 Reload a service

In case we need to reload the configuration of service (say ssh), without restarting it, we can use the command:

[[email protected] ~]# systemctl reload sshd

Reload a service

Although all of the above syntax are working, the official documentation suggests that these command be run with following syntax:

[[email protected] ~]# systemctl status dnsmasq.service

Alternate syntax

4. Managing services at boot

The chkconfig command was used to manage services at boot. The same command systemd is used with systemd to manage services at boot.

4.1 Checking service status at boot

In order to check if a service is enabled on boot or not:

[[email protected] ~]# systemctl is-enabled dnsmasq.service

Service at system boot

4.2 Enable a service at boot

systemctl command can be used like this to enable a service at boot (this corresponds to sysvinit 'chkconfig on')

[[email protected] ~]# systemctl enable dnsmasq.service

Enable service at boot

4.3 Disable a service at boot

Similarly, the services can be disabled at boot with systemctl command:

[[email protected] ~]# systemctl disable dnsmasq.service

Disable service at boot

5. Managing Remote systems

Typically, all of the ablve systemctl commands can be used to manage a remote host with systemctl command itself. This will use ssh for communication with the remote host. All you need to do is add the user and host to systemctl command like this:

[[email protected] ~]# systemctl status sshd -H [email protected]

Manage remote system

6. Managing targets:

Systemd has concept of targets having similar purpose to runlevels in sysVinit.
The runlevels in sysVinit were mostly numeric (0,1,2,...). Here are the runlevels in sysVinit with their systemd counterparts:

0 runlevel0.target, poweroff.target
1, s, single runlevel1.target, rescue.target
2, 4 runlevel2.target, runlevel4.target, multi-user.target
3 runlevel3.target, multi-user.target
5 runlevel5.target, graphical.target
6 runlevel6.target, reboot.target
emergency emergency.target

6.1 Changing current target

The current target(runlevel) can be changed with the command:

[[email protected] ~]# systemctl isolate graphical.target

Change target

6.2 List current target

If you want to see what target you are in, you need to list all the corresponding units. It might not feel at home with this new way, but its the way systemd works.

[[email protected]xide ~]# systemctl list-units --type=target

Current target

You can see "graphical.target" listed here. This is what we changed our target into. Now let's change the runlevel again to multi-user.target and then analyze this output:

[[email protected] ~]# systemctl isolate multi-user.target
[[email protected] ~]# systemctl list-units --type=target

Multi-user target

6.3 List default target

To list the default target, we use systemctl command like this:

[[email protected] ~]# systemctl get-default

Get default target

6.4 Change default target

The default target can be set with set-default command with systemctl:

[[email protected] ~]# systemctl set-default graphical.target

Set default target

7. Logging in systemd

The systemd has its own logging system called journald. It replaces the syslog daemon from sysVinit. The command journalctl is used to read the logs.

[[email protected] ~]# journalctl

Systemd logs

7.1 Boot messages

To see all boot messages, run the command "journalctl -b".

[[email protected] ~]# journalctl -b

Boot messages

7.2 Follow logs

The following command follows the system logs in real time (similar to tail -f).

[[email protected] ~]# journalctl -f

Follow logs

7.3 Service specific logs

To check logs specific to a particular service or executable, use journalctl like this:

[[email protected] ~]# journalctl /usr/sbin/dnsmasq

Service specific logs

8. Power management

The systemctl command can be used to put the system down, or reboot or hibernate.

To poweroff, reboot, suspend and hibernate, use the following commands respectively:

[[email protected] ~]# systemctl poweroff

[[email protected] ~]# systemctl reboot

[[email protected] ~]# systemctl suspend

[[email protected] ~]# systemctl reboot

9. Bonus

The systemd brings out the whole new approach to interacting with your operating system. The systemd is so full of features. For example, you can get the hostname and other useful features about your Linux machine, you can use hostnamectl command

[[email protected] ~]# hostnamectl

hostnamectl

 

About Raghu

Raghu is working as Linux Server Administrator in Acknown Technologies Pvt. Ltd. He has been using Linux from last 5 years. He completed his RHCE certification in 2009. He likes to read about Linux and other Open Source Technologies and write articles on these.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.