Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 1.4 - System Services

In the CompTIA Linux+ exam, understanding how to configure and manage system services is essential. System services are background processes that provide specific functionalities to the Linux system. This guide will cover important commands using systemctl, which is a command-line utility for controlling systemd, the default init system used in most modern Linux distributions.

Systemctl

systemctl is a powerful command that allows you to control various aspects of system services. Here are some commonly used commands with systemctl:

Stop

To stop a service, use the following command:

systemctl stop <service_name>

Example:

systemctl stop apache2

This command will stop the Apache web server.

Start

To start a service, use the following command:

systemctl start <service_name>

Example:

systemctl start nginx

This command will start the Nginx web server.

Restart

To restart a service, use the following command:

systemctl restart <service_name>

Example:

systemctl restart mysql

This command will restart the MySQL database server.

Enable

To enable a service to start automatically at boot, use the following command:

systemctl enable <service_name>

Example:

systemctl enable apache2

This command will configure the Apache web server to start automatically on system boot.

Disable

To disable a service from starting automatically at boot, use the following command:

systemctl disable <service_name>

Example:

systemctl disable nginx

This command will prevent the Nginx web server from starting automatically on system boot.

Mask

To mask a service, preventing it from being started, stopped, or enabled, use the following command:

systemctl mask <service_name>

Example:

systemctl mask postgresql

This command will mask the PostgreSQL database server.

Status

To check the status of a service, use the status subcommand followed by the service name. This will provide information about whether the service is running or not, along with additional details. For example, to check the status of the Apache web server, you would run:

systemctl status apache2

The output of the status subcommand will vary depending on the state of the service. Here are some possible outputs:

When a service is stopped:

  ● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2023-05-15 10:25:36 UTC; 2s ago
    Process: 12345 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 1137)
     Memory: 0B
     CGroup: /system.slice/apache2.service

  May 15 10:25:36 server systemd[1]: Started The Apache HTTP Server.

When a service is started:

  ● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-05-15 10:30:12 UTC; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 12345 (apache2)
      Tasks: 55 (limit: 1137)
     Memory: 6.5M
     CGroup: /system.slice/apache2.service
             ├─12345 /usr/sbin/apache2 -k start
             ├─12346 /usr/sbin/apache2 -k start
             └─12347 /usr/sbin/apache2 -k start

  May 15 10:30:12 server systemd[1]: Started The Apache HTTP Server.

When a service is disabled:

  ● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled)
     Active: inactive (dead)

  May 15 10:33:21 server systemd[1]: Stopped The Apache HTTP Server.

When a service is enabled:

  ● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-05-15 10:35:41 UTC; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 12345 (apache2)
      Tasks: 55 (limit: 1137)
     Memory: 6.5M
     CGroup: /system.slice/apache2.service
             ├─12345 /usr/sbin/apache2 -k start
             ├─12346 /usr/sbin/apache2 -k start
             └─12347 /usr/sbin/apache2 -k start

  May 15 10:35:41 server systemd[1]: Started The Apache HTTP Server.

When a service is masked:

  ● apache2.service
     Loaded: masked (/dev/null; bad)
     Active: inactive (dead)
     since Mon 2023-05-15 10:38:10 UTC; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/

  May 15 10:38:10 server systemd[1]: Stopped The Apache HTTP Server.

In the above examples, you can observe the different states and information provided by the systemctl status command for various service scenarios. This command is helpful for troubleshooting and monitoring services on your Linux system.

Mask vs Disable

While the disable command prevents a service from starting automatically at boot, the mask command goes a step further and prevents the service from being started manually as well. It creates a symbolic link from the service unit file to /dev/null, effectively disabling the service entirely. Use disable if you want to enable manual starting of the service in the future. Use mask if you want to completely disable the service.

Conclusion

Configuring and managing system services is an essential skill for Linux system administrators. Understanding how to control services using systemctl allows you to effectively manage the functionality of your system. With the commands covered in this guide, you can start, stop, restart, enable, disable, and mask services as needed.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.