设为首页 收藏本站
查看: 1106|回复: 0

[经验分享] CentOS 7之systemd学习笔记

[复制链接]

尚未签到

发表于 2018-4-19 15:10:24 | 显示全部楼层 |阅读模式
一、systemd简介
  systemd是CentOS 7和RHEL 7的init程序, 拥有如下新特性:


  • 系统引导时实现服务并行启动
  • 按需激活进程
  • 系统状态快照
  • 基于依赖关系定义服务控制逻辑

二、systemd核心概念: unit
  systemd将各种系统启动和运行的相关对象表示为各种不同类型的unit, 并提供了处理不同unit之间依赖关系的能力. 使用配置文件进行标识和配置, 文件中主要包含系统服务、监听socket、保存的系统快照以及其他的init相关的信息, 配置文件路径如下:


  • /usr/lib/systemd/system/
  • /run/systemd/systemd/system/
  • /etc/systemd/system/

2.1 unit的类型
  unit主要有以下类型:


unit类型
文件扩展名
作用




Service unit
.service
用于封装一个后台服务进程


Target unit
.target
用于模拟实现“运行级别”


Device unit
.device
用于定义内核识别的设备


Mount unit
.mount
用于定义文件系统挂载点


Socket unit
.socket
用于标识进程间通信用的socket文件


Snapshot unit
.snapshot
管理系统快照


Swap unit
.swap
用于标识swap设备


Automount
.automount
用于标识文件系统的自动挂载点


Path unit
.path
用于定义文件系统中的一个文件或目录

2.2 关键特性
  systemd关键特性如下:


  • 基于socket的激活机制: socket与服务程序分离
  • 基于bus的激活机制
  • 基于device的激活机制
  • 基于path的激活机制
  • 系统快照: 保存各unit的当前状态信息于持久存储设备中
  • 向后兼容sysV init脚本
  Note: init的命令和systemd的命令不完全兼容, systemctl命令固定不变, 非由systemd启动的服务, systemctl无法与之通信

三、systemd管理系统服务命令: systemctl
  systemctl - 管理系统服务, 能兼容早期的服务脚本

# 用法: systemctl COMMAND name[.service]
# 启动服务: service name start ==> systemctl start name[.service]
[iyunv@zabbix system]# systemctl start httpd
# 停止服务: service name stop ==> systemctl stop name[.service]
[iyunv@zabbix system]# systemctl stop httpd
# 重启服务: service name restart ==> systemctl restart name[.service]
[iyunv@zabbix system]# systemctl restart httpd
# 状态: service name status ==> systemctl status name[.service]
[iyunv@zabbix system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-02-26 10:30:40 CST; 23min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3870 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
CGroup: /system.slice/httpd.service
├─3870 /usr/sbin/httpd -DFOREGROUND
├─3927 /usr/sbin/httpd -DFOREGROUND
├─3928 /usr/sbin/httpd -DFOREGROUND
├─3929 /usr/sbin/httpd -DFOREGROUND
├─3930 /usr/sbin/httpd -DFOREGROUND
└─3931 /usr/sbin/httpd -DFOREGROUND
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Starting The Apache HTTP Server...
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Started The Apache HTTP Server.
Feb 26 10:31:40 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:32:20 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:39:18 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:28 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:38 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
# 条件式重启: service name condrestart ==> systemctl try-restart name[.service]
[iyunv@zabbix system]# systemctl try-restart httpd
# 重载或重启服务: systemctl reload-or-restart name[.service]
[iyunv@zabbix system]# systemctl reload-or-restart httpd
# 重载或条件式重启: systemctl reload-or-try-restart name[.service]
[iyunv@zabbix system]# systemctl reload-or-try-restart httpd
# 禁止设定为开机启动: systemctl mask name[.service]
[iyunv@zabbix system]# systemctl mask httpd
Created symlink from /etc/systemd/system/httpd.service to /dev/null.
# 取消禁止设定为开机自启: systemctl unmask name[.service]
[iyunv@zabbix system]# systemctl unmask httpd
Removed symlink /etc/systemd/system/httpd.service.
# 查看某服务当前激活状态: systemctl is-active name[.service]
[iyunv@zabbix system]# systemctl is-active httpd
active
# 查看所有已经激活的服务: systemctl list-units --type service
[iyunv@zabbix system]# systemctl list-units --type service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
auditd.service                     loaded active running Security Auditing Service
chronyd.service                    loaded active running NTP client/server
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Message Bus
...
# 查看所有服务: systemctl list-unit --type service --all
[iyunv@zabbix system]# systemctl list-units --type service --all
UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
auditd.service                                        loaded    active   running Security Auditing Service
brandbot.service                                      loaded    inactive dead    Flexible Branding Service
chronyd.service                                       loaded    active   running NTP client/server
cpupower.service                                      loaded    inactive dead    Configure CPU power related settings
crond.service                                         loaded    active   running Command Scheduler
dbus.service                                          loaded    active   running D-Bus System Message Bus
● display-manager.service                               not-found inactive dead    display-manager.service
...
# 设定某服务开机自启动: chkconfig name on ==> systemctl enable name[.service]
[iyunv@zabbix system]# systemctl enable httpd   
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
# 设定某服务禁止开机自启动: chkconfig name off ==> systemctl disable name[.service]
[iyunv@zabbix system]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
# 查看所有服务的开机自启状态: chkconfig --list ==> systemctl list-unit-files --type service
[iyunv@zabbix system]# systemctl list-unit-files --type service
UNIT FILE                                     STATE   
arp-ethers.service                            disabled
auditd.service                                enabled
autovt@.service                               enabled
blk-availability.service                      disabled
...
# 查看服务是否开机自启: systemctl is-enabled name[.service]
[iyunv@zabbix system]# systemctl is-enabled httpd
enabled
# 查看服务的依赖关系: systemctl list-dependencies name[.service]
[iyunv@zabbix system]# systemctl list-dependencies httpd
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
●   ├─microcode.service
●   ├─rhel-autorelabel-mark.service
●   ├─rhel-autorelabel.service
●   ├─rhel-configure.service
...

  • target unit

# 运行级别:
# level 0 ==> runlevel0.target, poweroff.target
# level 1 ==> runlevel1.target, rescue.target
# level 2 ==> runlevel2.target, multi-user.target
# level 3 ==> runlevel3.target, multi-user.target
# level 4 ==> runlevel4.target, multi-user.target
# level 5 ==> runlevel5.target, graphical.target
# level 6 ==> runlevel6.target, reboot.target
# 级别切换: init N ==> systemctl isolate name.service
[iyunv@zabbix ~]# systemctl isolate multi-user.target
# 查看级别: runlevel ==> systemctl list-units --type target
[iyunv@zabbix ~]# systemctl list-units --type target
UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
basic.target          loaded active active Basic System
cryptsetup.target     loaded active active Encrypted Volumes
getty.target          loaded active active Login Prompts
local-fs-pre.target   loaded active active Local File Systems (Pre)
local-fs.target       loaded active active Local File Systems
multi-user.target     loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target        loaded active active Network
paths.target          loaded active active Paths
remote-fs.target      loaded active active Remote File Systems
slices.target         loaded active active Slices
sockets.target        loaded active active Sockets
swap.target           loaded active active Swap
sysinit.target        loaded active active System Initialization
timers.target         loaded active active Timers
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
15 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
# 获取默认运行级别: /etc/inittab ==> systemctl get-default
[iyunv@zabbix ~]# systemctl get-default
multi-user.target
# 修改默认级别: /etc/inittab ==> systemctl set-default name[.target]
[iyunv@zabbix ~]# systemctl set-default rescue.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/rescue.target.
# 切换至紧急救援模式: systemctl resuce
# 切换至emergency模式: systemctl emergency

  • 其他常用命令

# 关机: systemctl halt, systemctl poweroff
# 重启: systemctl reboot
# 挂起: systemctl suspend
# 快照
# 保存系统快照: systemctl hibernate
# 快照并挂起: systemctl hibrid-sleep

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-449111-1-1.html 上篇帖子: Redhat 5.8更換centos Yum源 下篇帖子: CentOS 7 修改网卡名为eth0
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表