systemctl ubuntu

Ubuntu uses the systemd system and service manager as the init system. This means systemd runs as the first process at boot (PID 1) and manages all further user processes. Systemd, in turn, is managed using the systemctl tool.

Systemctl is commonly used to check the status of services or stop, start, enable, or disable them. But you can also use it to monitor and control the overall system state.

Listing systemd Units with Systemctl

systemd uses a dependency system of ‘units’, which are basically different types of resources required for system boot-up and maintenance. The most important of these are service units that manage daemons and sub-processes.

There are 11 unit types in total, and the default behavior of systemctl is to list all units currently loaded by systemd. It’ll list everything from socket and device units to mount units. 

systemctl

If you want to list service units only, you can specify the unit type as service. 

systemctl -l --type=service

Additionally, you can include the all option if you want to view inactive units as well. 

systemctl -l --type=service -all
ubuntu-active-services

In the output, 

  • UNIT is the systemd unit name
  • LOAD defines whether the unit was properly loaded
  • ACTIVE shows the high-level unit state
  • SUB shows a low-level (more exact) description of the unit state
  • DESCRIPTION shows the unit description

The state option is also very useful as it allows you to filter the extensive output and list services according to their status. The possible service states are active, inactive, activating, deactivating, failed, not-found, and dead

For instance, to list dead service units only, you could use

systemctl -l --state=dead

Checking Service Details

The list-units flag provides us with an overview of the service states, but often you’ll need details on a specific service instead. 

For instance, you may want to check the status of your NGINX server. For this, you would use the status command.   

systemctl status nginx

If you want to check a service’s properties instead, you can use the show command.

systemctl show nginx

This’ll list over 200 properties though, so doing this every time isn’t ideal. If you remember the exact property you want to check, you can search for it with the -p flag like so

systemctl show nginx -p UMask

Managing Services with Systemctl

Now that you know how to list services and get detailed info on them, let’s talk about changing service states.

Enable and Start Services

First of all, you can enable a service like so

systemctl enable nginx

This’ll create the necessary symlinks and reload the system manager configuration so that the service auto-starts at boot. But keep in mind that this won’t activate the service for the current session. For that, you must include the now switch.

systemctl enable nginx --now

Or, you can use the start command instead.

systemctl start nginx

Disable and Stop Services

The opposite is also true for the aforementioned points. You can disable a service like so

systemctl disable nginx

This’ll remove the symlinks made using enable or link and the service won’t start at boot. If you want to stop the service immediately, you can include the now option.

systemctl disable nginx --now

Or, you can use the stop command.

systemctl stop nginx

Restart and Reload Services

The restart command stops and then starts the specified service units. If the service is currently not yet running, it’ll activate the service the same as the start command.

systemctl restart nginx

We’ve been using an NGINX server in our examples. If you’re dealing with a similar program that can reload its configurations without restarting the service, you can use the reload command instead of restart.

systemctl reload nginx

Changing the System State

Systemd uses targets similar to how traditional init systems used runlevels to define the operating state.

Run levelsystemd TargetDescription
Runlevel 0poweroff.targetTurn off the system.
Runlevel 1rescue.targetSingle-user mode.
Runlevel 3multi-user.targetMulti-user non-graphical mode.
Runlevel 5graphical.targetMulti-user graphical mode.
Runlevel 6reboot.targetReboots the device.
emergencyemergency.targetEmergency shell.

You can check the default target used by systemd when booting with

systemctl get-default

There are a couple of ways to change targets. If you want to change the default target, you can use set-default. For instance, if you wanted to boot with a graphical login, you could set it like so

systemctl set-default graphical.target

The second method is switching runlevels using isolate. For instance, if you need to switch to rescue mode, you could use

systemctl isolate rescue.target

Systemctl supports shortcuts for certain commands like rescue, so you can also directly use

systemctl rescue

Other useful shortcuts include poweroff and reboot.

systemctl poweroff
systemctl reboot

Bonus Tip: You can also use the reboot command to directly boot to the firmware setup page.

systemctl reboot --firmware-setup
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.