Linux Systemd
systemd provides an array of system components for Linux operating systems. Its main aim is to unify service configuration and behavior across Linux distributions. It is the controlling interface and inspection tool for the init system and service manager systemd.
systemd replaces [Linux Sys V]] - We used to control system services with commands like "service httpd restart" and now we use instead the awkwardly syntaxed systemctl such as "systemctl restart httpd" or "systemctl restart apache2"
systemd uses the systemctl command
Contents
systemctl command
Command Description systemctl start [service] Start a service. systemctl stop [service] Stop a service. systemctl enable [service] Enable a service to start automatically at system boot. systemctl disable [service] Disable a service from starting automatically at system boot. systemctl status [service] View the status of a service. systemctl restart [service] Restart a service. systemctl reload [service] Reload a service's configuration without restarting it. systemctl mask [service] Prevent a service from being started. systemctl unmask [service] Allow a previously masked service to be started. systemctl set-default [target] Change the default system target (runlevel). systemctl list-unit-files List all installed unit files and their states. systemctl list-dependencies [unit] List the dependencies of a specific unit. systemctl list-sockets List all active sockets. systemctl list-jobs List all active systemd jobs. systemctl list-units Show the status of all loaded and active systemd units.
To start the service, all you have to do is use the start flag with the systemctl command
sudo systemctl start <service_name>
To stop the service, you use the stop flag with the systemctl command
sudo systemctl stop <service_name>
Check the status of the service
sudo systemctl status <service_name>
Managing Services
systemd can control components that run after boot and also maintain components indefinitely. These tasks are known as units, and each unit has a corresponding unit file. Units might concern mounting storage devices (.mount), configuring hardware (.device), sockets (.socket), or managing services (.service).
Enabling service referes to start the service automatically at the system boot.
sudo systemctl enable <service_name>
Prevent the service from auto starting at boot
sudo systemctl disable <service_name>
Each unit has a corresponding unit file. These unit files are usually located in the following directories:
- The /lib/systemd/system directory holds unit files that are provided by the system or are supplied by installed packages. This directory is also a symlink to /usr/lib/systemd/user/ directory.
- The /etc/systemd/system directory stores unit files that are user-provided.
Managing Service Example: Bluetooth
Check if Bluetooth is installed:
sudo systemctl status bluetooth
Enable Bluetooth at Startup: If it’s only starting manually and not on boot, you can enable it at startup:
sudo systemctl enable bluetooth
Run the following commands to start the Bluetooth daemon:
sudo systemctl start bluetooth
Restart the Bluetooth Service:
sudo systemctl restart bluetooth
Check Logs for Errors relating to bluetooth service:
journalctl -xe | grep bluetooth
Use the bluetoothctl command-line tool to further troubleshoot Bluetooth device activation:
bluetoothctl
Deprecated /forcefsck File
Creating a file /forcefsck to force a file check on reboot does not work anymore since systemd now handles initialization. The traditional way of using tune2fs works only in sysvinit or upstart systems.
A way is to pass kernel parameters during boot that control the systemd services for file system check. fsck.mode=force will force a file check. This involves editing grub, if you are on a modern Ubuntu/Mint system you will use Grub2 Bootloader. After editing /etc/default/grub or the scripts in the /etc/grub.d folder you should run sudo update-grub to incorporate the changes.
sudo vi /etc/default/grub sudo update-grub
The first line will open the grub2 bootloader configuration file into a text editor. In the text file look for a line that has
GRUB_CMDLINE_LINUX_DEFAULT=""
and change it to
GRUB_CMDLINE_LINUX_DEFAULT="fsck.mode=force"
you may also see a parameter for quiet splash, that is up to you however we recommend not using quiet splash as it hides important system messages during boot.