linux 系统服务

centos7后,red hat 系列放弃沿用多年的System V开机启动服务流程,改用systemd启动服务机制。
systemd有如下好处:
1.平行处理所有服务,加速开机流程
2.一经要求就响应的on-demand启动方式
3.服务相依性的自我检查
4.依daemon功能分类(service、socket、target、path、snapshot、timer)
5.将多个daemon集合成一个群组
6.向下兼容旧有的init服务版本

systemd的配置文件放置目录
/usr/lib/systemd/system/ 每个服务最主要的启动脚本设定
/run/systemd/system/ 系统执行过程中所产生的服务脚本,这些脚本
的优先序要比/usr/lib/systemd/system/ 高
/etc/systemd/system 管理员依据知己系统的需求所建立的执行脚本,优先序
比/run/systemd/system/ 高

systemd的unit类型分类说明

扩展名主要服务功能
.service一般服务类型,主要是系统服务,包括服务器本身所需要的本地服务以及网络服务
.socket内部程序数据交换的插槽服务,主要是IPC(Inter-process communication)的传输信息插槽文件,当有透过插槽文件传递讯息来说要链接服务时,就依据当时的状态将该用户的要求传送到对应daemon,若daemon尚未启动,则启动该daemon后再传送用户要求
.target执行环境类型,是一群unit的集合,可以理解为一对服务的集合
.mount .automount文件系统挂载相关服务:例如来自网络的自动挂载、NFS文件系统挂载
.path侦测特定文件或目录类型:例如打印服务
.time循环执行的服务

通过systemctl管理单一服务(service unit)
命令格式如下:
systemctl [command] [unit]

list-unit-files List installed unit files
enable [NAME…] Enable one or more unit files
disable [NAME…] Disable one or more unit files
reenable [NAME…] Reenable one or more unit files
preset [NAME…] Enable/disable one or more unit files based on preset configuration
is-enabled [NAME…] Check whether unit files are enabled
mask [NAME…] Mask one or more units
unmask [NAME…] Unmask one or more units
link [PATH…] Link one or more units files into the search path
get-default Get the name of the default target
set-default NAME Set the default target

单一服务目前的几种状态:
active(running):有一只或多只程序正在系统执行
active(exited):仅执行一次就正常结束服务,目前并没有任何程序在系统中执行。
active(waiting):正在执行当中,不过还在等待其他事件才能继续 处理。
inactive:这个服务没有运作

单一服务的预设状态:
enabled:这个daemon在开机时被执行
disabled:这个daemon在开机时不被执行
static:这个daemon不可以自己启动,不过可能被其他的enabled服务来唤醒
mask:这个daemon无论如何都无法被启动,可通过systemctl unmask 方式改回原状态

通过systemctl观察系统上的所有服务
使用 systemctl list-unit-files 命令会将所有的系统服务统统列出来,而不像list-units仅以unit分类作大致的说明。

当只想知道service 这种类别的daemon,需使用systemctl list-units --type=service --all命令来实现

通过systemctl管理不同的操作环境(target unit)
systemctl list-units --type=target --all 命令来实现
在target项目使用isolate隔离不同的操作模式,而service使用start/stop/restart实现。
其中还有
systemctl poweroff 系统关机
systemctl reboot 重新启动
systemctl suspend 暂停模式
systemctl hibernate 休眠模式
systemctl rescue 救援模式
systemctl emergency 紧急救援模式

通过systemctl分析个服务之间的相依性
systemctl list-dependencies 查看我依赖谁服务
systemctl list-dependencies --reverse 查看谁依赖我的服务

systemd运作相关文件介绍
/usr/lib/systemd/system/
使用centos官方提供的软件安装后,默认的启动脚本配置文件放在此处

/run/systemd/system/
系统执行过程中所产生的服务脚本

/etc/systemd/system/:
管理员依据主机系统的需求所建立的执行脚本

/etc/sysconfig/*
几乎所有的服务都会将初始化的一些选项设定写入到这个目录下。

/var/lib/
一些会产生数据的服务都会将他的数据写入到此目录中。

/run
防止好多daemon的暂存档

systemctl list-sockets 查看服务产生的socket文件

网络服务和端口详解
cat /etc/services 命令查看对应服务使用的协议和端口
netstat -tlunp 命令查看网络端口

以sshd.service服务为例
[root@localhost system]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service

[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

配置文件分为三部分:
【unit】
本身的说明,以及与其他相依daemon的设定,包括在什么服务后才能启动此unit

【Service】
不同的unit type 就要使用相对应的设定项目。因为使用的是sshd例子所以是【service】的设定。这个项目被主要在规范服务启动脚本、环境配置文件名、重新启动的方式等。

【install】
这个项目代表将次unit安装到哪个taget里。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值