About the Systemd
January 18, 2025 · 2 min read · Page View:
A real powerful tool
If you have any questions, feel free to comment below.
It has been a long time that the linux use init
to manage the startup process, such as sudo /etc/init.d/apache2 start
or service apache2 start
, but the init
is serial. To address this issue, the systemd
was born. The d is the abbreviation of daemon
, which means the systemd
is a daemon manager. The systemd substitutes the initd and becomes the default main process PID 1
.
You can check the version systemctl --version
.
systemctl #
sudo systemctl reboot
sudo systemctl poweroff
sudo systemctl suspend
hostnamectl #
Look up the host info, Architecture, Hardware, Kernel, Operating System, etc.
You can also query via
uname -a
.
timedatectl #
Query the timezone.
loginctl #
Manage the login session.
- loginctl list-sessions
- loginctl list-users
- loginctl show-user
Unit #
There are 12 types of units:
- Service unit
- Target unit which is a group of units
- Device Unit
- Mount Unit
- Automount Unit
- Path Unit
- Scope Unit: not started by systemd
- Slice Unit: process group
- Snapshot Unit: Systemd snapshot, can switch back to a snapshot
- Socket Unit: process communication socket
- Swap Unit: swap file
- Timer Unit
You can also query them:
- systemctl list-units –all
- systemctl list-units –all –state=inactive
- systemctl list-units –type=service
systemctl status #
- systemctl status bluetooth.service
systemctl about service #
- systemctl start
- systemctl stop
- systemctl restart
- systemctl reload
- systemctl enable
- systemctl disable
- systemctl show service
Unit config #
The unit config file is located at /etc/systemd/system/
. But most of them are the symbolic links to the real config file in /usr/lib/systemd/system/
.
The
systemctl enable
command will create a symbolic link to the real config file in/etc/systemd/system/
(if you config start on boot in the unit file, it will start on boot) And thesystemctl disable
command will remove the symbolic link. Such as
sudo systemctl enable [email protected]
# which is equivalent to
$ sudo ln -s '/usr/lib/systemd/system/[email protected]' '/etc/systemd/system/multi-user.target.wants/[email protected]'
You can list all the config files:
systemctl list-unit-files
# the tail is the kind of unit, such as service(default), socket, etc.
There are four status of the unit:
enabled
: the unit is enableddisabled
: the unit is disabledstatic
: the unit is static, which only served as other unit’s dependencymasked
: the unit is banned to be enabled
Adjust file #
systemctl cat atd.service
can show the specific unit file.
The detail you can refer to the official document.
Once you adjust the unit file, you need to reload the systemd and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart httpd.service
Target #
The target is a group of units, once the target is enabled, the units in the target will be enabled.
journalctl #
You can check the kernel log and the service log by only journalctl
. The config file is /etc/systemd/journald.conf
.
- sudo journalctl
- sudo journalctl -k # kernel log
- sudo journalctl –since yesterday
- sudo journalctl -f # follow the log
- sudo journalctl _PID=1 # the log of the specific process
- sudo journalctl -u # the log of the specific service
Refer to https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
Related readings
If you find this blog useful and want to support my blog, need my skill for something, or have a coffee chat with me, feel free to: