The “systemctl” Command in Linux [With 15+ Examples]

The “systemctl” command in Linux is a tool that queries or sends control commands to the system manager. This tool is invaluable for monitoring, and maintaining your system services and troubleshooting damages. With “systemctl”, you can start, stop, enable, disable, restart, reload, or even check the status of services and units.

In this article, you will learn all about the “systemctl” command in Linux with 17 practical examples.

Syntax of “systemctl” Command

The basic syntax of the “systemctl” command is as follows:

systemctl [command] [service]

Here,

  • [command] indicates the action you want to execute, for example: start, stop, check status, enable, disable, etc.
  • [service] refers to the service you want to act on.

The “systemctl” Commands Cheat Sheet

Before going to the detailed examples, here is a quick overview of some of the most used systemctl command options with their functions:

Option Function
systemctl halt Halts server
systemctl reboot Reboots server
systemctl poweroff Shuts down server
systemctl get-default Displays default target
systemctl status my_service Checks the current status of a service
journalctl -u my_service Checks logs of services
systemctl is-active my_service Checks active status of a certain service
systemctl mask my_service Masks a service
systemctl –type=service –state=running Lists services and daemons
systemd-analyze blame Shows service start time
systemctl list-unit-files Lists all unit files
systemctl edit my_service.service Edits unit files

17 Examples Using “systemctl” Command in Linux

Here are 17 examples of using “systemctl” Command to manage systems, services, and unit files in Linux:

System Management

The system management in Linux systemd environment is about the OS configurations. Here are a few commands to effectively manage your system:

1. Halt, Shutdown, and Reboot Server

In Linux, the systemctl command can halt, shut down, and reboot your servers and services. The “reboot” subcommand is useful for restarting the entire system and refreshing the service configurations. Additionally, the “shutdown” and “halt” subcommands give you more specific control.

  1. To halt your server, run the following command:
    systemctl halt

    This command halts the system, bringing it to a stop where it is safe to power off the machine.

  2. To reboot your server, use the command below:
    systemctl reboot

    It reboots the system, shutting down all running services and processes, unmounting filesystems, and then initiating the reboot process.

  3. To power off the servers and services, you can run the below code:
    systemctl poweroff

    This command stops all processes and services and powers off the hardware once the shutdown process is complete.

2. Displaying Default Target

You can display your default running target in Linux using the systemctl command.

A target is a unit file that indicates a system state in the systemd environment. The graphical.target and multi-user.target are the most common default targets in systemd-based Linux distributions.

To display your current default target, run the following command:

systemctl get-default

Displaying default target using systemctl

The command shows the system boots into a graphical user interface (GUI) mode.

3. Changing Default Target to GUI

To change the system’s current default target to the GUI, you can use the systemctl command. For example, run the following command:

systemctl set-default graphical.target

Changing default target to GUI using systemctl

As a result, the command sets graphical.target (GUI) as the default target.

4. Switching System State to multi-user.target

To switch system state to multi-user.target, use the systemctl command followed by the isolate option.

The multi-user.target is the state where the system takes multiple non-graphical user sessions. Here is the command to switch system state to multi-user.target:

systemctl isolate multi-user.target

Switching system state to multi-user

The command changes the operating mode of the system to multi-user access without graphical interference.

5. Switching System State to Rescue Mode

Further, you can switch the system state to rescue mode using the systemctl command.

The rescue mode is a boot option that uses basic commands and tools to run a minimal Linux environment. It restricts unnecessary services and daemons to start in your system.

To switch your current system state to rescue mode, run the command below:

systemctl rescue

Switching current system state to rescue mode

Thus, your system switches to rescue mode.

Service Management

A service is a systemd utility that recognizes and manages the operating system. Here are a few commands to manage the available services in the system:

6. Checking Current Status of a Service

To check the current status of a provided service using systemctl, type the following command:

systemctl status cups

Checking the current status of a service

Hence, you will get a snapshot of the status and basic information of the specified service on your system.

7. Starting and Stopping Services

You can start, stop, restart, or reload services using systemctl command. For example:

    1. To initiate or start systemd services by using a service unit file, use the following command:
      systemctl start my_service
    2. To stop a specific service, run the below command:
      systemctl stop my_service
    3. To restart a service without the need to issue separate stop and start commands, use the following restart command:

      systemctl restart my_service
    4. To reload a service to change and configure files without the need to restart, run this command:
      systemctl reload my_service
    5. To let the system take the appropriate action if you are unsure whether to reload or restart, run the below command:

      systemctl reload-or-restart my_service

8. Enabling and Disabling Services on Boot

You can enable systemd services using the systemctl command. You must enable your services if you want them to start automatically at boot.

Run the following command to enable services:

sudo systemctl enable cron.service

Running enable command with systemctl

Therefore, you enable cron to start automatically at boot.

Alternatively, to disable your systemd services from starting automatically at boot, run:

sudo systemctl disable cron.service

Disabling services with systemctl disable command

Running this command disables the cron service to start automatically during boot.

9. Checking Active Status of a Particular Service

You can check if a certain service is running or not using the systemctl command. Here is the command:

systemctl is-active apache

Checking active status of services

As you can see, the command returns an inactive status as the service Apache is currently unavailable in the system.

10. Masking Services

You can mask or unmask any current services in your distribution by using the systemctl command.

Masking is an operation in Linux that prevents the services or unit files from being started manually or by any other service.

To mask services, use the following command:

systemctl mask cups

Masking services using systemctl command

The above command marks the CUPS service as masked.

Alternatively, unmasking is an operation that sets up default permissions to recently created files or folders.

To unmask a service, use the following command:

systemctl unmask cups

Unmasking services with systemctl command

The unmask command marks the specified CUPS service as unmasked.

11. Listing Services and Daemons

To list the running services on your Linux server, use the command below:

systemctl --type=service --state=running

Listing services and daemons with systemctl command

Hence, the list of the running services appears.

Note: You don’t need to use sudo to list the services. You need sudo when you are making changes to the services.

12. Checking Enabled Status

You can check if a service is set to automatically start on boot using the systemctl command. It shows an enabled or disabled status based on the current configuration of the service.

To check an enabled status, run this command:

systemctl is-enabled cups

Checking enabled status of services

13. Displaying Service Start Time

You can display the service start time by filtering the systemd-analyze command. It is particularly useful to identify which services are slowing down the system’s startup process.

Here is the command to  display the service start time:

systemd-analyze blame

Displaying service start time using systemctl

As you can see, the command returns a list of services with their start-up time.

Unit File Management

Here are a few examples of using the systemctl command for unit file management:

14. Listing All Unit Files

You can list all unit files using the systemctl command with the list-unit-files subcommand. For example, run  the below command:

systemctl list-unit-files

Listing all unit files

OUTPUT ANALYSIS
  • State: refers to the current state of the unit file with components like enabled, disabled, generated, or static.
    • Enabled: means the unit starts automatically during boot or as needed.
    • Disabled: means it doesn’t.
    • Static indicates it’s enabled but can’t be disabled by the administrator.
  • VENDOR PRESET: indicates whether the unit file is provided by the system vendor or by the system administrator.
    • Enabled: means the unit file is set to start automatically during boot or as needed, according to the vendor’s configuration.
    • Disabled state means it won’t start automatically based on the vendor’s settings.

15. Creating Unit Files

To create a unit file and manage it using a text editor and the systemctl command follow the following simple steps:

      1. Create a new unit file using the below command:
        sudo nano /etc/systemd/system/new_service.service

        Creating unit file using nano

      2. Enter your password and press ENTER to open the nano text editor.
      3. Paste the following configurations to the file and press CTRL + O to write out:
        [Unit]
        Description=Example Service
        After=network.target
        [Service]
        Type=simple
        ExecStart=/usr/bin/python3 /path/to/your_script.py
        Restart=always
        [Install]
        WantedBy=multi-user.target

        Pasting system configurations to file

      4. Press CTRL + X to exit the text editor.
      5. Now, reload your systemd to end the process using the below command:
        sudo systemctl daemon-reload

        Reloading system to end the creating process using systemctlAs a result, you have created and configured a new unit file in your systemd service. You can check the current status of your new unit service as shown in the above image.

16. Viewing Unit Files

You can view the content of your unit files using the systemctl command. So that you don’t necessarily need to navigate to the unit files to see the contents anymore. Run the below simple command to view unit files:

systemctl cat my_service.service

Viewing unit files using systemctl

Upon execution, you can view and inspect the configuration of the existing unit files.

17. Editing Unit Files

You can edit a unit file without searching for it on the server using systemctl along with the edit command. For example:

  1. Run the command below to edit a service without directly editing the original unit file:
    sudo systemctl edit my_service.service

    Using sudo systemctl command to edit unit files

  2. Enter your password and press ENTER to enter the file to edit:Entering file to edit without changing original file

    Subsequently, this command opens a temporary override file for the service unit in the default text editor. It allows you to modify specific settings or add additional configuration options without directly editing the original unit file.

  3. If you need to edit using the file itself instead of creating a snippet, run this command:
    sudo systemctl edit --full my_service.service

    Editing original file using systemctl command

  4. Enter your password and hit ENTER:Directly entering the original file to edit

    Consequently, this command opens the service unit file in the default text editor and allows you to view and edit the entire contents of the file. It provides full access to all settings and configurations of the service.

Conclusion

In summary, you have covered the definition, characteristics and practical examples of the “systemctl” command in Linux. Hopefully, you have an improved understanding of how the “systemctl” command manages the task management of the server. Now, you can interact and oversee your systemd system and create, edit, or remove unit files with the service manager.

People Also Ask

What is the use of systemctl command in Linux?

The uses of systemctl command in Linux are to start, stop, check status, restart, enable, disable, and other advanced actions on various services.

What is the difference between systemctl and service command in Linux?

The difference between systemctl and service command is that the systemctl command interacts directly with the systemd service manager to control services. On the other hand, the service command typically runs init scripts to manage services.

What is the difference between stopping and killing a service in systemctl command?

The difference between stopping and killing a service is that the “kill” command kills processes of a specific unit. Contrary to “kill”, the “stop” command stops or deactivates one or more specified units.

How to check if a service is enabled with systemctl command?

To check if a service is enabled, you can use systemctl along with the is-enabled command. Here is the command: systemctl is-enabled apache2

This command shows if the apache2 service is enabled, disabled, or static.

How to see full log from systemctl status service?

To see the full log from a systemctl status service, use the journalctl command followed by the -u option. Run the below simple command: journalctl -u my_service.service

How can I remove unit files in Linux?

To remove unit files in Linux, type this command: rm -r /etc/systemd/system/my_service.service.d

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Avatar photo

Yousuf Shovon is a Linux Content Developer Executive (LCDE) at SOFTEKO. Previously, he worked for ExcelDemy, writing over 60+ articles on Excel. His goal is to write quality content for users to read and learn. In the future, he also wants to build various projects with LinuxSimply. He completed his graduation and post-graduation in Information Technology from Jahangirnagar University. Yousuf finds joy in solving diverse Linux-related problems to gain an all-around experience in Linux. Read Full Bio

Leave a Comment