1
0

network.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  4. <!ENTITY % general-entities SYSTEM "../general.ent">
  5. %general-entities;
  6. ]>
  7. <sect1 id="ch-scripts-network" revision="sysv">
  8. <?dbhtml filename="network.html"?>
  9. <title>一般网络配置</title>
  10. <indexterm zone="ch-scripts-network">
  11. <primary sortas="d-network">network</primary>
  12. <secondary>configuring</secondary></indexterm>
  13. <!--
  14. <para>This section only applies if a network card is to be
  15. configured.</para>
  16. <para>If a network card will not be used, there is likely no need to create
  17. any configuration files relating to network cards. If that is the case, you
  18. will need to remove the <filename class="symlink">network</filename> symlinks
  19. from all run-level directories (<filename
  20. class="directory">/etc/rc.d/rc*.d</filename>) after the bootscripts are
  21. installed in <xref linkend="ch-scripts-bootscripts"/>.</para>
  22. -->
  23. <sect2>
  24. <title>创建网络接口配置文件</title>
  25. <!--para>Which interfaces are brought up and down by the network script
  26. usually depends on the files in <filename
  27. class="directory">/etc/sysconfig/</filename>. This directory should
  28. contain a file for each interface to be configured, such as
  29. <filename>ifconfig.xyz</filename>, where <quote>xyz</quote> should describe
  30. the network card. The interface name (e.g. eth0) is usually appropriate.
  31. Inside this file are attributes to this interface, such as its IP
  32. address(es), subnet masks, and so forth. It is necessary that the stem of
  33. the filename be <emphasis>ifconfig</emphasis>.</para-->
  34. <para>负责网络的启动脚本根据
  35. <filename class="directory">/etc/sysconfig</filename> 中配置文件的内容,
  36. 决定应该启用或禁用哪些网络接口。对于每个需要配置的网络接口,
  37. 该目录中都应该包含一个文件,文件名类似
  38. <filename>ifconfig.xyz</filename>,
  39. 这里<quote>xyz</quote>应该能够描述该网卡,
  40. 使用接口名(如 eth0)一般比较合适。
  41. 文件内部是该网络接口的属性,如 IP 地址、子网掩码等。
  42. 文件名必须以 <emphasis>ifconfig</emphasis> 开头。</para>
  43. <!--note><para>If the procedure in the previous section was not used, Udev
  44. will assign network card interface names based on system physical
  45. characteristics such as enp2s1. If you are not sure what your interface
  46. name is, you can always run <command>ip link</command> or <command>ls
  47. /sys/class/net</command> after you have booted your system.
  48. </para></note-->
  49. <note><para>如果没有使用前一节描述的自定义命名策略,
  50. udev 会根据系统物理特征命名网卡接口,例如 enp2s1。
  51. 如果您不能确定接口名,可以在引导您的 LFS 系统后使用
  52. <command>ip link</command> 或 <command>ls /sys/class/net</command>
  53. 命令确认。</para></note>
  54. <para>作为示例,以下命令为
  55. <emphasis>eth0</emphasis> 设备创建一个静态 IP 地址配置:</para>
  56. <screen><userinput>cd /etc/sysconfig/
  57. cat &gt; ifconfig.eth0 &lt;&lt; "EOF"
  58. <literal>ONBOOT=yes
  59. IFACE=eth0
  60. SERVICE=ipv4-static
  61. IP=192.168.1.2
  62. GATEWAY=192.168.1.1
  63. PREFIX=24
  64. BROADCAST=192.168.1.255</literal>
  65. EOF</userinput></screen>
  66. <para>您必须修改文件中的变量,使配置与您的网络环境相匹配。</para>
  67. <!--para>If the <envar>ONBOOT</envar> variable is set to <quote>yes</quote> the
  68. System V network script will bring up the Network Interface Card (NIC) during
  69. booting of the system. If set to anything but <quote>yes</quote> the NIC
  70. will be ignored by the network script and not be automatically brought up.
  71. The interface can be manually started or stopped with the
  72. <command>ifup</command> and <command>ifdown</command> commands.</para-->
  73. <para>如果 <envar>ONBOOT</envar> 变量被设置为<quote>yes</quote>,
  74. 则 System V 网络脚本会在引导系统时启用该网络接口卡(NIC)。
  75. 否则,网络脚本会忽略该 NIC,不自动启用它。
  76. 您可以使用 <command>ifup</command> 和 <command>ifdown</command>
  77. 命令,手动启用或禁用网络接口。</para>
  78. <!--para>The <envar>IFACE</envar> variable defines the interface name,
  79. for example, eth0. It is required for all network device configuration
  80. files. </para-->
  81. <para><envar>IFACE</envar> 变量指定网络接口名,例如 eth0。
  82. 所有网络设备配置文件都需要它。</para>
  83. <!--para>The <envar>SERVICE</envar> variable defines the method used for
  84. obtaining the IP address. The LFS-Bootscripts package has a modular IP
  85. assignment format, and creating additional files in the <filename
  86. class="directory">/lib/services/</filename> directory allows other IP
  87. assignment methods. This is commonly used for Dynamic Host Configuration
  88. Protocol (DHCP), which is addressed in the BLFS book.</para-->
  89. <para><envar>SERVICE</envar> 变量定义获取 IP 地址的方法。
  90. LFS-Bootscripts 软件包使用模块化 IP 分配格式,在
  91. <filename class="directory">/lib/services/</filename>
  92. 目录中新建一些文件,即可使用其他 IP 分配方法,这一般被用于
  93. 动态主机配置协议(DHCP)配置,具体方法在 BLFS 手册中说明。</para>
  94. <!--para>The <envar>GATEWAY</envar> variable should contain the default
  95. gateway IP address, if one is present. If not, then comment out the
  96. variable entirely.</para-->
  97. <para>如果默认网关存在,
  98. <envar>GATEWAY</envar> 变量应该包含默认网关的 IP 地址。
  99. 如果默认网关不存在,应该将这一行完全注释掉。</para>
  100. <!--para>The <envar>PREFIX</envar> variable contains the number of
  101. bits used in the subnet. Each octet in an IP address is 8 bits. If the
  102. subnet's netmask is 255.255.255.0, then it is using the first three octets
  103. (24 bits) to specify the network number. If the netmask is 255.255.255.240,
  104. it would be using the first 28 bits. Prefixes longer than 24 bits are
  105. commonly used by DSL and cable-based Internet Service Providers (ISPs).
  106. In this example (PREFIX=24), the netmask is 255.255.255.0. Adjust the
  107. <envar>PREFIX</envar> variable according to your specific subnet.
  108. If omitted, the PREFIX defaults to 24.</para-->
  109. <para><envar>PREFIX</envar> 变量应该包含子网使用的 IP 地址位数。
  110. IP 地址中的每一段都是二进制为 8 位的数,如果子网掩码是
  111. 255.255.255.0,说明 IP 地址中前三段(24 位)表示子网编号。如果子网掩码是
  112. 255.255.255.240,则使用了前 28 位表示子网编号。
  113. 子网前缀比 24 长的情况一般见于基于 DSL 或同轴电缆的 Internet 服务提供商
  114. (ISP)。在我们的例子中,子网掩码是 255.255.255.0。
  115. 您应该根据您所处的子网调整 <envar>PREFIX</envar> 变量。
  116. 如果不指定它,则它默认为 24。</para>
  117. <para>参考 <command>ifup</command> 的 man 页面获得更多信息。</para>
  118. </sect2>
  119. <sect2 id="resolv.conf">
  120. <title>创建 /etc/resolv.conf 文件</title>
  121. <indexterm zone="resolv.conf">
  122. <primary sortas="e-/etc/resolv.conf">/etc/resolv.conf</primary>
  123. </indexterm>
  124. <para>系统需要某种方式,获取域名服务(DNS),
  125. 以将 Internet 域名解析成 IP 地址,或进行反向解析。
  126. 为了达到这一目的,最好的方法是将 ISP 或网络管理员提供的 DNS
  127. 服务器的 IP 地址写入
  128. <filename>/etc/resolv.conf</filename>。执行以下命令创建该文件:</para>
  129. <screen><userinput>cat &gt; /etc/resolv.conf &lt;&lt; "EOF"
  130. <literal># Begin /etc/resolv.conf
  131. domain <replaceable>&lt;您的域名&gt;</replaceable>
  132. nameserver <replaceable>&lt;您的主要域名服务器 IP 地址&gt;</replaceable>
  133. nameserver <replaceable>&lt;您的次要域名服务器 IP 地址&gt;</replaceable>
  134. # End /etc/resolv.conf</literal>
  135. EOF</userinput></screen>
  136. <para>可以省略 <varname>domain</varname> 语句,
  137. 或使用一条 <varname>search</varname> 语句代替它。
  138. 阅读 resolv.conf 的 man 页面了解更多细节。</para>
  139. <para>将
  140. <replaceable>&lt;域名服务器 IP 地址&gt;</replaceable>
  141. 替换为您的网络环境下最合适的 DNS 服务器 IP 地址。
  142. 这里往往会写入不止一个 DNS 服务器(需要提供后备功能的次要服务器)。
  143. 如果您只需要或只希望使用一个 DNS 服务器,可以删除文件中的第二个
  144. <emphasis>nameserver</emphasis> 行。也可以写入本地路由器的 IP 地址。
  145. </para>
  146. <note>
  147. <para>Google 公用 DNS 服务器的 IP 地址是 8.8.8.8 和 8.8.4.4。</para>
  148. </note>
  149. </sect2>
  150. <!--sect2 id="ch-scripts-hostname">
  151. <title>Configuring the system hostname</title>
  152. <indexterm zone="ch-scripts-hostname">
  153. <primary sortas="d-hostname">hostname</primary>
  154. <secondary>configuring</secondary>
  155. </indexterm>
  156. <para>During the boot process, the file <filename>/etc/hostname</filename>
  157. is used for establishing the system's hostname.</para>
  158. <para>Create the <filename>/etc/hostname</filename> file and enter a
  159. hostname by running:</para>
  160. <screen><userinput>echo "<replaceable>&lt;lfs&gt;</replaceable>" &gt; /etc/hostname</userinput></screen>
  161. <para><replaceable>&lt;lfs&gt;</replaceable> needs to be replaced with the
  162. name given to the computer. Do not enter the Fully Qualified Domain Name
  163. (FQDN) here. That information is put in the
  164. <filename>/etc/hosts</filename> file.</para>
  165. </sect2-->
  166. <!--sect2 id="ch-scripts-hosts">
  167. <title>Customizing the /etc/hosts File</title>
  168. <indexterm zone="ch-scripts-hosts">
  169. <primary sortas="e-/etc/hosts">/etc/hosts</primary>
  170. </indexterm>
  171. <indexterm zone="ch-scripts-hosts">
  172. <primary sortas="d-localnet">localnet</primary>
  173. <secondary>/etc/hosts</secondary>
  174. </indexterm>
  175. <indexterm zone="ch-scripts-hosts">
  176. <primary sortas="d-network">network</primary>
  177. <secondary>/etc/hosts</secondary>
  178. </indexterm>
  179. <para>Decide on the IP address, fully-qualified domain name (FQDN), and
  180. possible aliases for use in the <filename>/etc/hosts</filename> file. The
  181. syntax is:</para>
  182. <screen><literal>IP_address myhost.example.org aliases</literal></screen>
  183. <para>Unless the computer is to be visible to the Internet (i.e., there is
  184. a registered domain and a valid block of assigned IP addresses&mdash;most
  185. users do not have this), make sure that the IP address is in the private
  186. network IP address range. Valid ranges are:</para>
  187. <screen><literal>Private Network Address Range Normal Prefix
  188. 10.0.0.1 - 10.255.255.254 8
  189. 172.x.0.1 - 172.x.255.254 16
  190. 192.168.y.1 - 192.168.y.254 24</literal></screen>
  191. <para>x can be any number in the range 16-31. y can be any number in the
  192. range 0-255.</para>
  193. <para>A valid private IP address could be 192.168.1.1. A valid FQDN for
  194. this IP could be lfs.example.org.</para>
  195. <para>Even if not using a network card, a valid FQDN is still required.
  196. This is necessary for certain programs to operate correctly.</para>
  197. <para>Create the <filename>/etc/hosts</filename> file by running:</para>
  198. <screen><userinput>cat &gt; /etc/hosts &lt;&lt; "EOF"
  199. <literal># Begin /etc/hosts
  200. 127.0.0.1 localhost
  201. 127.0.1.1 <replaceable>&lt;FQDN&gt;</replaceable> <replaceable>&lt;HOSTNAME&gt;</replaceable>
  202. <replaceable>&lt;192.168.1.1&gt;</replaceable> <replaceable>&lt;FQDN&gt;</replaceable> <replaceable>&lt;HOSTNAME&gt;</replaceable> <replaceable>[alias1] [alias2 ...]</replaceable>
  203. ::1 localhost ip6-localhost ip6-loopback
  204. ff02::1 ip6-allnodes
  205. ff02::2 ip6-allrouters
  206. # End /etc/hosts</literal>
  207. EOF</userinput></screen>
  208. <para>The <replaceable>&lt;192.168.1.1&gt;</replaceable>,
  209. <replaceable>&lt;FQDN&gt;</replaceable>, and
  210. <replaceable>&lt;HOSTNAME&gt;</replaceable> values need to be
  211. changed for specific uses or requirements (if assigned an IP address by a
  212. network/system administrator and the machine will be connected to an
  213. existing network). The optional alias name(s) can be omitted.</para-->
  214. <!-- TRANSLATE: I didn't translate the same thing again. Just include
  215. the systemd version. -->
  216. <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
  217. href="networkd.xml"
  218. xpointer="xpointer(//*[@id='ch-scripts-hostname'])"/>
  219. <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
  220. href="networkd.xml"
  221. xpointer="xpointer(//*[@id='ch-scripts-hosts'])"/>
  222. <!-- This is not very useful
  223. <para>If a network card is not going to be configured, create the
  224. <filename>/etc/hosts</filename> file by running:</para>
  225. <screen role="nodump"><userinput>cat &gt; /etc/hosts &lt;&lt; "EOF"
  226. <literal># Begin /etc/hosts (no network card version)
  227. 127.0.0.1 localhost
  228. 127.0.1.1 <replaceable>&lt;FQDN&gt;</replaceable> <replaceable>&lt;HOSTNAME&gt;</replaceable>
  229. ::1 localhost ip6-localhost ip6-loopback
  230. ff02::1 ip6-allnodes
  231. ff02::2 ip6-allrouters
  232. # End /etc/hosts (no network card version)</literal>
  233. EOF</userinput></screen> -->
  234. <!--/sect2-->
  235. </sect1>