一、运行级别的分类 runlevel
二、管理服务 systemctl (root权限)
三.一些常用命令 systemctl
-
查看当前已经启动的服务 systemctl list-units
-
查看所有服务 systemctl list-unit-files
-
查看服务有哪些依赖 systemctl list-dependencies xx.service
-
查看服务有哪些依赖(反向) systemctl list-dependencies –reverse xx.service
四.system 服务相关的一些目录( Centos 环境,Debian 类的环境可能会有稍许不同)
-
/usr/lib/systemd/system/ 系统安装的软件默认启动脚本目录
-
/etc/systemd/system/ 用户根据自己需要建立的启动脚本目录
-
/etc/sysconfig/ 服务初始化选项目录
-
/var/lib/ 服务运行时产生的数据存储目录
-
/etc/xxx/ 各服务配置目录
五.结合一个例子来具体讲解,一台机开启两个ssh服务
我们最常使用的ssh服务,系统默认ssh服务22端口,我现在想再开一个ssh服务,端口8888
1.系统服务启动脚本 /usr/lib/systemd/system/sshd.service,将其复制到 /etc/systemd/system/ 下,并改名为 sshd2.service,文件内容如下:
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
因为要重启一个新的服务,所以要修改一下ExecStart这一行,读取新的配置文件 sshd2_config,改为
ExecStart=/usr/sbin/sshd -D $OPTIONS -f /etc/ssh/sshd2_config
2.到 /etc/ssh/ 下,将 sshd_config 复制到 sshd2_config,并修改端口那一行
Port 8888
3.运行命令 systemctl reload 重新加载一下配置
4.运行命令 systemctl status sshd2.service 查看状态
5 运行命令 systemctl start sshd2.service 开启服务
6.运行命令 systemctl enable sshd2.service 设置开机启动
7.在另一台机器上登录 ssh fancy@ip -p8888 就可以登录了
六、我们再来举个例子,做一个自己的服务
1.在 /root/bin/ 下创建一个shell脚本 fancy_test.sh,并修改其权限,chmod u+x fancy_test.sh,内容如下
#!/bin/bash
logdate=$(date +%s)
logdir="/root/log/"
logname=fancy.${logdate}.log
#echo $logname
touch ${logdir}${logname}
意思是,运行该服务时,在 /root/log/ 目录下创建一个日志文件
2.在 /etc/systemd/system/ 下创建启动脚本 fancy_test.service,输入一下内容
[Unit]
Description=fancy_test server daemon
[Service]
Type=simple
ExecStart=/root/bin/fancy_test.sh
[Install]
WantedBy=multi-user.target
3.运行命令 systemctl reload
4.运行命令 systemctl start fancy_test.service
5.此时你会看到在 /root/log/ 目录下创建了一个日志文件
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)