修改配置文件
打开位于/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
在文件末尾添加如下内容保存:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
创建软链接
Ubuntu18.04在启动时会读取/etc/systemd/system
目录下的配置文件,因此在修改rc-local.service后,需要在/etc/systemd/system目录下创建软链接指向配置文件,执行的命令如下:
sudo ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
创建自启脚本
在/etc/
目录下新建文件rc.local
同时修改权限,命令如下:
sudo touch /etc/rc.local
sudo chmod 755 /etc/rc.local
键入要执行的自启命令,系统在开机时会以root
用户执行此脚本,记得在脚本的开头添加#!/bin/bash
,示例文件如下:
#!/bin/bash
echo "hello world!" > /var/test.log
重启后可查看命令是否执行,以确认自启脚本配置成功。