Linux中systemctl怎么用
这篇文章给大家分享的是有关Linux中systemctl怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
我们最常使用的ssh服务,系统默认ssh服务22端口,我现在想再开一个ssh服务,端口8888
1.系统服务启动脚本 /usr/lib/systemd/system/sshd.service,将其复制到 /etc/systemd/system/ 下,并改名为 sshd2.service,文件内容如下:
[Unit]Description=OpenSSHserverdaemonDocumentation=man:sshd(8)man:sshd_config(5)After=network.targetsshd-keygen.serviceWants=sshd-keygen.service[Service]Type=notifyEnvironmentFile=/etc/sysconfig/sshdExecStart=/usr/sbin/sshd-D$OPTIONSExecReload=/bin/kill-HUP$MAINPIDKillMode=processRestart=on-failureRestartSec=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,并修改端口那一行
Port8888
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/bashlogdate=$(date+%s)logdir="/root/log/"logname=fancy.${logdate}.log#echo$lognametouch${logdir}${logname}
意思是,运行该服务时,在 /root/log/ 目录下创建一个日志文件
2.在 /etc/systemd/system/ 下创建启动脚本 fancy_test.service,输入一下内容
[Unit]Description=fancy_testserverdaemon[Service]Type=simpleExecStart=/root/bin/fancy_test.sh[Install]WantedBy=multi-user.target
3.运行命令 systemctl reload
4.运行命令 systemctl start fancy_test.service
5.此时你会看到在 /root/log/ 目录下创建了一个日志文件
感谢各位的阅读!关于“Linux中systemctl怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。