以下命令需要root用户权限.
新版本的Ubuntu和debian已经默认停用了原有的rc.local开启启动的功能,可能是出于安全或者相关的原因,但并没有完全移除这个功能,我们只要做点小改动就可以重新启用这个功能,因为它确实太有用了.
新系统并不是没有替代方案,但是显得繁琐,用惯了rc.local的人无法适应.
现在我们来重新启用它,回到我们熟悉的状态.
1.我们看新版rc-local.service是什么状况:
cat /lib/systemd/system/rc-local.service
显示的结果如下:
# SPDX-License-Identifier: LGPL-2.1+ # # 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.local is executable. [Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no
我们在以上文本末尾添加几行:
vi /lib/systemd/system/rc-local.service
# added by yourself [Install] WantedBy=multi-user.target Alias=rc-local.service
保存退出
:wq!
2. 打开 /etc/rc.local 输入:
vi /etc/rc.local
在这个文档里你可以配置开机启动的命令或者脚本,就和以前的版本一样,作者是用来开机启动生效iptables规则:
#!/bin/sh iptables-restore < /etc/iptables.up.rules exit 0
保存退出:
:wq!
配置权限:
chmod 755 /etc/rc.local
3. 配置rc.local服务开机启动:
systemctl enable rc.local
systemctl start rc.local
systemctl status rc-local.service
4. 重启
reboot