1.权限设置(/etc/rc.local和/etc/rc.d/rc.local都需要赋予执行权限)

chmod +x /etc/rc.d
chmod +x /etc/rc.d/rc.local

2.设置服务启动脚本。 1)进入路径

cd /usr/lib/systemd/system

2)编辑服务脚本

vi 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 Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

3.使用命令启动服务(确认服务可用)

systemctl start rc-local

使用systemctl -t service查看服务器所有服务的状态,确认服务rc-local.service是否运行正常. 4.执行systemctl enable rc-local

systemctl enable rc-local

出现以下提示表示成功添加开机自启动: Created symlink from /etc/systemd/system/multi-user.target.wants/rc-local.service to /usr/lib/systemd/system/rc-local.service.

如下图:

完成以上步骤就可以将开机自动执行的命令写入/etc/rc.d/rc.local中,实现开机自动执行。