/etc/rc.local 和 /etc/init.d/rc.local 有什么区别?

gri*_*yvp 31 startup configuration

我想永久添加iptables规则到我的新VPS,并简要谷歌搜索后,我感到惊讶的是有两个地方可以添加这一规则,这似乎是相同的:/etc/rc.local/etc/init.d/rc.local。也许有人知道为什么简单的启动代码放两个地方?它是特定于 linux 的风格吗(但 ubuntu 两者都有!)?或者其中之一已被弃用?

gol*_*cks 32

/etc/init.d在 ubuntu 上维护以与 sysvinit 东西向后兼容。如果您实际查看,/etc/init.d/rc.local您会看到(同样来自 12.04 LTS 服务器):

#! /bin/sh
### BEGIN INIT INFORMATION
# Provides:          rc.local
# Required-Start:    $remote_fs $syslog $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO
Run Code Online (Sandbox Code Playgroud)

“运行/etc/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
Run Code Online (Sandbox Code Playgroud)

我猜想这样做的目的是提供一个简单的地方来放置你想在启动时运行的 shell 命令,而不必处理/etc/init.d/rc.local.

所以它实际上是一个服务,可以这样运行。我echo/etc/rc.local和添加了一行:

»service rc.local start
hello world
Run Code Online (Sandbox Code Playgroud)

但是,我不相信 upstart 的/etc/init(不是 init.d!)目录中的任何内容引用它:

»initctl start rc.local
initctl: Unknown job: rc.local
Run Code Online (Sandbox Code Playgroud)

新贵中有一些“rc”服务:

»initctl list | grep rc
rc stop/waiting
rcS stop/waiting
rc-sysinit stop/waiting
Run Code Online (Sandbox Code Playgroud)

但这些似乎都与 rc.local 无关。


小智 5

这更像是一个特定于发行版的事情。(比如,你不会在 CentOS 中找到不同的 rc.local)。

现在回到您的实际问题,我认为在 /etc/init.d/rc.local 中添加任何内容使其作为“服务”启动,而 /etc/rc.local 中的任何内容都会在启动时简单地启动该脚本。

我不太确定为什么 Ubuntu 仍然维护它们?(也许其他人可能会对此部分有所了解!!)

  • @pragmatic 因为`/etc/rc.local` 脚本是由`/etc/initd/rc.local` 脚本管理的可执行进程,就像(例如)`/bin/syslog` 将是由`/etc/initd/syslog`。你明确说`/etc/rc.local` 只是一个引导脚本,而`/etc/initd/rc.local` 是一个完全独立的运行级服务。 (2认同)