Introduction to systemd/systemctl

Systemd is a suite of tools that allow you to quickly and easily manage your server from bootup onwards. Systemd gives you a linux system and service manager which runs as its own process ID (PID), which will always be PID 1. PID 1 controls the start of the rest of the system. With most of the newer Linux based operating systems, systemd is now the default service manager. In this article, we’ll overview the most important commands you’ll need to know for managing a systemd server.

Managing Units

The first thing you can try when using your systemd-based server, is running the command shown below:

systemctl

This will output a list of everything running on your system.  You’ll likely have a large number of units listed (a unit is similar to a ‘service’ in older Linux based operating systems, however it can also be used to refer to network resources, devices, and filesystem mounts), but if you’re working with a server that might not have been set up by you, and want to get familiar with what exactly is running on it, this would be a good place to begin.

Another useful systemctl command would be to check for failed units.  You can have systemctl output only failed units with this command:

systemctl --failed

If you want to know more about a service which shows up in a failed state, you can use this command to get that information:

systemctl status 

This will give you process and service information for the unit, as well as the last few lines of logs.

As far as managing a unit, you can start any unit by issuing the following command:

systemctl start 

Stopping a unit is the same, but replace start with stop as shown below:

systemctl stop 

You can restart a service with the following command:

systemctl restart 

When you need to have a service start on boot, or need to disable a service from starting on boot, the commands below will accomplish that:

systemctl enable 

systemctl disable 

These are the basics of working with systemctl, and should be enough to get you started with interacting with units on your systemd server.

Managing Power States

Another thing that systemd can do is handle the power states of your server. 

To reboot the server, type the following command:

systemctl reboot

To shut down and power the server off, enter the following command:

systemctl poweroff

Handling and Viewing Logs

Systemd includes a component called journald, which gathers and manages journal entries from the whole system. It gathers log information from applications as well as the kernel itself.

To view all the information that journald has captured ordered from oldest to newest, issue the following command:

journalctl

You can get more specific by asking it to only output entries since the most recent boot:

journalctl -b

Suppose that your server stopped responding at a specific date and time, and you wanted to look at logs around that, you can use the following command to specify a starting date and time for the logs fetched:

journalctl --since="2019-10-31 14:23:46"

Modifying Unit Files

Due to the complex nature of the format of unit files, it’s beyond the scope of this article, but it’s worth knowing that with systemctl, you have the ability to edit Unit Files without needing to know the exact location of the files on the disk.

To edit an existing unit file, type:

systemctl edit --full unit

Once you’ve made the edit, you will need to reload systemd for it to accept the changes you’ve made to the unit:

systemctl daemon-reload

Conclusion

Systemd is robust, and covers a much wider range of possibilities and functionality than covered in this introduction. You can read more about systemd, systemctl, and journalctl on the web or by utilizing their respective man pages from within your server.

Leave a Reply

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