Centos7 开启启动nat123
官方教程添加到启动项里面
- 参考: 官方教程地址
- sudo find / -name rc.local
2.sudo cd /etc/rc.lcal
注意: 这里可能不是这个路径,由上面指令查找到为准 - sudo vi /etc/rc.lcal
- 将
cd /soft/nat123
mono nat123linux.sh service &
复制到文件里面
- sudo shutdown -r now
重启服务器 - 服务器启动后 ps -ef | grep nat
出现 nat123linux.sh service 则启动成功,如果没有则需要修改centos配置
重启不生效解决方案
参考:博客地址
-
在centos7之前,都习惯将自己的程序启动命令添加到/etc/rc.local,可是到centos7上面发现不生效了。难道不兼容了?系统默认应该是装了systemd-sysvcompat来兼容sysv的服务的。
-
rc-local.service
在centos7上面默认是使用systemd的,所以rc.local这个文件应该也是通过systemd的service启动的,可以在/usr/lib/systemd/system/rc-local.service找到这个service文件,查看里面的内容:
cat /usr/lib/systemd/system/rc-local.service
# This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version.# This unit gets pulled automatically into multi-user.target by # systemd-rc- local-generator if /etc/rc.d/rc.local is executable. [Unit]Description=/etc/rc.d/rc.local
CompatibilityConditionFileIsExecutable=/etc/rc.d/rc.localAfter
=network.target[Service]Type=forkingExecStart=/etc/rc.d/rc.lcal startTimeoutSec=0RemainAfterExit=yes -
分析原因
service是通过ExecStart=/etc/rc.d/rc.local start来启动的,所以这个文件需要有执行权限。可是ls -l 查看到/etc/rc.local是一个软连接连接到当前目录的rc.d/rc.local,接着查看/etc/rc.d/rc.local,发现没有执行权限,那我们就给这个文件执行权限:
chmod +x /etc/rc.d/rc.local
开启 rc-local.service 服务:
systemctl enable rc-local.service
systemctl start rc-local.service
-
Systemctl enable rc-loacl.service 不成功?
systemctl enable rc-local.service
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
- A unit may be statically enabled by being symlinked from another unit’s
.wants/ or .requires/ directory.
- A unit’s purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
- A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, …).
提示启动service里面没有install这节的内容。那就给它通过多用户的target启动就可以了。
vim /usr/lib/system/system/rc-local.service
[Install]
WantedBy=multi-user.target
然后再次enable启动服务:
systemctl enable rc-local.service
ln -s ‘/lib/systemd/system/rc-local.service’ ‘/etc/systemd/system/multi-user.target.wants/rc-local.service’
-
systemd还是sysv?
systemd用起来还是没有sysv顺手,但是个人感觉centos7还是比较推荐让我们自己去写service文件,而不是去添加到rc.local的文件,毕竟添加到这个文件里面,启动就变成了单线程了。如果是自己写service文件可以根据实际情况,跟一些服务并行启动,提升开机速度。个人观点,你觉得是如何呢?