Systemd provides a standard process for controlling what programs run when a Linux system boots up.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Systemd related tasks on Linux.
In this context, we shall look into how to use systemd to manage Linux Server.
How to Manage Linux server using systemd?
Here, you will see how to Manage Linux server using systemd with ease.
Also, we use the systemctl command to control services with systemd.
Linux servers can be managed easily via command-line with the help of various commands of systemd.
We will use the Apache Service, known as httpd on RedHat Based Linux distributions as an example.
$ systemctl start httpd
$ systemctl stop httpd
This command Fully stops and starts a service:
$ systemctl restart httpd
Reloads a service’s configuration without causing the service to restart.
$ systemctl reload httpd
Enable Apache on Boot:
This will set the specified service to start up every time a system is booted.
$ systemctl enable httpd
Disable Apache on Boot:
This will set the specified service to not start up every time a system is booted up.
$ systemctl disable httpd
Check Apache Status:
$ systemctl status httpd
Methods to check specific states using systemctl command?
1. To check to see if a unit is currently active (running), we can use the following command:
$ systemctl is-active application.service
2. To see if the unit is enabled, we can use the following command:
$ systemctl is-enabled application.service
3. To check whether the unit is in a failed state, we can use the following command:
$ systemctl is-failed application.service
System State Overview
We can check the current state of the system with a number of systemctl commands that are given below.
1. List Current Units
To see a list of all of the active units we can use the following command:
$ systemctl list-units
We can get other information by adding additional flags as given below:
$ systemctl list-units --all
Use other flags to filter results with the following command:
$ systemctl list-units --all --state=inactive
Another common filter is the –type= filter. We can display units of the type we are interested in with the following command:
$ systemctl list-units --type=service
2. List All Unit Files
We can use the list-unit-files command to see every available unit file within the systemd paths, including those that systemd has not attempted to load.
$ systemctl list-unit-files
How to Manage Unit files with systemd ?
Now we can find out more specific information about units using some additional commands.
1. For Displaying a Unit File
To display the unit file that systemd has loaded into its system, we can use the cat command:
$ systemctl cat atd.service
2. For Displaying Dependencies
To see a unit’s dependency tree, we can use the list-dependencies command:
$ systemctl list-dependencies sshd.service
3. For Checking Unit Properties
To see the low-level properties of a unit, we can use the show command.
$ systemctl show sshd.service
If we want to display a single property, we can pass the -p flag with the property name.
$ systemctl show sshd.service -p Conflicts
We can replace Conflicts with any property we wish to check
How to Mask and Unmask Units using Systemd ?
Systemd can mark a unit as completely unstartable, automatically or manually, by linking it to /dev/null.
This is called masking the unit, and is possible with the following command:
$ systemctl mask nginx.service
This will prevent the Nginx service from being started, automatically or manually, for as long as it is masked.
We can check the list-unit-files, to see if the service is now listed as masked:
$ systemctl list-unit-files
On attempts to start the service, a message can be seen as shown below:
$ systemctl start nginx.service
Failed to start nginx.service: Unit nginx.service is masked.
To unmask a unit, making it available for use again, we can use the following command:
$ systemctl unmask nginx.service
[Need assistance to manage unit files in Linux? We are here for you!]
How to use Systemd for Editing Unit Files in Linux?
From the systemd version 218 and above, we can use it for modifying unit files.
The edit command can be used to open a unit file snippet.
$ systemctl edit nginx.service
To edit a full unit file instead of creating a snippet, we can pass the –full flag:
$ sudo systemctl edit --full nginx.service
For removing a fully modified unit file, we would type:
$ sudo rm /etc/systemd/system/nginx.service
After deleting the file or directory, we must reload the systemd process to stop it from reverting those files back to using the system copies.
$ systemctl daemon-reload
How to Adjust the System State (Runlevel) with Targets ?
To find the default target for the system we can use the following command:
$ systemctl get-default
Listing Available Targets using Systemd?
For the list of the available targets we can use the following command:
$ systemctl list-unit-files --type=target
To see all of the active targets we can use the following command:
$ systemctl list-units --type=target
Shortcuts for Important Events with Systemd
systemctl has some shortcuts for events like powering off or rebooting a linux server.
To put the system into rescue (single-user) mode we can use the following command:
$ systemctl rescue
For halting the system, we can use the halt command:
$ systemctl halt
To initiate a full shutdown, we can use the following command:
$ systemctl poweroff
A restart can be started with the following cmmand:
$ systemctl reboot