Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

systemctl in Unix

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

systemctl is used to examine and control the state of “systemd” system and service manager. systemd is a system and service manager for Unix-like operating systems (most of the distributions, not all). As the system boots up, the first process created, i.e. init process with PID = 1, is systemd system that initiates the userspace services. 

Syntax of `systemctl` command in Unix

systemctl [command] [service]

[command] = The action we want to perform (start, stop, enable, disable, etc.)

[service] = The name of the service we want to perform the action on.

Services in `systemctl`

1) Starting and Stopping Services

Starting Services

Syntax:

systemctl start [service]

Example:

If we want to start our SSH service.

systemctl start sshd

Stopping Services

Syntax:

systemctl stop [service]

Example:

If we want to stop our SSH service.

systemctl stop sshd

2) Enabling and Disabling Service

Enabling Services

Syntax:

systemctl enable [service]

Example:

If we want to enable our firewall service.

systemctl enable firewalld

Disabling Services

Syntax:

systemctl disable [service]

Example:

If we want to disable our firewall service.

systemctl disable firewalld

3) Viewing the Staus of Services

Viewing Status of Services

Syntax:

systemctl status [service]

Example:

If we want to see the status of our firewall service.

systemctl status firewalld

4) Restarting and Reloading Services

Restarting Services

Syntax:

systemctl restart [service]

Example:

If we want to restart our SSH service.

systemctl restart sshd

Reloading Services

Syntax:

systemctl reload [service]

Example:

If we want to reload our Apache service.

systemctl reload httpd

5) Masking and Unmasking Services

Masking Services

Syntax:

systemctl mask [service]

Example:

If we want to mask our MySQL service.

systemctl mask mysqld

Masking a service prevents it from being started or enabled, even if it is required by other services.

Unmasking Services

Syntax:

systemctl unmask [service]

Example:

If we want to unmask our MySQL service.

systemctl unmask mysqld

Unmasking a service allows it to be started or enabled again.

6) Changing the Default Target

Syntax:

systemctl set-default [target]

Example:

If we want to change the default target to graphical.target (which will start the graphical user interface).

systemctl set-default graphical.target

The default target determines which system services are started when the system boots up.

7) Listing Unit Files

Syntax:

systemctl list-unit-files

This command lists all the unit files available on the system, including both enabled and disabled unit files.
 

8) Masking and Unmasking Unit Files

Masking Unit Files

Syntax:

systemctl mask [unit-file]

Example:

If we want to mask the SSH unit file.

systemctl mask sshd.service

Masking a unit file prevents it from being started or enabled, even if it is required by other services.

Unmasking Unit Files

Syntax:

systemctl unmask [unit-file]

Example:

If we want to unmask the SSH unit file.

systemctl unmask sshd.service

Unmasking a unit file allows it to be started or enabled again.

Options available in `systemctl` command in Unix

Options                                                      Description Syntax
–version This option displays the version number of the systemctl command.
systemctl --version
–help This option displays the help manual for systemctl, including a list of available options and commands
systemctl --help
–type The argument in this case should be comma-separated list of unit types such as service and socket.
systemctl --type=service
–all This option lists all available units, including those that are inactive.
systemctl --all
–failed This option lists all units that have failed.
 
systemctl --failed
–user talk to service manager of calling user, instead of system.
systemctl --user
–force This option forces the service to start or stop, even if it has dependencies that are not yet started or stopped.
systemctl stop --force httpd.service

–no-block

This option starts or stops the service without blocking the shell, allowing the user to continue to use the shell while the service is starting or stopping.
systemctl start --no-block httpd.service
–state

This option is used to filter the output based on the specified unit state. You can specify one or more-unit states separated by commas, such as active, inactive, failed, and activating.

 For example: to list all the failed units.

systemctl list-units --state=failed
-r, –recursive

This option is used to show units of local containers as well.

 For example: to list all units including those in local containers.

systemctl list-units --recursive
–show-types

This option is used to show the types of sockets along with showing sockets. 

For example: to show the types of all sockets.

systemctl list-sockets --show-types
–job-mode=

This option controls how to deal with already queued jobs in case of queuing a new job. There are three available modes:

replace: replace already queued jobs with the new one.
fail: cancel the new job and return failure if there are already queued jobs.
isolate: queue the new job and isolate it from the other jobs.

For example: to use the replace mode.

systemctl isolate graphical.target --job-mode=replace
-i, –ignore-inhibitors

This option is used to ignore inhibitor locks when requesting a system shutdown or sleep state. 

For example: to ignore inhibitor locks when requesting a system shutdown.

systemctl poweroff --ignore-inhibitors
-q, –quiet

This option is used to suppress the printing of results of various commands and the hints about the truncated lines. 

For example: to reload the systemctl daemon without showing any output.

systemctl daemon-reload --quiet
–no-wall

This option is used to not send a wall message before power-off, halt, or reboot. 

For example: to halt the system without sending a wall message.

systemctl halt --no-wall

Conclusion

In this article we will understand the use of `systemctl` command in Unix which is used to examin and control the state of the `systemd` system and service manager. We have covered various options and commands that are available in `systemctl` command. Like starting and stopping services, enabling and disabling services, viewing the status of services, restarting and reloading services, masking and unmasking services, changing the default target, listing unit files, and masking and unmasking unit files. We have also discussed about various options available with `systemctl` command, such as `–version`, `–help`, `–type`, `–all`, `–failed`, `–user`, `–force`, `–no-block`, `–state`, `-r, –recursive`, `–show-types`, `–job-mode`, and `-i, –ignore-inhibitors`.
 


My Personal Notes arrow_drop_up
Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads