设置开机自启rc.local

文章目录

  • 一、说明
  • 二、配置
    • 1、创建rc.local文件
      • (1)设置所有网卡自动启动
      • (2)设置网卡DHCP
    • 2、创建rc-local.service服务文件
    • 3、设置服务开机自启
    • 4、启动服务

一、说明

有的一些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.

# 在此自行添加想开机启动就执行的脚本或逻辑

exit 0

然后给/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

你可能感兴趣的:(Linux,linux,服务器,运维)