一、说明
有的一些Linux没有/etc/rc.local文件,该文件主要是用于设置系统开机后的一些自定义初始设置。当缺失时若需要可以自行添加和配置。
二、配置
1、创建rc.local文件
在/etc/目录下创建rc.local文件,内容如下:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# 在此自行添加想开机启动就执行的脚本或逻辑
然后给/etc/rc.local添加可执行权限:
chmod 755 /etc/rc.local
(1)设置所有网卡自动启动
可以在rc.local添加开机启动逻辑,【脚本】 【Linux】启动所有网卡
# 启动所有网卡
/lib/ifupdown/ifup_ethernets.sh
(2)设置网卡DHCP
参考:Linux下网络配置 中的DHCP配置方法。
2、创建rc-local.service服务文件
在/etc/systemd/system/目录下创建服务文件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.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
3、设置服务开机自启
systemctl enable rc-localsystemctl daemon-reload
4、启动服务
systemctl start rc-localsystemctl status rc-local
文章介绍了如何在Linux系统中创建并配置rc.local文件以实现开机自启动脚本,同时设置了网卡的自动启动和DHCP配置。通过创建rc-local.service服务文件,并设置开机自启,确保了rc.local在系统启动时执行。此外,文章还提供了启用和检查服务的相关命令。
2909

被折叠的 条评论
为什么被折叠?



