CentOS7中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)

应用举例:

 
  1. #启动网络服务

  2. systemctl start network.service

  3.  
  4. #停止网络服务

  5. systemctl stop network.service

  6.  
  7. #重启网络服务

  8. systemctl restart network.service

  9.  
  10. #查看网络服务状态

  11. systemctl status network.serivce

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

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

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

应用举例:

 
  1. #停止cup电源管理服务

  2. systemctl stop cups.service

  3.  
  4. #禁止cups服务开机启动

  5. systemctl disable cups.service

  6.  
  7. #查看cups服务状态

  8. systemctl status cups.service

  9.  
  10. #重新设置cups服务开机启动

  11. 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]取消注销指定服务

应用举例:

 
  1. #查看网络服务是否启动

  2. systemctl is-active network.service

  3.  
  4. #检查网络服务是否设置为开机启动

  5. systemctl is-enable network.service

  6.  
  7. #停止cups服务

  8. systemctl stop cups.service

  9.  
  10. #注销cups服务

  11. systemctl mask cups.service

  12.  
  13. #查看cups服务状态

  14. systemctl status cups.service

  15.  
  16. #取消注销cups服务

  17. 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

应用举例:

 
  1. #获得当前运行级别的target

  2. [root@www ~]# systemctl get-default

  3. multi-user.target

  4.  
  5. #查看当前运行级别target(mult-user)启动了哪些服务

  6. [root@www ~]# systemctl list-dependencies

  7. default.target

  8. ├─abrt-ccpp.service

  9. ├─abrt-oops.service

  10. ├─vsftpd.service

  11. ├─basic.target

  12. │ ├─alsa-restore.service

  13. │ ├─alsa-state.service

  14. .....(中间省略).....

  15. │ ├─sockets.target

  16. │ │ ├─avahi-daemon.socket

  17. │ │ ├─dbus.socket

  18. .....(中间省略).....

  19. │ ├─sysinit.target

  20. │ │ ├─dev-hugepages.mount

  21. │ │ ├─dev-mqueue.mount

  22. .....(中间省略).....

  23. │ └─timers.target

  24. │ └─systemd-tmpfiles-clean.timer

  25. ├─getty.target

  26. │ └─getty@tty1.service

  27. └─remote-fs.target

  28.  
  29. #查看哪些target引用了当前运行级别的target

  30. [root@www ~]# systemctl list-dependencies --reverse

  31. default.target

  32. └─graphical.target

7、关闭网络服务

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

使用systemctl查看开启的sshd服务

 
  1. [root@www system]# systemctl list-units --all | grep sshd

  2. sshd-keygen.service loaded inactive dead OpenSSH Server Key Generation

  3. sshd.service loaded active running OpenSSH server daemon

  4. sshd.socket loaded inactive dead OpenSSH Server Socket

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

 
  1. systemctl stop sshd.service

  2. systemctl stop sshd.socket

  3. 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取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:

1, systemctl stop firewalld.service
2, systemctl disable firewalld.service

--------------------- 本文来自 somnus_小凯 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012486840/article/details/53161574?utm_source=copy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值