linux /etc/init.d 目录

本文详细介绍了Linux系统中/etc/init.d目录的功能及其使用方法,包括如何通过命令手动控制目录内的启动/停止脚本,以及与/etc/rc.local脚本的区别。

转自:http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/

Get To Know Linux: The /etc/init.d Directory

By Jack Wallen on April 4, 2009 - TAGS: None

If you use Linux you most likely have heard of the init.d directory. But what exactly does this directory do? It ultimately does one thing but it does that one thing for your entire system, soinit.d is very important. The init.d directory contains a number of start/stop scripts for various services on your system. Everything fromacpid to x11-common is controlled from this directory. Of course it's not exactly that simple.

If you look at the /etc directory you will find directories that are in the formrc#.d (Where # is a number reflects a specific initialization level - from 0 to 6). Within each of these directories is a number of other scripts that control processes. These scripts will either begin with a "K" or an "S". All "K" scripts are run before "S" scripts. And depending upon where the scripts are located will determine when the scripts initiate. Between the directories the system services work together like a well-oiled machine. But there are times when you need to start or stop a process cleanly and without using the kill or killall commands. That is where the/etc/init.d directory comes in handy.

Now if you are using a distribution like Fedora you might find this directory in/etc/rc.d/init.d. Regardless of location, it serves the same purpose.

In order to control any of the scripts in init.d manually you have to have root (or sudo) access. Each script will be run as a command and the structure of the command will look like:

/etc/init.d/command OPTION

Where command is the actual command to run and OPTION can be one of the following:

  • start
  • stop
  • reload
  • restart
  • force-reload

Most often you will use either start, stop, or restart. So if you want to stop your network you can issue the command:

/etc/init.d/networking stop

Or if you make a change to your network and need to restart it, you could do so with the following command:

/etc/init.d/networking restart

Some of the more common init scripts in this directory are:

  • networking
  • samba
  • apache2
  • ftpd
  • sshd
  • dovecot
  • mysql

Of course there may be more often-used scripts in your directory - it depends upon what you have installed. The above list was taken from a Ubuntu Server 8.10 installation so a standard desktop installation would have a few less networking-type scripts.

But what about /etc/rc.local

There is a third option that I used to use quite a bit. This option is the /etc/rc.local script. This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup. Many times I will place mounting instructions for things like nfs in this script. This is also a good place to place "troubleshooting" scripts in. For instance, once I had a machine that, for some reason, samba seemed to not want to start. Even afer checking to make sure the Samba daemon was setup to initialize at boot up. So instead of spending all of my time up front with this I simply placed the line:

/etc/init.d/samba start

in the /etc/rc.local script and Samba worked like a charm. Eventually I would come back and trouble shoot this issue.

Final Thoughts

Linux is flexible. Linux is so flexible there is almost, inevitably, numerous ways to solve a single problem. Starting a system service is one such issue. With the help of the/etc/init.d system (as well as /etc/rc.local) you can pretty much rest assured your service will start.

Enjoyed the article?: Then sign-up for our free newsletter or RSS feed to kick off your day with the latest technology news and tips, or share the article with your friends and contacts on Facebook, Twitter or Google+ using the icons below.
<think>首先,用户的问题是关于“/etc/rc.d/init.d/ 配置文件目录”的介绍。我需要基于提供的引用内容来回答,并确保回答结构清晰、逐步解决问题。 回顾引用内容: - 引用[1] 和 [2] 描述了 /etc/rc.d/ 目录的结构,包括 init.d/、rcx.d/ 目录和一些脚本如 rc.sysinit, rc, rc.local。 - 引用[3] 比较了 /etc/init.d 和 /etc/rc.d/rc.local,提到了 systemd 的普及。 - 引用[4] 详细描述了 Linux 启动顺序,包括 init 进程如何读取 /etc/inittab 并根据运行级别在 /etc/rc*.d/ 目录中执行脚本。 关键点: - /etc/rc.d/init.d/ 目录包含各种服务器和程序的启动脚本(二进制文件或脚本)。 - 这些脚本用于启动、停止、重启服务。 - rcx.d/ 目录(如 rc3.d/)包含指向 init.d/ 目录中脚本的软链接,以 S(start)或 K(kill)开头,表示启动或停止服务。 - 启动顺序由运行级别决定,脚本按顺序执行。 用户特别询问 /etc/rc.d/init.d/ 目录,所以我需要专注于这个目录的介绍。 结构化回答: 1. **介绍目录作用**:解释 /etc/rc.d/init.d/ 是什么,比如它存放系统服务的启动脚本。 2. **目录内容**:描述其中的脚本类型,如用于管理服务的二进制文件或脚本。 3. **与其他目录的关系**:链接到 rcx.d/ 目录,这些是软链接指向 init.d/ 中的脚本。 4. **使用方式**:如何通过这些脚本来管理服务(启动、停止等)。 5. **启动顺序**:简要提到在系统启动过程中,这些脚本如何被执行,基于运行级别。 6. **现代系统的影响**:提到 systemd 的普及,但 /etc/rc.d/init.d/ 在传统 SysV init 系统中仍然重要。 确保回答真实可靠:基于提供的引用,避免添加未提及的信息。 引用标识:在回答中适当添加引用标识,如 [^1], [^2], 等。 生成相关问题:在结尾添加相关问题列表。 最后,使用中文回答。 完整回答结构: - 开头:直接回答问题。 - 主体:分步骤或分点解释。 - 结尾:生成相关问题。 </think> ### /etc/rc.d/init.d/ 配置文件目录介绍 /etc/rc.d/init.d/ 是 Linux 系统中用于存储系统服务启动脚本的核心目录,特别是在使用传统的 SysV init 初始化系统的发行版中(如早期的 CentOS、Red Hat 等)。这些脚本负责管理系统服务的启动、停止、重启和状态查询。以下是基于引用内容的详细说明,我将逐步解释其作用、结构和使用方式。 #### 1. **目录作用** - /etc/rc.d/init.d/ 包含各种系统服务和应用程序的启动脚本(通常是 Shell 脚本或二进制文件)。这些脚本用于控制服务的生命周期,例如启动 Apache 服务器或停止 MySQL 服务。 - 这些脚本不是配置文件本身,而是可执行的"控制脚本"。它们定义了服务的启动参数、依赖关系和操作命令(如 `start`, `stop`, `restart`)。 - 在系统启动或运行级别切换时,这些脚本通过软链接被调用,确保服务按需加载[^1]。 #### 2. **目录内容** - **脚本类型**:目录中的文件通常是服务特定的脚本,例如: - 系统服务:如 `httpd`(Apache)、`sshd`(SSH 服务)、`network`(网络服务)。 - RPM 包安装的服务:当通过 RPM 包管理器安装软件时,对应的启动脚本会存放在此目录下[^2]。 - **脚本功能**:每个脚本支持标准参数: - `start`:启动服务。 - `stop`:停止服务。 - `restart`:重启服务。 - `status`:检查服务状态。 例如,执行 `/etc/rc.d/init.d/sshd start` 会启动 SSH 服务。 #### 3. **与其他目录的关系** - **rcx.d/ 目录**:在 /etc/rc.d/ 下还有子目录如 rc0.d/, rc1.d/, ..., rc6.d/,对应不同的运行级别(runlevel)。这些目录包含指向 init.d/ 脚本的软链接: - 链接以 `S`(Start)开头表示启动服务,例如 `S55sshd`。 - 链接以 `K`(Kill)开头表示停止服务,例如 `K10network`。 - 数字表示执行顺序(从小到大),系统启动时按顺序运行 `S*` 脚本,关机时运行 `K*` 脚本[^1][^4]。 - **关键脚本**:/etc/rc.d/ 目录还包含全局脚本: - `rc.sysinit`:系统初始化脚本,执行硬件检测和基本设置。 - `rc`:根据运行级别调用对应的 rcx.d/ 脚本。 - `rc.local`:在所有服务启动后执行用户自定义命令,适合简单任务(如启动自定义进程)[^3]。 #### 4. **在系统启动中的角色** - **启动顺序**:在 Linux 启动过程中: 1. 内核加载后,init 进程读取 /etc/inittab 文件,确定默认运行级别(例如多用户图形界面是级别 5)。 2. init 进程执行 /etc/rc.d/rc.sysinit 进行系统初始化。 3. 根据运行级别,执行 /etc/rc.d/rcX.d/(X 为运行级别)目录中的脚本:先运行 `S*` 链接指向的 init.d/ 脚本以启动服务。 4. 最后执行 /etc/rc.d/rc.local 完成用户自定义任务[^4]。 - **运行级别示例**:如果默认运行级别是 5,系统会优先执行 /etc/rc5.d/ 中的 `S*` 链接,这些链接指向 /etc/rc.d/init.d/ 中的实际脚本。 #### 5. **现代系统的变化** - 随着 systemd 的普及(在 Ubuntu、CentOS 7+ 等系统中),/etc/rc.d/init.d/ 的重要性降低。systemd 使用单元文件(如 .service 文件)替代传统脚本,提供更高效的服务管理。 - 但 /etc/rc.d/init.d/ 仍被保留用于兼容性。许多系统支持混合模式:systemd 可以调用这些脚本(通过 `systemctl` 命令),而 rc.local 也常用于快速添加启动任务。 #### 6. **使用方法示例** - **手动管理服务**:作为 root 用户,可以直接操作脚本: ```bash # 启动 HTTP 服务 sudo /etc/rc.d/init.d/httpd start # 查看服务状态 sudo /etc/rc.d/init.d/httpd status ``` - **自动管理工具**:使用 `chkconfig` 命令配置服务在特定运行级别自启: ```bash # 添加 httpd 服务到运行级别 3 和 5 自启 sudo chkconfig --level 35 httpd on ``` 这会自动在 /etc/rc3.d/ 和 /etc/rc5.d/ 中创建 `S*` 链接。 总之,/etc/rc.d/init.d/ 是传统 Linux 系统中服务管理的核心目录,通过脚本和软链接实现服务的动态控制。尽管 systemd 正在取代它,但在维护旧系统或特定场景(如自定义服务脚本)时,它仍然非常有用[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值