一、说明

有的一些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-local
  • systemctl daemon-reload

4、启动服务

  • systemctl start rc-local
  • systemctl status rc-local
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐