解决linux的开机启动不生效

近偶然间发现加到 /etc/rc.local中的开机启动命令没有生效,于是排查了一下解决了,记录一下处理的过程

主要步骤

  1. ls /lib/systemd/system 你可以看到有很多启动脚本,其中就有我们需要的 rc.local.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cat /lib/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

  1. 创建 /etc/rc.local把需要启动命令写进去
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vim /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之前

exit 0

注:

  • debian10默认是没有这个文件,直接创建保存。
  • 主要就是第一句和最后一句
  1. 赋予执行权限,否则的话会导致开机后不运行

    1
    sudo chmod +x /etc/rc.local
  2. /etc/systemd/system 目录下创建软链接

    1
    ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/ 
  3. reboot测试是否生效