Debian 12 解决 /etc/rc.local 开机启动问题

在 Debian 12(以及 Debian 9 及以上版本)中,/etc/rc.local 是配置开机自启动脚本的传统方式,但默认未启用,导致自定义脚本无法自动运行。本文将详细指导你在 Debian 12 上启用和配置 /etc/rc.local,步骤同样适用于 Debian 9 Stretch、10 Buster 和 11 Bullseye

问题背景:rc.local 为什么不生效?

Debian 9 起采用 systemd 作为初始化系统,传统的 /etc/rc.local 默认不生效。尽管系统内置了 rc-local,但默认处于禁用状态:

root@debian:~$ systemctl status rc-local.service
○ rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; static)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: inactive (dead)
       Docs: man:systemd-rc-local-generator(8)

以下是默认的 rc-local.service 配置,表明它会在 /etc/rc.local 可执行时自动拉起:

root@docker:~$ systemctl cat rc-local.service
# /lib/systemd/system/rc-local.service
#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  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
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

# /usr/lib/systemd/system/rc-local.service.d/debian.conf
[Unit]
# not specified by LSB, but has been behaving that way in Debian under SysV
# init and upstart
After=network-online.target

# Often contains status messages which users expect to see on the console
# during boot
[Service]
StandardOutput=journal+console
StandardError=journal+console

解决步骤:启用 /etc/rc.local

以下步骤助你快速启用 /etc/rc.local,实现开机脚本自动运行

创建 /etc/rc.local 文件

默认没有 /etc/rc.local,我们需要手工添加一个 /etc/rc.local 文件:

cat <<EOF >/etc/rc.local
#!/bin/bash
# 这是一个示例 rc.local 文件
# 在这里添加你的开机执行命令
# 示例:启动一个自定义脚本
# /path/to/your/script.sh
exit 0
EOF

设置可执行权限

确保文件具有可执行权限

chmod +x /etc/rc.local

启用并立即启动 rc-local 服务

启动 rc-local 服务,此时可能会弹出警告,可直接忽略

systemctl enable --now rc-local

检查服务状态, 状态显示 active (exited) 表示服务已运行

root@docker:~$ systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; static)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Fri 2025-05-02 18:21:26 EDT; 3s ago
       Docs: man:systemd-rc-local-generator(8)
    Process: 333116 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
        CPU: 4ms

May 02 18:21:26 docker systemd[1]: Starting rc-local.service - /etc/rc.local Compatibility...
May 02 18:21:26 docker systemd[1]: Started rc-local.service - /etc/rc.local Compatibility.

添加自定义开机脚本

在 /etc/rc.local 的 exit 0 前添加命令。例如:

#!/bin/sh -e
#
# rc.local
#
# By default this script does nothing.
/data/scripts/ip.sh || true
exit 0

注意:使用绝对路径(如 /data/scripts/ip.sh),确保脚本有执行权限。|| true 可防止脚本失败影响。

测试配置

重启系统

总结

通过以上步骤,你可以在 Debian 9 及以上版本快速启用 /etc/rc.local,实现开机自动运行脚本。尽管 systemd 提供更现代的方案,rc.local 仍适合简单任务


Chapters

Sponsor

Like this article? $1 reward

Comments