Centos7中systemctl命令详解

Centos7中systemctl命令详解

LinuxSystemctl是一个系统管理守护进程、工具和库的集合,用于取代System V、service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器。通过Systemctl –help可以看到该命令主要分为:查询或发送控制命令给systemd服务,管理单元服务的命令,服务文件的相关命令,任务、环境、快照相关命令,systemd服务的配置重载,系统开机关机相关的命令。

  1. 列出所有可用单元

# systemctl list-unit-files

  1. 列出所有运行中单元

# systemctl list-units

  1. 列出所有失败单元

# systemctl –failed

  1. 检查某个单元(如 crond.service)是否启用

# systemctl is-enabledcrond.service

  1. 列出所有服务

# systemctl list-unit-files –type=service

  1. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

# systemctl start httpd.service

# systemctl restart httpd.service

# systemctl stop httpd.service

# systemctl reload httpd.service

# systemctl status httpd.service

注意:当我们使用systemctl的start,restart,stop和reload命令时,终端不会输出任何内容,只有status命令可以打印输出。

  1. 如何激活服务并在开机时启用或禁用服务(即系统启动时自动启动mysql.service服务)

# systemctl is-active mysql.service

# systemctl enable mysql.service

# systemctl disable mysql.service

  1. 如何屏蔽(让它不能启动)或显示服务(如ntpdate.service)

# systemctl mask ntpdate.service

ln -s ‘/dev/null”/etc/systemd/system/ntpdate.service’

# systemctl unmask ntpdate.service

rm ‘/etc/systemd/system/ntpdate.service’

  1. 使用systemctl命令杀死服务

# systemctl killcrond

  1. 列出所有系统挂载点

# systemctl list-unit-files –type=mount

  1. 挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

# systemctl start tmp.mount

# systemctl stop tmp.mount

# systemctl restart tmp.mount

# systemctl reload tmp.mount

# systemctl status tmp.mount

  1. 在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

# systemctl is-active tmp.mount

# systemctl enable tmp.mount

# systemctl disable tmp.mount

  1. 在Linux中屏蔽(让它不能启用)或可见挂载点

# systemctl mask tmp.mount

ln -s ‘/dev/null”/etc/systemd/system/tmp.mount’

# systemctl unmask tmp.mount

rm ‘/etc/systemd/system/tmp.mount’

  1. 列出所有可用系统套接口

# systemctl list-unit-files –type=socket

  1. 检查某个服务的所有配置细节

# systemctl showmysql

  1. 获取某个服务(httpd)的依赖性列表

# systemctl list-dependencies httpd.service

  1. 启动救援模式

# systemctl rescue

  1. 进入紧急模式

# systemctl emergency

  1. 列出当前使用的运行等级

# systemctl get-default

  1. 启动运行等级5,即图形模式

# systemctl isolate runlevel5.target

# systemctl isolate graphical.target

  1. 启动运行等级3,即多用户模式(命令行)

# systemctl isolate runlevel3.target

# systemctl isolate multiuser.target

  1. 设置多用户模式或图形模式为默认运行等级

# systemctl set-default runlevel3.target

# systemctl set-default runlevel5.target

  1. 重启、停止、挂起、休眠系统或使系统进入混合睡眠

# systemctl reboot

# systemctl halt

# systemctl suspend

# systemctl hibernate

# systemctl hybrid-sleep

对于不知运行等级为何物的,说明如下。

Runlevel 0 : 关闭系统

Runlevel 1 : 救援,维护模式

Runlevel 3 : 多用户,无图形系统

Runlevel 4 : 多用户,无图形系统

Runlevel 5 : 多用户,图形化系统

Runlevel 6 : 关闭并重启机器

对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合chkconfig 与 service功能于一体。

systemctl is-enabled iptables.service

systemctl is-enabled servicename.service #查询服务是否开机启动

systemctl enable *.service #开机运行服务

systemctl disable *.service #取消开机运行

systemctl start *.service #启动服务

systemctl stop *.service #停止服务

systemctl restart *.service #重启服务

systemctl reload *.service #重新加载服务配置文件

systemctl status *.service #查询服务运行状态

systemctl –failed #显示启动失败的服务

注:*代表某个服务的名字,如http的服务名为httpd

例如在CentOS 7 上安装http

[root@CentOS7 ~]# yum -y install httpd

启动服务(等同于service httpd start)

systemctl start httpd.service

停止服务(等同于service httpd stop)

systemctl stop httpd.service

重启服务(等同于service httpd restart)

systemctl restart httpd.service

查看服务是否运行(等同于service httpd status)

systemctl status httpd.service

开机自启动服务(等同于chkconfig httpd on)

systemctl enable httpd.service

开机时禁用服务(等同于chkconfig httpd on)

systemctl disable httpd.service

查看服务是否开机启动 (等同于chkconfig –list)

systemctl & systemd的相关资料整理

1、脚本目录:
/usr/lib/systemd/
2、服务配置文件目录(开机自启动):
/usr/lib/systemd/system
3、服务文件
服务格式:
*.service
服务文件格式:
[Unit] //用户服务说明
Description=xxx //描述服务
After=xxx //描述服务类别
[Service] //服务具体运行参数
Type=forking //服务后台运行
PIDFile=/path/pid //PID的路径
ExecStart= //服务运行的命令与参数(绝对路径)
ExecReload= //服务重启的命令与参数(绝对路径)
ExecStop= //服务停止运行的命令与参数(绝对路径)
PrivateTmp=True //为服务分配独立的临时空间
[Install] //服务安装的配置

4、服务使用方法

systemctl start [服务文件名]
systemctl restart [服务文件名]
systemctl stop [服务文件名]
systemctl status [服务文件名]

5、相关命令
任务  旧指令 新指令
使某服务自动启动    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 –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

 

以下文章点击率最高

Loading…

     

如果这文章对你有帮助,请扫左上角微信支付-支付宝,给于打赏,以助博客运营

发表评论

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