在 Debian 及 Ubuntu 开机后, 如果想自动执行一些 shell script 或指令, 可以直接编辑 /etc/rc.local 档案.
以下是 /etc/rc.local 默认内容:
|
1 2 3 4 5 6 |
#!/bin/sh -e # # rc.local - executed at the end of each multiuser runlevel # # Make sure that the script will "exit 0" on success or any other # value on error. |
要加入自动执行指令或 shell script 十分简单, 只要直接加上要执行的指令即可, 例如我设定了每次开机会自动恢复 iptables 的设定, 便加入了 iptables-restore 指令:
|
1 2 3 4 5 6 7 8 |
#!/bin/sh -e # # rc.local - executed at the end of each multiuser runlevel # # Make sure that the script will "exit 0" on success or any other # value on error. /sbin/iptables-restore < /root/iptables-save |
储存盘案后就完成了, 下次开机便会自动执行加入的指令.
————————————
11-Jun-2018 更新:
在 Ubuntu 16.10 及 17.10 已经没有 /etc/rc.local 这个档案了, 如果不想逐一透过 systemd 启动服务, 可以用以下方法自行建立 /etc/rc.local:
建立启动 /etc/rc.local 的 systemd 档案, 这里只设定执行 /etc/rc.local 的内容:
$ sudo vim /etc/systemd/system/rc-local.service
加入以下内容:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=/etc/rc.local Compatibility ConditionPathExists=/etc/rc.local [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 StandardOutput=tty RemainAfterExit=yes SysVStartPriority=99 [Install] WantedBy=multi-user.target |
把上面的 rc-local 设定成开机自动执行:
$ sudo systemctl enable rc-local.service
执行以下指令建立 /etc/rc.local 档案, 及给予可执行权限:
$ sudo touch /etc/rc.local
$ echo ‘#!/bin/bash\n’ >> /etc/rc.local
$ sudo chmod +x /etc/rc.local
$ echo ‘#!/bin/bash\n’ >> /etc/rc.local
$ sudo chmod +x /etc/rc.local
这样就可以像以往一样, 将要开机执行的指令放到 /etc/rc.local 档案.
看你这个编辑器感觉好累的说,有没想过换一个啊
Ubuntu 17.10 OS默认内并没有这个档案