1 systemctl命令:是一个systemd工具,主要负责控制systemd系统和服务管理器。
2 service命令:可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
3 chkconfig命令:是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。
所以systemctl命令是service命令和chkconfig命令的集合和代替。
例如:使用service启动服务实际上也是调用systemctl命令。
1 [root@localhost ~]# service httpd start
2 Redirecting to /bin/systemctl start httpd.service
systemctl命令的用法
Systemctl命令简介:
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。
systemd即为system daemon,是linux下的一种init软件。
Systemctl命令常见用法:
(1)列出所有可用单元:
1 [root@alwen ~]# systemctl list-unit-files
2 UNIT FILE STATE
3 proc-sys-fs-binfmt_misc.automount static
4 -.mount generated
5 boot.mount generated
6 dev-hugepages.mount static
7 dev-mqueue.mount static
8 proc-sys-fs-binfmt_misc.mount static
9 sys-fs-fuse-connections.mount static
10 sys-kernel-config.mount static
11 sys-kernel-debug.mount static
12 tmp.mount disabled
13 systemd-ask-password-console.path static
14 systemd-ask-password-wall.path static
15 session-1.scope transient
16 session-3.scope transient
17 session-4.scope transient
18 session-5.scope transient
19 session-6.scope transient
20 session-7.scope transient
21 arp-ethers.service disabled
22 atd.service enabled
23 auditd.service enabled
24 autovt@.service enabled
25 blk-availability.service disabled
26 chrony-dnssrv@.service static
27 chrony-wait.service disabled
28 chronyd.service disabled
29 cockpit-motd.service static
30 cockpit-wsinstance-http-redirect.service static
(2)列出所有可用单元:
1 [root@alwen ~]# systemctl list-units
2 UNIT LOAD ACTIVE SUB DESCRIPTION
3 proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File>
4 sys-devices-pci0000:00-0000:00:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged VMwar>
5 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged VMwar>
6 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged VMwar>
7 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged VMware_Vir>
8 sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loaded active plugged 82545EM Gigabit Etherne>
9 sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded active plugged ES1371/ES1373 / Creat>
10 sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/ser>
11 sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/ser>
12 sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/ser>
13 sys-devices-pnp0-00:05-tty-ttyS0.device loaded active plugged /sys/devices/pnp0/00:05/t>
14 sys-devices-virtual-block-dm\x2d0.device loaded active plugged /sys/devices/virtual/bloc>
15 sys-devices-virtual-block-dm\x2d1.device loaded active plugged /sys/devices/virtual/bloc>
16 sys-module-configfs.device loaded active plugged /sys/module/configfs
17 sys-subsystem-net-devices-ens33.device loaded active plugged 82545EM Gigabit Ethernet >
18 -.mount loaded active mounted Root Mount
19 boot.mount loaded active mounted /boot
20 dev-hugepages.mount loaded active mounted Huge Pages File System
21 dev-mqueue.mount loaded active mounted POSIX Message Queue File >
22 run-user-0.mount loaded active mounted /run/user/0
23 sys-kernel-config.mount loaded active mounted Kernel Configuration File>
24 sys-kernel-debug.mount loaded active mounted Kernel Debug File System
25 systemd-ask-password-console.path loaded active waiting Dispatch Pass
(3)列出所有失败单元:
[root@alwen ~]# systemctl --failed``0 loaded units listed. Pass --all to see loaded but inactive units, too.``To show all installed unit files use ``'systemctl list-unit-files'``.
4)检查某个单元是否启动:
1 [root@localhost ~]# systemctl is-enabled httpd.service
2 enabled
(5)检查某个服务的运行状态:
1 [root@alwen ~]# systemctl status httpd.service
2 ● httpd.service - The Apache HTTP Server
3 Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
4 Active: active (running) since Thu 2021-06-10 19:58:25 CST; 34min ago
5 Docs: man:httpd.service(8)
6 Main PID: 1919 (httpd)
7 Status: "Total requests: 2; Idle/Busy workers 100/0;Requests/sec: 0.00096; Bytes served/sec: 96 B/sec"
8 Tasks: 278 (limit: 11210)
9 Memory: 48.0M
10 CGroup: /system.slice/httpd.service
11 ├─1919 /usr/sbin/httpd -DFOREGROUND
12 ├─1920 /usr/sbin/httpd -DFOREGROUND
13 ├─1921 /usr/sbin/httpd -DFOREGROUND
14 ├─1922 /usr/sbin/httpd -DFOREGROUND
15 ├─1923 /usr/sbin/httpd -DFOREGROUND
16 └─2148 /usr/sbin/httpd -DFOREGROUND
17
18 Jun 10 19:58:25 alwen.com systemd[1]: Starting The Apache HTTP Server...
19 Jun 10 19:58:25 alwen.com systemd[1]: Started The Apache HTTP Server.
20 Jun 10 19:58:25 alwen.com httpd[1919]: Server configured, listening on: port 80
(6)列出所有服务:
1 [root@alwen ~]# systemctl list-unit-files --type=service
2 UNIT FILE STATE
3 arp-ethers.service disabled
4 atd.service enabled
5 auditd.service enabled
6 autovt@.service enabled
7 blk-availability.service disabled
8 chrony-dnssrv@.service static
9 chrony-wait.service disabled
10 chronyd.service disabled
11 cockpit-motd.service static
12 cockpit-wsinstance-http-redirect.service static
13 cockpit-wsinstance-http.service static
14 cockpit-wsinstance-https-factory@.service static
15 cockpit-wsinstance-https@.service static
16 cockpit.service static
17 console-getty.service disabled
18 container-getty@.service static
19 cpupower.service disabled
20 crond.service enabled
21 dbus-org.freedesktop.hostname1.service static
22 dbus-org.freedesktop.locale1.service static
23 dbus-org.freedesktop.login1.service static
24 dbus-org.freedesktop.nm-dispatcher.service enabled
25 dbus-org.freedesktop.portable1.service static
26 dbus-org.freedesktop.timedate1.service enabled
27 dbus.service static
28 debug-shell.service disabled
29 dm-event.service static
30 dnf-makecache.service static
(7)启动,停止,重启服务等:
1 [root@localhost ~]# systemctl restart httpd.service
2 # systemctl restart httpd.service
3 # systemctl stop httpd.service
4 # systemctl reload httpd.service
5 # systemctl status httpd.service
(8)查询服务是否激活,和配置是否开机启动:
1 [root@alwen ~]# systemctl is-active httpd
2 active
3 [root@alwen ~]# systemctl disable httpd
4 [root@alwen ~]# systemctl enable httpd
5 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
6 [root@alwen ~]#
(9)使用systemctl命令杀死服务:
[root@alwen ~]# systemctl kill httpd
10)列出系统的各项服务,挂载,设备等:
1 [root@alwen ~]# systemctl list-unit-files
2 UNIT FILE STATE
3 proc-sys-fs-binfmt_misc.automount static
4 -.mount generated
5 boot.mount generated
6 dev-hugepages.mount static
7 dev-mqueue.mount static
8 proc-sys-fs-binfmt_misc.mount static
9 sys-fs-fuse-connections.mount static
10 sys-kernel-config.mount static
11 sys-kernel-debug.mount static
12 tmp.mount disabled
13 systemd-ask-password-console.path static
14 systemd-ask-password-wall.path static
15 session-1.scope transient
16 session-3.scope transient
17 session-4.scope transient
18 session-5.scope transient
19 session-6.scope transient
20 session-7.scope transient
21 arp-ethers.service disabled
22 atd.service enabled
(11)获得系统默认启动级别和设置默认启动级别:
1 [root@alwen ~]# systemctl get-default
2 multi-user.target
1 [root@localhost ~]# systemctl get-default
2 graphical.target
3 [root@localhost ~]# systemctl set-default multi-user.target
12)启动运行等级:
1 [root@alwen ~]# systemctl isolate multiuser.target
2 Failed to start multiuser.target: Operation refused, unit may not be isolated.
3 See system logs and 'systemctl status multiuser.target' for details.
(13)重启、停止,挂起、休眠系统等:
1 # systemctl reboot
2 # systemctl halt
3 # systemctl suspend
4 # systemctl hibernate
5 # systemctl hybrid-sleep
service命令可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
service命令的作用是去/etc/init.d目录下寻找相应的服务,进行开启和关闭等操作。
使用示例:
开启关闭一个服务:service httpd start/stop
1 [root@localhost ~]# service httpd start
2 Redirecting to /bin/systemctl start httpd.service
查看系统服务的状态:service --status-all
1 [root@alwen ~]# service --status-all
2 [root@alwen ~]# service all status
3 Redirecting to /bin/systemctl status all.service
4 Unit all.service could not be found.
1 [root@localhost ~]# service --status-all
2 未加载 netconsole 模块
3 已配置设备:
4 lo ens33 ens33.old team0 team0-port1 team0-port1.old team0-port2 team0-port2.old team0.old
5 当前活跃设备:
6 lo ens33 virbr0 ens38 ens39 team0
7 ● rhnsd.service - LSB: Starts the Spacewalk Daemon
8 Loaded: loaded (/etc/rc.d/init.d/rhnsd; bad; vendor preset: disabled)
9 Active: active (running) since 五 2018-10-12 14:53:19 CST; 3 days ago
10 Docs: man:systemd-sysv-generator(8)
11 Main PID: 1380 (rhnsd)
12 CGroup: /system.slice/rhnsd.service
13 └─1380 rhnsd
chkconfig是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
chkconfig可以更新(启动或停止)和查询系统服务(service)运行级信息。更简单一点,chkconfig是一个用于维护/etc/rc[0-6].d目录的命令行工具。
chkconfig常见用法:
1 [root@alwen ~]# chkconfig --help
2 chkconfig version 1.13 - Copyright (C) 1997-2000 Red Hat, Inc.
3 This may be freely redistributed under the terms of the GNU Public License.
4
5 usage: chkconfig [--list] [--type <type>] [name]
6 chkconfig --add <name>
7 chkconfig --del <name>
8 chkconfig --override <name>
9 chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
(一)设置service开机是否启动:
1 chkconfig name on/off/reset
默认情况下,on和off开关只对运行级2,3,4,5有效,reset可以对所有运行级有效。
1 [root@alwen ~]# chkconfig httpd on
2 Note: Forwarding request to 'systemctl enable httpd.service'.
[root@localhost ~]# chkconfig httpd on
注意:正在将请求转发到“systemctl enable httpd.service”。
在Redhat7上,运行chkconfig命令,都会被转到systemcle命令上。
2)设置service运行级别:
1 [root@alwen ~]# chkconfig --level levels
2 chkconfig version 1.13 - Copyright (C) 1997-2000 Red Hat, Inc.
3 This may be freely redistributed under the terms of the GNU Public License.
4
5 usage: chkconfig [--list] [--type <type>] [name]
6 chkconfig --add <name>
7 chkconfig --del <name>
8 chkconfig --override <name>
9 chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
该命令可以用来指定服务的运行级别,即指定运行级别2,3,4,5等。
例如:
1 [root@localhost ~]# chkconfig --level 5 httpd on
2 注意:正在将请求转发到“systemctl enable httpd.service”
(三)列出service启动信息:
1 # chkconfig --list [name]
如果不指定name,会列出所有services的信息。
每个service每个运行级别都会有一个启动和停止脚本;当切换运行级别时,init不会重启已经启动的service,也不会重新停止已经停止的service。
例如:
1 [root@localhost ~]# chkconfig --list
2
3 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
4 如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
5 欲查看对特定 target 启用的服务请执行
6 'systemctl list-dependencies [target]'。
7
8 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
9 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
10 rhnsd 0:关 1:关 2:开 3:开 4:开 5:开 6:关
1 [root@alwen ~]# systemctl list-dependencies
2 default.target
3 ● ├─atd.service
4 ● ├─auditd.service
5 ● ├─crond.service
6 ● ├─dbus.service
7 ● ├─httpd.service
8 ● ├─irqbalance.service
9 ● ├─kdump.service
10 ● ├─libstoragemgmt.service
11 ● ├─mcelog.service
12 ● ├─mdmonitor.service
13 ● ├─NetworkManager.service
14 ● ├─nvmefc-boot-connections.service
15 ● ├─smartd.service
16 ● ├─sshd.service
17 ● ├─sssd.service
18 ● ├─systemd-ask-password-wall.path
19 ● ├─systemd-logind.service
20 ● ├─systemd-update-utmp-runlevel.service
21 ● ├─systemd-user-sessions.service
22 ● ├─tuned.service
23 ● ├─vdo.service
24 ● ├─basic.target
25 ● │ ├─-.mount
26 ● │ ├─microcode.service
27 ● │ ├─paths.target
28 ● │ ├─slices.target
29 ● │ │ ├─-.slice
30 ● │ │ └─system.slice
总结:service命令的功能基本都被systemct取代。直接使用systemctl命令即可
查看当前系统的运行级别
[root@apenglinux ~]# runlevel
3 5
查看系统的默认级别
[root@apenglinux ~]# systemctl get-default
graphical.target
设置系统的默认运行级别为3
[root@apenglinux ~]# systemctl set-default multi-user.target
[root@apenglinux ~]# rm -f /etc/systemd/system/default.target
[root@apenglinux ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
设置系统的默认运行级别为5
[root@apenglinux ~]# systemctl set-default graphical.target
[root@apenglinux ~]# rm -f /etc/systemd/system/default.target
[root@apenglinux ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
从3级别切换到5级别
init 5
systemctl isolate graphical.target
从5级别切换到3级别
init 3
systemctl isolate multi-user.target
查看系统的运行级别
[root@apenglinux ~]# ls -l /lib/systemd/system/runlevel*target
lrwxrwxrwx. 1 root root 15 1月 18 17:03 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 1月 18 17:03 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 1月 18 17:03 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 1月 18 17:03 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 1月 18 17:03 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 1月 18 17:03 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 1月 18 17:03 /lib/systemd/system/runlevel6.target -> reboot.target
发表于2014-09-01 作者 admin
linux中有很多命令已经存在了N多年,渐渐一些已被一些新命令所代替,不过由于习惯的原因,很多时候我们并不能一下子适应过来 ,例如ifconfig之于ip命令。最近在玩ubuntu和opensuse时发现了systemctl命令了,后来在试玩centos7时也发现了该命令,systemctl是systemd下的一个工具。网上查了下,该命令已经存在很久了。该命令是用来替代service和chkconfig两个命令的 — 尽管个人感觉还是后者好用。
为了顺应时间的发展,这里总结下。在目前很多linux的新发行版本里,系统对于daemon的启动管理方法不再采用SystemV形式,而是使用了sytemd的架构来管理daemon的启动。
在systemd的管理体系里面,以前的运行级别(runlevel)的概念被新的运行目标(target)所取代。tartget的命名类似于multi-user.target等这种形式,比如原来的运行级别3(runlevel3)就对应新的多用户目标(multi-user.target),run level 5就相当于graphical.target。
由于不再使用runlevle概念,所以/etc/inittab也不再被系统使用 — 无怪乎在新版本ubuntu上找不到inittab文件了。
而在systemd的管理体系里面,默认的target(相当于以前的默认运行级别)是通过软链来实现。如:
ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target
在/lib/systemd/system/ 下面定义runlevelX.target文件目的主要是为了能够兼容以前的运行级别level的管理方法。 事实上/lib/systemd/system/runlevel3.target,同样是被软连接到multi-user.target。
注:opensuse下是在/usr/lib/systemd/system/目录下。
在systemd管理体系里,称呼需要管理的daemon为单元(unit)。对于单元(unit)的管理是通过命令systemctl来进行控制的。例如显示当前的处于运行状态的unit(即daemon),如:
#systemctl
#systemctl --all
#systemctl list-units --type=sokect
#systemctl list-units --type=service
注:type后面可以接的类型可以通过help查看
361way:~ # systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
snapshot
timer
swap
path
slice
scope
systemctl 默认有5列输出,如下:
361way:~ # systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
列表中的第一栏是单位的名字;
第二栏则表示该单位的定义是否已由 systemd 正确加载;
第三栏则告诉我们该单位是否正在运行。如果你使用了 -a 参数,那么该程序将仅显示非正在运行的单位,即已安装但并未在启动时使用的单位,同时也包含引导系统未能正常加载的单位文件(原因很可能为该单位文件出现错误);
第四栏则给出了当前状态:“exited”表示该进程已经无任何错误地完成,这种情况适用于一诸如进程在启动后并不在后台继续运行的情况,例如,在系统启动时由于考虑到兼容性因素执行在 sysvinit 里面常用的 /etc/rc.d/rc.local 文件的服务单位。“Running”表示正在后台运行的服务,如 cron、dbus、sshd 和 udev ;
第五栏是对该单位的描述。标有“LSB”或“SYSV”的单位已由 systemd 自动创建以管理传统启动脚本。
不能启动或启动后崩溃的服务在第四栏中用红色标为“failed”(如果终端可以显示彩色)。
对于失败的服务,可以通过systemctl status 服务名 的方式查看详细信息,具体输出信息,systemctl用法和示例中会给出。
chkconfig、service命令与systemctl命令的区别见下表:
| 任务 | 旧指令 | 新指令 |
|---|---|---|
| 使某服务自动启动 | chkconfig –level 3 httpd on | systemctl enable httpd.service |
| 使某服务不自动启动 | chkconfig –level 3 httpd off | systemctl disable httpd.service |
| 检查服务状态 | service httpd status | systemctl status httpd.service (服务详细信息)systemctl is-active httpd.service (仅显示是否 Active) |
| 加入自定义服务 | chkconfig –add test | systemctl load test.service |
| 删除服务 | chkconfig –del xxx | 停掉应用,删除相应的配置文件 |
| 显示所有已启动的服务 | chkconfig –list | systemctl list-units –type=service |
| 启动某服务 | service httpd start | systemctl start httpd.service |
| 停止某服务 | service httpd stop | systemctl stop httpd.service |
| 重启某服务 | service httpd restart | systemctl restart httpd.service |
注:systemctl后的服务名可以到/usr/lib/systemd/system目录查看(opensuse下),其他发行版是位于/lib/systemd/system/ 下。
//opensuse下
361way:~ # systemctl status network.service
network.service - LSB: Configure network interfaces and set up routing
Loaded: loaded (/usr/lib/systemd/system/network.service; enabled)
Active: active (exited) since Mon 2014-09-01 20:05:45 CST; 2h 14min ago
Process: 1022 ExecStart=/etc/init.d/network start (code=exited, status=0/SUCCESS)
Sep 01 20:05:15 linux-pnp8 systemd[1]: Starting LSB: Configure network interfaces and set up routing...
Sep 01 20:05:15 linux-pnp8 network[1022]: Setting up network interfaces:
Sep 01 20:05:15 linux-pnp8 network[1022]: lo
Sep 01 20:05:15 linux-pnp8 network[1022]: lo IP address: 127.0.0.1/8
Sep 01 20:05:45 linux-pnp8 network[1022]: ..done..done..doneSetting up service network . . . . . . . . . . . . ...done
Sep 01 20:05:45 linux-pnp8 systemd[1]: Started LSB: Configure network interfaces and set up routing.
//centos下,apache服务未启动时的显示
# systemctl status httpd.service
httpd.service - The Apache HTTP Server (prefork MPM)
Loaded: loaded (/lib/systemd/system/httpd.service; disabled)
Active: inactive (dead) <-- 表示未启动
CGroup: name=systemd:/system/httpd.service
上面两个命令相当于/etc/init.d/network status 和 /etc/init.d/httpd status,opensuse和centos下的用法相同,只不过显示的路径不同。其他操作类似。
还以上面提到的httpd.service配置为例,httpd.service文件里可以找到如下行:
[Install]
WantedBy=multi-user.target
则表明在多用户目标(multi-user.target,相当于level3)时自动启动。如果想在runlevel 5下也自启动,则可以将配置改为如下:
[Install]
WantedBy=multi-user.target graphical.target
一旦设定了自动启动(enbale),就在/etc/systemd/system/.wants/下面建了一个httpd.service的软连接,连接到/lib/systemd/system/下的相应服务那里 。所以显示自动启动状态的unit (类似于chekconfig --list命令的结果),可以通过下面的方法:
#ls /etc/systemd/system/multi-user.target.wants/
systemctl的总结先写到这里,其是systemd包内的一个工具,也是该包中最为常用的工具。回头再针对systemd做一个总结。