Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Systemctl command to manage system services

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use the Systemctl command to manage system services. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Systemctl is systemd's tool for managing systems and managing services. Many modern Linux distributions, such as Ubuntu, Debian, Fedora, Linux Mint, OpenSuSE, Redhat, use systemd as the default init system.

systemctl enables you to start, stop, reload, restart services, list service units, check service status, enable/disable services, manage runlevels, and manage power. This article shows how to use the systemctl command to manage systemd services in Linux.

* Use systemctl command Start/Stop/Restart/Reload service

When using systemctl to start a service, the command format is systemctl start [service-name]. For example, start firewalld service:

[root@localhost ~]# systemctl start firewalld

In contrast to the service command in older versions of linux, the systemctl start command does not print anything.

To stop the service, use systemctl stop [service-name]. For example, stop firewalld service:

[root@localhost ~]# systemctl stop firewalld

To restart the service, use systemctl restart [service-name], for example:

[root@localhost ~]# systemctl restart firewalld

To reload a service's configuration (such as ssh) without restarting it, use systemctl reload [service-name], for example:

[root@localhost ~]# systemctl reload sshd

*systemctl checks service status

To see if the service is running, we can use systemctl status [service-name] to check.

[root@localhost ~]# systemctl status firewalld

* Check if the service is set to boot

To enable services at boot time, use systemctl enable [service-name], for example:

[root@localhost ~]# systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

Similarly, disable enables services when unbooting:

[root@localhost ~]# systemctl disable httpd.service

You can use the is-enabled option to check whether the service is enabled at boot. Run:

[root@localhost ~]# systemctl is-enabled httpd.service

The output content enabled means that the service is started when booting, disabled means that the service is not started when booting.

*systemctl List Units To list all active units, use the list-units option.

[root@localhost ~]# systemctl list-units

To list all active services, run:

[root@localhost ~]# systemctl list-units -t service

* Restart and shutdown the system using systemctl

Like poweroff and shutdown commands, systemctl can shut down a system, restart it, or go to sleep. Shutdown:

[root@localhost ~]# systemctl poweroff

Restart:

[root@localhost ~]# systemctl reboot

System hibernation:

[root@localhost ~]# systemctl hibernate

* Manage remote systems using systemclt

In general, all of the systemctl commands described above can be used to manage remote hosts through the systemctl command itself. This will use ssh to communicate with remote hosts. As follows:

[root@localhost ~]# systemctl status httpd -H root@192.168.0.12

The-H option specifies the username and password of the remote host.

* Managing Targets

Systemd has the concept of Targets, whose purpose is similar to run-levels in sysVinit systems. The runlevels in sysVinit are primarily numeric (0, 1, 2,-6). Here are the runlevels in sysVinit and their corresponding targets in systemd:

0 runlevel0.target, poweroff.target 1 runlevel1.target, rescue.target 2,3,4 runlevel2.target, runlevel3.target,runlevel4.target, multi-user.target 5 runlevel5.target, graphical.target 6 runlevel6.target, reboot.target

If you want to see the current run level, you can use the following command:

[root@localhost ~]# systemctl get-default multi-user.target

To set the default runlevel to graphical, command:

[root@localhost ~]# systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

To list all active targets, use the following command:

[root@localhost ~]# systemctl list-units -t target

* Other commands for systemd tool

journalctl log collection

systemd has its own logging system called journald. It replaces syd in sysVinit.

[root@localhost ~]# journalctl

To view all boot messages, run the command journalctl -b

[root@localhost ~]# journalctl -b

The following command traces the system log in real time (similar to tail -f):

[root@localhost ~]# journalctl -f

Query the duration of the system startup process

[root@localhost ~]# systemd-analyze Startup finished in 497ms (kernel) + 1.836s (initrd) + 6.567s (userspace) = 8.901s

The last time the system started was 8.901 seconds. View service startup time:

[root@localhost ~]# systemd-analyze blame

hostnamectl command

View host name:

[root@localhost ~]# hostnamectl

Thank you for reading! About "how to use Systemctl command to manage system services" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report