> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/4.3-bsd-unix-cao-zuo-xi-tong-she-ji-yu-shi-xian/di-wu-bu-fen-xi-tong-yun-xing/13.4.-yong-hu-ji-chu-shi-hua.md).

# 13.4 用户级初始化

1 号进程开始执行时，系统的大部分已经运作。从这一点到用户看到登录提示之间还有若干步骤，全部由使用前几章描述的标准 UNIX 系统调用接口的用户级程序完成。我们简述典型 4.3BSD 系统中的步骤；不过要留意，这些步骤在各种 UNIX 系统间可能大相径庭。

**/etc/init**

**/etc/init** 程序作为自举过程的最后一步被调起。自举 UNIX 时指定的参数以依赖机器的方式传给 **init**：VAX 上，传给 **boot** 程序的标志经由传给内核的同一个硬件寄存器传给 **init**。**init** 用这些标志的值决定把系统带到单用户还是多用户运行。单用户运行时，**init** fork 一个调起标准 UNIX shell /bin/**sh** 的进程，该进程的标准输入、输出和错误描述符指向系统控制台终端 **/dev/console**。这个 shell 随后正常运行但持有超级用户特权，直到终止。

多用户运行时，**init** 先派生一个 shell 解释 **/etc/rc** 文件中的命令。这些命令做文件系统一致性检查、启动系统进程、初始化数据库文件（如含已挂载文件系统列表的 **/etc/mtab**）。**/etc/rc** 脚本成功完成后，**init** 为 **/etc/ttys** 文件中标记使用的每台终端设备 fork 自己的一个副本。这些 **init** 副本调用 **/etc/getty** 等其他系统程序管理标准的签到过程。1 号进程始终充当系统运行的主协调进程：终端会话终止时派生新进程，并管理运行中系统的关闭。

**/etc/rc**

**/etc/rc** 命令脚本先检查各文件系统的完好。这一检查为确保此前系统故障可能造成的损坏得到修复：内核中的文件系统支持只管读写既有文件系统；文件系统中的任何不一致都由用户级程序修复。

**/etc/fsck** 程序是检查文件系统一致性、修复损坏文件系统的主要工具。正常情况下，**fsck** 由 **/etc/rc** 脚本调起，在每个文件系统挂载前检查修复。内核在 **/etc/rc** 被调起前挂载的根文件系统若需修复，4.3BSD 必须重启自己，确保缓冲缓存中的数据与文件系统中的数据一致。这一重启操作自动完成，见后文。

文件系统检查之后，挂载各文件系统，启用用于交换和调页的设备；随后检查启用磁盘配额，系统启动实现各种系统服务的后台进程。这些进程包括 **/etc/update**（每 30 秒把缓冲缓存的磁盘写冲到盘上的程序）、/etc/cron（按周期性时间间隔执行命令的程序）、/etc/accton（启用系统记账的程序）、/etc/syslogd（系统错误日志进程）。其中一些进程从命令脚本 /etc/rc.local 启动：/etc/rc.local 中的命令按各主机需要裁剪，/etc/rc 中的命令则对所有主机共同。例如，提供网络服务的进程典型地从 /etc/rc.local 命令文件启动。

**/etc/getty**

/etc/getty 程序由 /etc/init 为系统的每条硬件终端线路派生，负责打开并初始化终端线路。打开终端使系统指派一个进程组（进程若已有进程组，**getty** 先用 `vhangup` 系统调用清零）。**getty** 程序设置终端线路的初始参数，判明接在线路上的终端类型。接调制解调器的线路，**getty** 可以被指示按多种波特率接受连接。**getty** 靠响应断开字符或成帧错误（通常由用户敲断开键引起）改变终端线路速度来选出波特率；用户可以连续敲断开键，在多个线路速度间轮转直到找到正确的。**getty** 的动作由位于 /etc/gettytab 文件的终端配置数据库驱动。

**getty** 最后读入登录名，调用 /bin/login 程序完成登录序列。

**/bin/login**

**login** 程序负责把用户签到系统上，通常由 /etc/getty 带想登录的用户名调起。**login** 提示用户输入口令（可能的话先关掉终端回显）。用户提供的口令加密后与主口令文件 /etc/passwd 中存的值相同，**login** 就向各记账文件写入签到记录，把用户和组标识符初始化为口令和 /etc/group 文件中指定的值，切换到用户的登录目录；最后 **login** 用 `exec` 以用户的 shell 覆盖自己。

用户经网络连接进入系统时也调起 **login** 程序；这种连接绕过 **getty** 和 **init**。在加入这一复杂化（4.2BSD）之前，系统可以靠注意到 shell 的父进程是 **init** 来识别登录 shell——这对记账和限制同时登录数很有用。为此在 proc 结构中加入了 SLOGIN 标志，由 **login** 设置来标记登录 shell。4.3BSD 的 **login** 不设置也不使用这个标志，但该标志已在别处使用 \[Elz, 1984]。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/4.3-bsd-unix-cao-zuo-xi-tong-she-ji-yu-shi-xian/di-wu-bu-fen-xi-tong-yun-xing/13.4.-yong-hu-ji-chu-shi-hua.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
