ubuntu18.04 改用 systemd 管理启动系统,虽说对于系统来说更加规范,但是丢失了 rc.local 的便捷。

所以,虽然默认没有 rc.local 了,但是系统仍然保留了启动 rc.local 的服务,只要启用就可以重回 rc.local 了。

首先创建软链接

Bash
ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service

然后编辑该文件,添加最后的 [Install] 块

TEXT
vi /etc/systemd/system/rc-local.service

如下:

Bash
#  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
 
[Install]
WantedBy=multi-user.target
Alias=rc-local.service

创建 rc.local 文件

Bash
echo -e '#!/bin/bash' > /etc/rc.local

授可执行权

Bash
chmod 755 /etc/rc.local

设置服务

Bash
systemctl enable rc-local
systemctl start rc-local
systemctl status rc-local

然后就可以在 rc.local 里随便写点东西了,连之前的 exit 0 都不需要了。