fbpx systemctl Command in Linux: Manage Services | Certificação Linux

systemctl Command in Linux: Start, Stop, and Manage Services

📅 01/01/2019 ⏱ 10 min ✍️ Uira Ribeiro
Linuxcommands
systemctl Command in Linux: Start, Stop, and Manage Services
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The systemctl command on Linux is the Systemd manager, which is considered the manager of "almost everything" on Linux.

The systemd system is already widely used in the most well-known distributions, especially in Kernel versions higher than 2.6. It is unlikely you will find a modern distribution that does not use systemd.

Its main advantage is faster system boot, executing services and processes in parallel. This was made possible with the arrival of new processors with multiple cores, which allow the execution of multiple threads in parallel.

Among the new features of systemd are:

  • Socket and bus activation (executes services on demand);
  • Better process parallelization;
  • Use of cgroups (control groups) instead of PIDS;
  • Supports creation of memory state images to save and restore execution states.

The group control system allows systemd to supervise processes, in order to guarantee that when a service is stopped, all child processes, grandchildren, great-grandchildren, etc. are also stopped.

With a more robust management, more CPU cycles are needed for the creation of new processes. But due to the high performance of processors, this does not seem to be a problem for systemd.

In systemd, process loading, runlevel change and boot scripts are quite different from init.

To start, Systemd treats Runlevels as Targets. Each Target can have multiple Units. 

Think of Units as objects that are read by Systemd. These objects are configuration files that define for example:

  • Object name
  • Execution prerequisites (if they depend on other Units)
  • What should be done, for example, running a specific program or service
  • What should be done after the command is started.

These files are very comprehensive, and can define a number of things depending on their type. The most common Systemd Units are:

  • Service type (service): used to manage a service or application on the server. This will include how to start or stop the service, under what circumstances it should be started automatically, and the dependency and order information of the related software. Are files with .service extension.
  • Mount point type (mount): This unit defines a mount point in the system to be managed by systemd. Are files with the .mount extension
  • Timer type (timer): defines that something will be managed by systemd, similar to a cron job, for delayed or scheduled activation. Are files with the .timer extension

See a comparative table of Runlevel (System V) and Systemd Targets:

TABLE - Linux Runlevels

RunlevelDescriptionTarget
0Level zero defines an elegant and fast system shutdown.poweroff.target
1System Maintenancerescue.target
2Multi-user text mode defaultmulti-user.target
3Multi-user text mode defaultmulti-user.target
4Multi-user text mode defaultmulti-user.target
5Multi-user graphical mode defaultgraphical.target
6Machine rebootreboot.target

The syntax of Systemd configuration files, called units, is inspired by Windows .ini files. These files are found in two directories, namely:

/usr/lib/systemd/system/: Units from installed software packages;

/etc/systemd/system/: Units installed by the administrator;

Emergency and Rescue Mode

Two extra special targets are rescue and emergency.

Rescue.target

The rescue or recovery mode can be used for "lighter" system maintenance. In this mode the system:

  • Mounts all local file systems;
  • Only the root user has permission to log into the system;
  • Network services are deactivated and only some other services are started;
  • The systemctl is-system-running command will return the maintenance status.

In this mode it is possible to run disk utilities such as fsck or xfs_repair to repair corrupted disks. This mode is useful for fixing also failures in system or service configurations.

Emergency.target

The Emergency target is for heavy maintenance, when a lot goes wrong, usually with corrupted data. In this mode the system:

  • Only mounts the root file system as read-only;
  • Similar to recovery mode, it only allows the root user to log into the system;
  • Network services are deactivated and only some other services are started;
  • The systemctl is-system-running command will return the maintenance status.

If the system enters emergency mode, there are undoubtedly serious problems. This mode is used for situations where even rescue mode does not work.

systemctl Command

$ systemctl [options] command [name]

The systemctl command is the one that runs the show of the systemd service management system. The possible commands are:

CommandAction
list-unitsLists known units
list-socketsLists socket-type units
list-timersLists timer-type units
start NAMEStarts one or more units specified in NAME
stop NAMEStops one or more units specified in NAME
reload NAMEReloads the configuration of one or more specified units
restart NAMERestarts one or more units specified in NAME
try-restart NAMERestarts units if they are running. Does nothing if they are not running
isolate NAMEStarts all specified units and their dependencies, and stops all others. Used for target change
status [NAME]Shows the status of a unit or of all if nothing is specified
show [NAME]Shows the properties of one or more units
cat NAMEShows the unit files
enable NAMEEnables a unit for loading at boot. Does not start a unit
disable NAMEDisables a unit for loading at boot. Does not stop the unit
daemon-reloadRestarts the systemd service.
mask NAMEPuts the service in limbo, preventing its execution.
unmask NAMEReturns the service from limbo, allowing it to be started.

Generally, the developer does not need to know how to create, edit or modify any unit. They just need to know how to list units, start, stop, enable, disable, view status and change Runlevel.

You may or may not specify the unit extension (.target / .service / .timer) when passing the command. Systemctl will always look for a pattern indicated in the NAME.

Status in Systemctl

This way, to check the cron service (scheduler) you can use the status command followed by the unit name cron with or without the ".service" extension:

# systemctl status cron

Or

# systemctl status cron.service
cron.service - Command Scheduler
               Loaded: loaded (/lib/systemd/system/cron.service; enabled)
               Active: active (running) since Mon, 28 May 2012 18:09:57
               Main PID: 673 (cron)
               CGroup: name=systemd:/system/cron.service
               + 673 /usr/sbin/cron -n

Stop a service with systemctl

To stop a service:

# systemctl stop cron.service

See the service status when stopped:

# systemctl status cron.service
cron.service - Command Scheduler
               Loaded: loaded (/lib/systemd/system/cron.service; enabled)
               Active: inactive (dead) since Mon, 28 May 2012 18:18:26
               Main PID: 673 (code=exited, status=0/SUCCESS)
               CGroup: name=systemd:/system/cron.service

Start a service with systemctl

To start a service:

# systemctl start cron.service

Restart a service with systemctl

To restart a service:

# systemctl restart cron.service

Enable a service at boot with systemctl

To enable a service during system boot, use the enable command:

# systemctl enable cron.service

Disable a service with systemctl

To disable a service during system boot, use the disable command:

# systemctl disable cron.service

Change the runlevel with systemctl

To change the runlevel to text mode (runlevel 3), use the isolate command

# systemctl isolate multi-user.target

To change the runlevel to graphical mode (runlevel 5):

# systemctl isolate graphical.target

To see which runlevel is currently running, you can use the get-default command:

$ systemctl get-default
graphical.target

Change the default runlevel with systemctl

To change the default runlevel there are two ways:

The first is to manually remove and recreate the symbolic link:

a) To remove the symbolic link:

# rm /etc/systemd/system/default.target

b) To set runlevel 3 as default, recreate the symbolic link:

# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

The second way is to use systemctl to do this for you, with the set-default command:

# systemctl set-default multi-user.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target.

To see which units a given service depends on, you can use the list-dependencies command:

$ systemctl list-dependencies apache2apache2.service● ├─-.mount● ├─system.slice● └─sysinit.target●   ├─apparmor.service●   ├─dev-hugepages.mount●   ├─dev-mqueue.mount●   ├─keyboard-setup.service●   ├─kmod-static-nodes.service( ... )

It is important to say that when a given service is started by systemd, the manager will also start all the dependencies that the given service requires.

For example, if the system.slice service is stopped and disabled, and the administrator starts the apache2 service, the system.slice service will be started, so that the apache2 service works correctly.

Mask a service with systemctl

To prevent a service from being executed, even if it is disabled and is part of a dependency of some unit, the mask command can be used:

# systemctl mask apache2Created symlink /etc/systemd/system/apache2.service → /dev/null.

This way, systemctl will create a symbolic link of the service to limbo /dev/null. Thus, the service will not be started in any way, even if it is a dependency of another unit:

# systemctl start apache2Failed to start apache2.service: Unit apache2.service is masked.

To return the service, the unmask command can be executed, returning the unit from exile:

# systemctl unmask apache2Removed /etc/systemd/system/apache2.service.

Restart systemd with systemctl

Every time a unit is created or modified, it is necessary to restart the systemd service with the daemon-reload command:

# systemctl daemon-reload

Systemd maintains some compatibility with System V, through a script that emulates the service command, which actually executes systemctl commands.

Disabling Unnecessary Services

An important task of the administrator is to disable unnecessary services, especially network services.

The first step is to check which services are running with the command:

$ systemctl status

This command displays a list of all running services.

You can also list open ports and their respective processes using the netstat or ss commands:

# netstat -tunlp

or

# ss -tunlp

To stop unnecessary services, use the command:

# systemctl stop nomedoservico

And to disable at the system boot process:

# systemctl disable nomedoservico

If the distribution still uses the System V Init Daemon, running services can be viewed with the command:

$ service --status-all

Or with the command:

$ chkconfig --list

To stop services, the command can be used:

# service nomedoservido stop

And to disable:

# chkconfig nomedoservico off

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp