Linux 很多内置或者第三方软件都支持使用systemctl 命令控制: 启动、停止、开机自启动。 systemctl管理的软件也叫做 “服务”。
语法: systemctl [start stop status enable disable ] 服务名
中间选项的含义为:
start 启动 stop 关闭
status 查看状态 enable 开机自启动 disable 关闭开机自启动
很多Linux内置服务,例如:
NetworkManager 主网络服务
network 副网络服务
firewalld 防火墙服务
sshd ssh服务
例如我们可以查看防火墙的状态
[zhuxiao@localhost ~]$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor
preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
可以看到Active下是inactive的状态,这是因为我之前将其关闭了
我们可以通过start命令将其开启,但该操作需要在root用户下执行
执行完成之后可以看到这里防火墙是运行了的。
同样也可以通过stop将其关上,同样是在root用户下运行,这里不在演示。
同样,通过yum命令安装的软件(服务)也可以通过systemctl控制,例如我安装一个ntp软件,对他进行查看、启动、开机自启动、关闭开机自启动、查看。
[root@localhost zhuxiao]# yum install ntp //下载ntp软件
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.nju.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.nju.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost zhuxiao]# systemctl status ntpd
//查看ntp状态,ntp注册服务名为ntpd所以 此处服务名称要写为ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead) //不运行
[root@localhost zhuxiao]# systemctl start ntpd //运行服务
[root@localhost zhuxiao]# systemctl enable ntpd //开机自启动服务
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost zhuxiao]# systemctl status ntpd //查看服务状态
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2023-07-26 09:44:04 CST; 55s ago //运行
Main PID: 65310 (ntpd)
CGroup: /system.slice/ntpd.service
└─65310 /usr/sbin/ntpd -u ntp:ntp -g
7月 26 09:44:04 localhost.localdomain ntpd[65310]: ntp_io: estimated max descriptors: 1024, initial socket boundary: 16
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen and drop on 1 v6wildcard :: UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen normally on 2 lo 127.0.0.1 UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen normally on 3 ens33 192.168.19.136 UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen normally on 4 virbr0 192.168.122.1 UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen normally on 5 ens33 fe80::364d:9ea4:c98b:f3c3 UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listen normally on 6 lo ::1 UDP 123
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listening on routing socket on fd #23 for interface updates
7月 26 09:44:50 localhost.localdomain ntpd[65310]: Deferring DNS for 1.centos.pool.ntp.org 1
[root@localhost zhuxiao]# systemctl disable ntpd //停止自启动
Removed symlink /etc/systemd/system/multi-user.target.wants/ntpd.service.
[root@localhost zhuxiao]# systemctl stop ntpd //停止运行
[root@localhost zhuxiao]# systemctl status ntpd //查看状态
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead) since 三 2023-07-26 09:45:41 CST; 13s ago
Main PID: 65310 (code=exited, status=0/SUCCESS)
7月 26 09:44:04 localhost.localdomain ntpd[65310]: Listening on routing socket on fd #23 for interface updates
7月 26 09:44:50 localhost.localdomain ntpd[65310]: Deferring DNS for 1.centos.pool.ntp.org 1
7月 26 09:45:20 localhost.localdomain ntpd[65310]: 0.0.0.0 c016 06 restart
7月 26 09:45:20 localhost.localdomain ntpd[65310]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
7月 26 09:45:20 localhost.localdomain ntpd[65310]: 0.0.0.0 c011 01 freq_not_set
7月 26 09:45:20 localhost.localdomain ntpd[67616]: signal_no_reset: signal 17 had flags 4000000
7月 26 09:45:27 localhost.localdomain ntpd[65310]: 0.0.0.0 c614 04 freq_mode
7月 26 09:45:41 localhost.localdomain systemd[1]: Stopping Network Time Service...
7月 26 09:45:41 localhost.localdomain ntpd_intres[67616]: ntpd exiting on signal 15
7月 26 09:45:41 localhost.localdomain systemd[1]: Stopped Network Time Service.
以上就是systemctl命令的简要介绍,大家也可以自己实操一下。