Mastering Basic Linux Commands: Managing Linux Services with  systemctl

Mastering Basic Linux Commands: Managing Linux Services with systemctl

While browsing Stack Overflow, I stumbled upon a post where a user was struggling to install a package required for an application. A fellow user commented that Kali Linux might not be the best distribution for a beginner and that it was too complicated. However, the solution to the issue was as simple as running

sudo apt update && apt upgrade -y

Don't let people discourage you. I find the responses on Stack Overflow to be just as frustrating as they are useful.

If you are new to Linux like that user was, you might be wondering how things work under the hood and how to manage the services that run in the background. This post stems from the use case I mentioned a moment ago. This is where Systemctl comes in. Systemctl is a command-line utility that allows you to manage system services on your Linux machine. It is a powerful tool that can help you monitor, start, stop, enable, and disable services.

Systemctl works by interacting with unit files, which are responsible for describing the properties and dependencies of a service. By using commands like "systemctl status", "systemctl start", "systemctl stop", "systemctl enable", and "systemctl disable", you can easily manage the services running on your Linux machine. For example, "systemctl status apache2" will show you the status of the Apache web server service.

In addition to managing system services, Systemctl also allows you to view logs for each service using the "journalctl" command. This can be helpful in debugging issues related to a service.

Journalctl is a powerful command-line utility that allows you to view and manage the logs generated by the systemd journal, a logging system used by many modern Linux distributions. Here are some basics about using journalctl:

  • To view the entire journal, simply run journalctl with no arguments. This will display the most recent entries in the journal, with the newest entries appearing at the bottom.

  • You can use the -u option to filter the journal by a specific systemd unit, such as a service or timer. For example, journalctl -u apache2 will show only the log entries for the Apache web server.

  • You can also use the -f option to "follow" the journal in real-time, similar to the tail command. This can be useful for monitoring the output of a particular process or service as it runs.

  • Other useful options include -n to specify the number of log entries to display, -r to reverse the order of the entries (oldest first), and --since and --until to filter by a specific time range.

Overall, journalctl is a powerful tool for troubleshooting and monitoring Linux systems, allowing you to quickly identify and diagnose issues with system services and applications.

Back to Systemctl:

What is Systemctl?

Systemctl is a tool for controlling the state of systemd, which is the system and service manager for Linux. Systemd is responsible for managing the core functionality of your Linux machine, including starting and stopping services, managing user sessions, and much more. Systemctl allows you to interact with systemd and control the state of system services.

Why is Systemctl important?

Systemctl is important because it allows you to manage the services that run in the background on your Linux machine. Services are programs that run in the background and provide functionality to other programs. For example, the Apache web server is a service that provides web hosting functionality to other programs. With Systemctl, you can start, stop, restart, and enable or disable services as needed.

Basic Systemctl commands

Here are some basic commands to get started with Systemctl:

  • Start a service: sudo systemctl start SERVICE_NAME

  • Stop a service: sudo systemctl stop SERVICE_NAME

  • Restart a service: sudo systemctl restart SERVICE_NAME

  • Check the status of a service: sudo systemctl status SERVICE_NAME

  • Enable a service to start at boot: sudo systemctl enable SERVICE_NAME

  • Disable a service from starting at boot: sudo systemctl disable SERVICE_NAME

Note:
When you run systemctl enable apache2, it creates a symbolic link from the /etc/systemd/system/multi-user.target.wants/ directory to the apache2.service file in the /lib/systemd/system/ directory. This enables the Apache2 service to start automatically when the system boots up. This is handy if you are running a server and in this case, a web server and you want the site to come back online on its own if the power goes out and your server restarts.

The systemctl enable command is used to enable a service or unit to be started automatically when the system boots up. It does not start the service immediately. To start the service, you need to use the systemctl start command.

For example, if you want to start the Apache web server service, you can use the following command:

sudo systemctl start apache2

Managing services with Systemctl

In addition to the basic commands above, Systemctl provides a number of options for managing services. Here are a few examples:

  • View all services: sudo systemctl list-units --type=service

  • Reload configuration files: sudo systemctl daemon-reload

  • View the journal (log) for a service: sudo journalctl -u SERVICE_NAME

  • Kill a service: sudo systemctl kill SERVICE_NAME

  • View the dependencies for a service: sudo systemctl list-dependencies SERVICE_NAME

For example, if you want to view the journal for the Apache web server service, you can use the following command:

sudo journalctl -u apache2

Wrapping up

Systemctl is a powerful tool for managing system services on your Linux machine. With just a few simple commands, you can start, stop, and manage services with ease. Whether you're a beginner or an experienced Linux user, Systemctl is an essential tool for managing your system services.

Did you find this article valuable?

Support Matthew Hard by becoming a sponsor. Any amount is appreciated!