The command-line use systemctl, which stands for "system control," is essential for managing services on Linux systems using the systemd init system. It is an important tool for monitoring how services, daemons, and other system components behave.
To help you learn how to control services on your Linux system, we'll go into much detail about systemctl command examples in this article.
But before moving on to the command examples, let's quickly grasp the context in which systemctl works.
In the most present Linux distributions, Systemd, a system and service manager, has taken the role of the standard SysV init system. It manages the launch and administration of system services and acts as the initialization procedure.
Systemctl, the main command line tool to Systemd, allows users and administrators to effectively control the state and behavior of these services. Whether you need to start, stop, enable, or deactivate services, Systemctl is the suitable tool for the task.
Basic systemctl Commands
1. Starting and Stopping Services
One of the most common tasks while working with them is starting and stopping services. Type "systemctl start" to start a service and "systemctl stop" to stop one. Try this:
sudo systemctl start apache2 sudo systemctl stop apache2
2. Enabling and Disabling Services
A service can start either automatically at boot time or not, based on whether it is enabled or deactivated. For this, "systemctl enable" and "systemctl disable" should be used.
sudo systemctl enable ssh sudo systemctl disable ssh
3. Checking the Status of Services
Use the 'systemctl status' command to find out a service's status and if it is active:
systemctl status apache2
Advanced systemctl Commands
1. Reloading Configurations
You can tell systemd to refresh the configuration when you make changes to a service's configuration file without restarting the service. Use 'systemctl reload' to do this:
sudo systemctl reload apache2
2. Restarting Services
A service must be restarted to apply updates or fix problems. Utilize 'systemctl restart'.
sudo systemctl restart apache2
3. Viewing Service Logs
Systemctl also provides the easiest way to access service logs. You can use 'systemctl' with the '--user' option to view logs for user services.
journalctl --user -u <service-name> # View logs for a specific user service
Service Management Examples
Now, let's see some practical examples of managing common system services.
1. Apache Web Server
Starting Apache
sudo systemctl start apache2
Stopping Apache
sudo systemctl stop apache2
Restarting Apache
sudo systemctl restart apache2
Enabling Apache at Boot
sudo systemctl enable apache2
Disabling Apache at Boot
sudo systemctl disable apache2
2. MySQL Database Server
Starting MySQL
sudo systemctl start mysql
Stopping MySQL
sudo systemctl stop mysql
Restarting MySQL
sudo systemctl restart mysql
Enabling MySQL at Boot
sudo systemctl enable mysql
Disabling MySQL at Boot
sudo systemctl disable mysql
These examples show how systemctl simplifies the management of required services, making it an important tool for system administrators.
Working with Targets
Targets, which are related to runlevels in the old SysV init system, are used by Systemd to manage services and system states. Targets specify the status of a system and identify the services that are in use. Let's see various instances of interacting with targets.
1. Setting the Default Target
Use the 'systemctl set-default' command to provide the default target that will be used when the system boots.
sudo systemctl set-default multi-user.target # Set the default target to multi-user
2. Changing the Current Target
You can also change the current target during runtime using 'systemctl isolate'.
sudo systemctl isolate graphical.target
User Services
For operations that don't require superuser access, Systemctl's functionality is extended by adding user-specific services. User unit files have to be made and activated in order to handle user services.
1. Creating a User Service Unit File
Create a directory for user service unit files (if it doesn't exist).
mkdir -p ~/.config/systemd/user/
Make a user service unit file containing your service configuration, such as "my-service.service."
Reload the user manager configuration.
systemctl --user daemon-reload
2. Activating and Managing User Services
Starting a User Service
systemctl --user start my-service.service
Stopping a User Service
systemctl --user stop my-service.service
Enabling a User Service at Login
systemctl --user enable my-service.service
Disabling a User Service at Login
systemctl --user disable my-service.service
These commands allow you to manage user-specific services independently of system services.
3. Dependencies and Ordering
Systemd is also excellent at managing startup order and service dependencies. Let's see how to manage these elements.
Controlling Service Startup Order
You can add dependencies for services in unit files using the 'After' and 'Before' variables. For example, you might add the lines below to "service-B's" unit file to make sure that "service-A" comes first before "service-B".
[Unit] After=service-A.service
Managing Service Dependencies
Use 'systemctl daemon-reload' to update unit files and reload the systemd management configuration.
sudo systemctl daemon-reload
4. Runtime Control and System State
You can alter the system's runtime status with Systemctl, including actions like poweroff, reboot, and emergency mode.
Rebooting the System
To reboot the system, use 'systemctl'.
sudo systemctl reboot
Shutting Down the System
To power off the system, use:
sudo systemctl poweroff
Entering Emergency Mode
Emergency mode is a minimal system state, useful for debugging and troubleshooting. To enter emergency mode, run:
sudo systemctl emergency
5. Securing Services with systemctl
The most important aspect of system management is service security. Here are some essential safety precautions for using systemctl.
- Always run services with the least privilege necessary.
- Disable unnecessary services to reduce the attack surface.
- Regularly update and patch both the operating system and installed services.
- Monitor system logs for suspicious activities and service failures.
- Implement firewall rules to restrict external access to services.
6. Troubleshooting with systemctl
Systemctl is an excellent resource for identifying problems with your Linux system and resolving them. The following are some common troubleshooting situations in which systemctl can be quite useful.
Checking Service Logs
To review the logs for a specific service, use 'journalctl'.
journalctl -u <service-name>
Investigating Failed Services
Use 'systemctl status' to find out more about a problem if a service performs strangely or fails to start.
systemctl status <service-name>
Debugging Failed Units
To enter debugging mode for a unit, use 'systemctl' with the '--full' option.
systemctl --full start <service-name>
Best Practices
Consider using the following recommended practices as you gain experience with systemctl.
Use the most recent patches and updates to keep your system and services current.
Regularly review and audit your system's enabled services.
Document your services and their dependencies.
Utilize systemd unit files to customize service behavior to your specific needs.
Use descriptive unit file names and comments for better readability.
Additional systemctl Features
List Units: You can use 'systemctl list-units' to display a list of all available units (services, targets, etc.) along with their status, making it easy to monitor the entire system.
Examples of using 'systemctl list-units' to display a list of available units (services, targets, etc.) along with their status is as follows:
1. List All Units
To list all available units and their statuses on your Linux system, simply run:
systemctl list-units
This command will provide a comprehensive list of all active and inactive units, including services, targets, sockets, and more.
2. List Only Active Units
If you want to see a list of only active units, you can use the '--state=active' option.
systemctl list-units --state=active
This will display a list of currently active units, which can be helpful for monitoring running services.
3. List Only Failed Units
To check for units that have failed, you can use the '--state=failed' option.
systemctl list-units --state=failed
This will show a list of units that are in a failed state, making it easier to identify and troubleshoot issues.
4. List Specific Unit Types
You can narrow down the list to specific unit types, such as services or targets. For example, to list only services:
systemctl list-units --type=service
This command will display a list of all service units and their statuses.
5. List Units in a Specific Target
If you want to see units that are part of a particular target, you can specify the target name. For instance, to list units within the 'multi-user.target', run:
systemctl list-units --type=target --all multi-user.target
This will provide a detailed list of units associated with the 'multi-user.target', including services required for a multi-user system.
6. List Units with Custom Output Format
You can customize the output format of the 'systemctl list-units' command using the '--output' option. For example, to display unit names and descriptions in a more concise format, use the following command:
systemctl list-units --type=service --output=unit,description
User and Group Service Management
User Groups: You can create custom user groups and manage services that belong to specific user groups using systemd. This capability is particularly useful for organizing and securing services that are shared among multiple users.
Here are some examples:
1. Creating a Custom User Group
To create a custom user group, you can use the 'groupadd' command. For instance, let's create a group called 'myusers'.
sudo groupadd myusers
This command creates a new user group called 'myusers'.
2. Adding Users to the Custom Group
You can add users to the 'myusers' group using the 'usermod' command.
sudo usermod -aG myusers username
Replace 'username' with the actual username of the user you want to add to the group. This step ensures that specific users are part of the custom group.
3. Creating a User Service Unit File
Now, you can create a user service unit file for a service that should be managed by the custom user group. For example, let's create a simple user service named 'my-service.service'.
[/term]ini
[Unit]
Description=My Custom User Service
[Service]
Type=simple
ExecStart=/path/to/your/command
[Install]
WantedBy=default.target
[/term]
This unit file defines a basic user service that can be customized based on your requirements.
4. Placing the User Service Unit File
Save the 'my-service.service' unit file in the user's home directory under '~/.config/systemd/user/'. This location is where user-specific systemd service files should be stored.
5. Reloading the User Manager Configuration
After creating the user service unit file, you need to reload the user manager configuration.
systemctl --user daemon-reload
This step ensures that the user manager recognizes the new service unit file.
6. Enabling and Starting the User Service
Now, you can enable and start the user service.
systemctl --user enable my-service.service systemctl --user start my-service.service
This ensures that the custom user service runs for users who belong to the 'myusers' group.
7. Managing Access Control with User Groups
By organizing services under custom user groups, you can control which users have access to specific services. Only users who are members of the 'myusers' group will be able to enable and start the 'my-service.service' unit.
8. Revoking Access
If you want to revoke access to the service for a particular user, you can remove them from the 'myusers' group using the 'gpasswd' command.
sudo gpasswd -d username myusers
Replace 'username' with the actual username. This will remove the user from the custom group and deny access to the associated service.
By implementing these examples, you can create and manage custom user groups and associated services using systemd. This approach allows you to organize, secure, and control access to services efficiently, making it especially valuable in multi-user environments.
Systemctl Show Command
Show Details of a Unit: The 'systemctl show' command allows you to retrieve and display detailed information about a specific unit. This information includes unit properties, dependencies, and more, aiding in in-depth analysis and troubleshooting.
Here are some examples:
1. Show Details of a Service Unit
To view comprehensive information about a specific service unit, such as 'apache2.service', you can use the 'systemctl show' command as follows:
systemctl show apache2.service
This command provides an extensive list of properties and settings associated with the 'apache2.service' unit. You can find information about the unit's description, dependencies, configuration, and more.
2. Show Specific Properties of a Unit
If you're interested in particular properties of a unit, you can specify them with the '--property' option. For example, to retrieve only the 'Description' and 'Wants' properties for 'sshd.service', run the following command:
systemctl show sshd.service --property=Description --property=Wants
This command will display only the specified properties, making it easier to focus on the information you need.
3. Show Dependencies of a Target Unit
To examine the dependencies of a target unit, such as 'multi-user.target', you can use 'systemctl show'.
systemctl show multi-user.target
This will reveal details about the target unit's dependencies, including other targets and services that must be started for it to activate successfully.
4. Show Properties of a Timer Unit
If you have a timer unit, like 'my-timer.timer', you can view its properties and configuration.
systemctl show my-timer.timer
This will provide insights into the timer's settings, schedule, and related information.
5. Show Details of a Socket Unit
For socket units like 'nginx.socket', you can use 'systemctl show' to retrieve details about the socket configuration.
systemctl show nginx.socket
This command will display information related to the socket's properties and settings.
6. Show All Properties for a Unit
If you want to view all available properties for a unit, you can use the '--all' option:
systemctl show my-service.service --all
This command will present an extensive list of all properties and their values associated with the specified unit, providing a comprehensive overview of its configuration.
By utilizing the 'systemctl show' command and its various options, you can gain valuable insights into the properties, dependencies, and configurations of systemd units. This capability is incredibly useful for in-depth analysis, troubleshooting, and fine-tuning of your system's units, ensuring optimal performance and reliability.
Conclusion
On Linux systems that employ the systemd init system, systemctl is a strong and flexible tool for managing services. You can control services, solve problems, and improve the performance and security of your system using the information you get from these systemctl command examples.
As you improve in your understanding of Linux administration, systemctl will continue to be an essential part of your toolset, allowing you to securely control the services that constitute the system's essential parts.