systemctl使用指南:CentOS 7.x systemd对比CentOS 6.x daemon

从CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替。

1、原来的 service 命令与 systemctl 命令对比

daemon命令systemctl命令说明
service [服务] startsystemctl start [unit type]启动服务
service [服务] stopsystemctl stop [unit type]停止服务
service [服务] restartsystemctl restart [unit type]停止服务

此外还是二个systemctl参数没有与service命令参数对应

status :参数来查看服务运行情况
reload :重新加载服务,加载更新后的配置文件(并不是所有服务都支持这个参数,比如network.service

应用举例:

#启动网络服务
systemctl start network.service
#停止网络服务
systemctl stop network.service
#重启网络服务
systemctl restart network.service
#查看网络服务状态
systemctl status network.serivce

2、原来的chkconfig 命令与 systemctl 命令对比

2.1、设置开机启动/不启动

daemon命令systemctl命令说明
chkconfig [服务] onsystemctl enable [unit type]设置服务开机启动
chkconfig [服务] offsystemctl disable [unit type]设备服务禁止开机启动

应用举例:

#停止cup电源管理服务
systemctl stop cups.service
#禁止cups服务开机启动
systemctl disable cups.service
#查看cups服务状态
systemctl status cups.service
#重新设置cups服务开机启动
systemctl enable cups.service

2.2、查看系统上上所有的服务
命令格式:

systemctl [command] [–type=TYPE] [–all]

参数详解:

command

list-units:依据unit列出所有启动的unit。加上 –all 才会列出没启动的unit;
list-unit-files:依据/usr/lib/systemd/system/ 内的启动文件,列出启动文件列表

–type=TYPE

为unit type, 主要有service, socket, target

应用举例:

systemctl命令说明
systemctl列出所有的系统服务
systemctl list-units列出所有启动unit
systemctl list-unit-files列出所有启动文件
systemctl list-units –type=service –all列出所有service类型的unit
systemctl list-units –type=service –all |grep cpu列出 cpu电源管理机制的服务
systemctl list-units –type=target –all列出所有target

3、systemctl特殊的用法

systemctl 命令说明
systemctl is-active [unit type]查看服务是否运行
systemctl is-enable [unit type]查看服务是否设置为开机启动
systemctl mask [unit type]注销指定服务]
systemctl unmask [unit type]取消注销指定服务

应用举例:

#查看网络服务是否启动
systemctl is-active network.service
#检查网络服务是否设置为开机启动
systemctl is-enable network.service
#停止cups服务
systemctl stop cups.service
#注销cups服务
systemctl mask cups.service
#查看cups服务状态
systemctl status cups.service
#取消注销cups服务
systemctl unmask cups.service

4、init 命令与systemctl命令对比

init命令systemctl命令说明
init 0systemctl poweroff系统关机
init 6systemctl reboot重新启动

与开关机相关的其他命令:

systemctl命令说明
systemctl suspend进入睡眠模式
systemctl hibernate进入休眠模式
systemctl rescue强制进入救援模式
systemctl emergency强制进入紧急救援模式

5、设置系统运行级别

5.1、运行级别对应表

init级别systemctl target
0shutdown.target
1emergency.target
2rescure.target
3multi-user.target
4
5graphical.target
6

此外还是一个getty.target用来设置tty的数量。

5.2、设置运行级别
命令格式:

systemctl [command] [unit.target]

参数详解:

command:

get-default :取得当前的target
set-default :设置指定的target为默认的运行级别
isolate :切换到指定的运行级别

unit.target :为5.1表中列出的运行级别

systemctl命令说明
systemctl get-default获得当前的运行级别
systemctl set-default multi-user.target设置默认的运行级别为mulit-user
systemctl isolate multi-user.target在不重启的情况下,切换到运行级别mulit-user下
systemctl isolate graphical.target在不重启的情况下,切换到图形界面下

6、使用systemctl分析各服务之前的依赖关系

命令格式:

systemctl list-dependencies [unit] [–reverse]

–reverse是用来检查寻哪个unit使用了这个unit

应用举例:

#获得当前运行级别的target
[root@www ~]# systemctl get-default
multi-user.target#查看当前运行级别target(mult-user)启动了哪些服务
[root@www ~]# systemctl list-dependencies
default.target
├─abrt-ccpp.service
├─abrt-oops.service
├─vsftpd.service
├─basic.target
│ ├─alsa-restore.service
│ ├─alsa-state.service
.....(中间省略).....
│ ├─sockets.target
│ │ ├─avahi-daemon.socket
│ │ ├─dbus.socket
.....(中间省略).....
│ ├─sysinit.target
│ │ ├─dev-hugepages.mount
│ │ ├─dev-mqueue.mount
.....(中间省略).....
│ └─timers.target
│   └─systemd-tmpfiles-clean.timer
├─getty.target
│ └─getty@tty1.service
└─remote-fs.target#查看哪些target引用了当前运行级别的target
[root@www ~]# systemctl list-dependencies --reverse
default.target
└─graphical.target

7、关闭网络服务

在使用systemctl关闭网络服务时有一些特殊
需要同时关闭unit.servce和unit.socket

使用systemctl查看开启的sshd服务

[root@wwwsystem]# systemctl list-units --all | grep sshd
sshd-keygen.service loaded inactive dead        OpenSSH Server Key Generation
sshd.service        loaded active   running     OpenSSH server daemon
sshd.socket         loaded inactive dead        OpenSSH Server Socket

可以看到系统同时开启了sshd.service和sshd.socket , 如果只闭关了sshd.service那么sshd.socket还在监听网络,在网络上有要求连接sshd时就会启动sshd.service。因此如果想完全关闭sshd服务的话,需要同时停用sshd.service和sshd.socket。

systemctl stop sshd.service
systemctl stop sshd.socket
systemctl disable sshd.service sshd.socket

由于centos 7.x默认没有安装net-tools���因此无法使用netstat 来查看主机开发的商品。需要通过yum安装来获得该工具包:

 yum -y install net-tools

查看是否关闭22端口

netstat -lnp |grep sshd

8、关闭防火墙firewall

Centos 7.x 中取消了iptables, 用firewall取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:

systemctl stop firewalld.service
systemctl disable firewalld.service

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/219431.html<

(0)
运维的头像运维
上一篇2025-04-14 02:50
下一篇 2025-04-14 02:51

相关推荐

  • HostVenomVPS测评,实测体验如何?HostVenomVPS好不好用

    HostVenom VPS 在 2026 年实测中展现出极高的性价比,特别适合需要低成本搭建海外独立站或轻量级应用的中小开发者,但在高并发场景下稳定性略逊于一线品牌,在 2026 年云计算市场格局重塑的背景下,HostVenom VPS 作为新兴的托管服务商,凭借灵活的计费模式与优化的网络架构,迅速在开发者社区……

    2026-05-02
    0
  • 美国virtonoVPS测评靠谱吗?virtonoVPS真实体验与数据对比

    美国VirtonoVPS在2026年的实测结论是:其基于LVE架构的独享资源方案在价格与性能的平衡上表现优异,特别适合需要高并发处理且预算敏感的小微跨境电商与独立站卖家,但在跨国网络延迟上略逊于原生CN2 GIA线路,随着2026年云计算市场的进一步洗牌,美国VPS服务商的竞争已从单纯的硬件堆砌转向架构优化与网……

    2026-05-02
    0
  • woothostingVPS测评,实测体验,woothostingVPS怎么样,woothostingVPS真实测评

    WooHosting VPS 在 2026 年的实测结论是:其基于 NVMe SSD 的优化架构在中小型电商场景下表现卓越,但在高并发抗 D 能力上略逊于顶级云厂商,适合追求性价比与 WordPress 深度优化的站长,而非对网络稳定性有极端要求的企业级用户,在 2026 年云计算市场格局重塑的背景下,针对Wo……

    2026-05-02
    0
  • 服务器测评,实测数据与性能表现,服务器性能怎么样,服务器测评

    2026 年服务器实测结论明确:在 AI 推理与高频交易场景下,搭载最新一代国产算力芯片或英伟达 H200 架构的机型,在 4K 视频渲染与万级并发处理中展现出 40% 以上的性能跃升,但需警惕地域性网络延迟对海外业务的影响,随着 2026 年企业数字化转型进入深水区,服务器选型已从单纯的“参数堆砌”转向“场景……

    2026-05-02
    0
  • HostBasticVPS测评靠谱吗?高防实测数据与性能表现如何

    HostBasticVPS 在 2026 年高防场景下表现优异,其抗 DDoS 能力实测可达 500Gbps 峰值,适合需要高可用性的游戏及电商业务,但价格略高于市场平均水平,适合预算充足且对稳定性有极致追求的用户,核心防护能力实测:数据背后的安全逻辑在 2026 年网络攻击日益复杂化的背景下,单纯依靠带宽堆砌……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注