<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-scripts-network" revision="sysv">
  <?dbhtml filename="network.html"?>

  <title>一般网络配置</title>

  <indexterm zone="ch-scripts-network">
    <primary sortas="d-network">network</primary>
  <secondary>configuring</secondary></indexterm>
<!--
  <para>This section only applies if a network card is to be
  configured.</para>

  <para>If a network card will not be used, there is likely no need to create
  any configuration files relating to network cards. If that is the case, you
  will need to remove the <filename class="symlink">network</filename> symlinks
  from all run-level directories (<filename
  class="directory">/etc/rc.d/rc*.d</filename>) after the bootscripts are
  installed in <xref linkend="ch-scripts-bootscripts"/>.</para>
-->
  <sect2>
    <title>创建网络接口配置文件</title>

    <!--para>Which interfaces are brought up and down by the network script
    usually depends on the files in <filename
    class="directory">/etc/sysconfig/</filename>.  This directory should
    contain a file for each interface to be configured, such as
    <filename>ifconfig.xyz</filename>, where <quote>xyz</quote> should describe
    the network card.  The interface name (e.g. eth0) is usually appropriate.
    Inside this file are attributes to this interface, such as its IP
    address(es), subnet masks, and so forth.  It is necessary that the stem of
    the filename be <emphasis>ifconfig</emphasis>.</para-->
    <para>负责网络的启动脚本根据
    <filename class="directory">/etc/sysconfig</filename> 中配置文件的内容，
	决定应该启用或禁用哪些网络接口。对于每个需要配置的网络接口，
	该目录中都应该包含一个文件，文件名类似
	<filename>ifconfig.xyz</filename>，
	这里<quote>xyz</quote>应该能够描述该网卡，
	使用接口名（如 eth0）一般比较合适。
	文件内部是该网络接口的属性，如 IP 地址、子网掩码等。
	文件名必须以 <emphasis>ifconfig</emphasis> 开头。</para>

    <!--note><para>If the procedure in the previous section was not used, Udev
    will assign network card interface names based on system physical
    characteristics such as enp2s1. If you are not sure what your interface
    name is, you can always run <command>ip link</command> or <command>ls
    /sys/class/net</command> after you have booted your system.  
	</para></note-->
	<note><para>如果没有使用前一节描述的自定义命名策略，
    udev 会根据系统物理特征命名网卡接口，例如 enp2s1。
    如果您不能确定接口名，可以在引导您的 LFS 系统后使用
    <command>ip link</command> 或 <command>ls /sys/class/net</command>
    命令确认。</para></note>

    <para>作为示例，以下命令为
    <emphasis>eth0</emphasis> 设备创建一个静态 IP 地址配置：</para>

<screen><userinput>cd /etc/sysconfig/
cat &gt; ifconfig.eth0 &lt;&lt; "EOF"
<literal>ONBOOT=yes
IFACE=eth0
SERVICE=ipv4-static
IP=192.168.1.2
GATEWAY=192.168.1.1
PREFIX=24
BROADCAST=192.168.1.255</literal>
EOF</userinput></screen>

	<para>您必须修改文件中的变量，使配置与您的网络环境相匹配。</para>

    <!--para>If the <envar>ONBOOT</envar> variable is set to <quote>yes</quote> the
    System V network script will bring up the Network Interface Card (NIC) during
    booting of the system. If set to anything but <quote>yes</quote> the NIC
    will be ignored by the network script and not be automatically brought up.
    The interface can be manually started or stopped with the
    <command>ifup</command> and <command>ifdown</command> commands.</para-->
    <para>如果 <envar>ONBOOT</envar> 变量被设置为<quote>yes</quote>，
    则 System V 网络脚本会在引导系统时启用该网络接口卡（NIC）。
	否则，网络脚本会忽略该 NIC，不自动启用它。
	您可以使用 <command>ifup</command> 和 <command>ifdown</command>
	命令，手动启用或禁用网络接口。</para>

    <!--para>The <envar>IFACE</envar> variable defines the interface name,
    for example, eth0.  It is required for all network device configuration
    files. </para-->
    <para><envar>IFACE</envar> 变量指定网络接口名，例如 eth0。
    所有网络设备配置文件都需要它。</para>

    <!--para>The <envar>SERVICE</envar> variable defines the method used for
    obtaining the IP address. The LFS-Bootscripts package has a modular IP
    assignment format, and creating additional files in the <filename
    class="directory">/lib/services/</filename> directory allows other IP
    assignment methods. This is commonly used for Dynamic Host Configuration
    Protocol (DHCP), which is addressed in the BLFS book.</para-->
    <para><envar>SERVICE</envar> 变量定义获取 IP 地址的方法。
    LFS-Bootscripts 软件包使用模块化 IP 分配格式，在
	<filename class="directory">/lib/services/</filename>
	目录中新建一些文件，即可使用其他 IP 分配方法，这一般被用于
	动态主机配置协议（DHCP）配置，具体方法在 BLFS 手册中说明。</para>

    <!--para>The <envar>GATEWAY</envar> variable should contain the default
    gateway IP address, if one is present. If not, then comment out the
    variable entirely.</para-->
    <para>如果默认网关存在，
    <envar>GATEWAY</envar> 变量应该包含默认网关的 IP 地址。
    如果默认网关不存在，应该将这一行完全注释掉。</para>

    <!--para>The <envar>PREFIX</envar> variable contains the number of
    bits used in the subnet. Each octet in an IP address is 8 bits. If the
    subnet's netmask is 255.255.255.0, then it is using the first three octets
    (24 bits) to specify the network number. If the netmask is 255.255.255.240,
    it would be using the first 28 bits.  Prefixes longer than 24 bits are
    commonly used by DSL and cable-based Internet Service Providers (ISPs).
    In this example (PREFIX=24), the netmask is 255.255.255.0. Adjust the
    <envar>PREFIX</envar> variable according to your specific subnet.
    If omitted, the PREFIX defaults to 24.</para-->
    <para><envar>PREFIX</envar> 变量应该包含子网使用的 IP 地址位数。
    IP 地址中的每一段都是二进制为 8 位的数，如果子网掩码是
	255.255.255.0，说明 IP 地址中前三段（24 位）表示子网编号。如果子网掩码是
	255.255.255.240，则使用了前 28 位表示子网编号。
	子网前缀比 24 长的情况一般见于基于 DSL 或同轴电缆的 Internet 服务提供商
	（ISP）。在我们的例子中，子网掩码是 255.255.255.0。
	您应该根据您所处的子网调整 <envar>PREFIX</envar> 变量。
	如果不指定它，则它默认为 24。</para>

    <para>参考 <command>ifup</command> 的 man 页面获得更多信息。</para>

  </sect2>
  <sect2 id="resolv.conf">
	  <title>创建 /etc/resolv.conf 文件</title>

    <indexterm zone="resolv.conf">
      <primary sortas="e-/etc/resolv.conf">/etc/resolv.conf</primary>
    </indexterm>

    <para>系统需要某种方式，获取域名服务（DNS），
    以将 Internet 域名解析成 IP 地址，或进行反向解析。
    为了达到这一目的，最好的方法是将 ISP 或网络管理员提供的 DNS
    服务器的 IP 地址写入
    <filename>/etc/resolv.conf</filename>。执行以下命令创建该文件：</para>

<screen><userinput>cat &gt; /etc/resolv.conf &lt;&lt; "EOF"
<literal># Begin /etc/resolv.conf

domain <replaceable>&lt;您的域名&gt;</replaceable>
nameserver <replaceable>&lt;您的主要域名服务器 IP 地址&gt;</replaceable>
nameserver <replaceable>&lt;您的次要域名服务器 IP 地址&gt;</replaceable>

# End /etc/resolv.conf</literal>
EOF</userinput></screen>

    <para>可以省略 <varname>domain</varname> 语句，
    或使用一条 <varname>search</varname> 语句代替它。
    阅读 resolv.conf 的 man 页面了解更多细节。</para>

    <para>将
    <replaceable>&lt;域名服务器 IP 地址&gt;</replaceable>
    替换为您的网络环境下最合适的 DNS 服务器 IP 地址。
    这里往往会写入不止一个 DNS 服务器（需要提供后备功能的次要服务器）。
    如果您只需要或只希望使用一个 DNS 服务器，可以删除文件中的第二个
    <emphasis>nameserver</emphasis> 行。也可以写入本地路由器的 IP 地址。
    </para>

    <note>
      <para>Google 公用 DNS 服务器的 IP 地址是 8.8.8.8 和 8.8.4.4。</para>
    </note>

  </sect2>

  <!--sect2 id="ch-scripts-hostname">
    <title>Configuring the system hostname</title>

    <indexterm zone="ch-scripts-hostname">
      <primary sortas="d-hostname">hostname</primary>
      <secondary>configuring</secondary>
    </indexterm>

     <para>During the boot process, the file <filename>/etc/hostname</filename>
     is used for establishing the system's hostname.</para>

     <para>Create the <filename>/etc/hostname</filename> file and enter a
     hostname by running:</para>

<screen><userinput>echo "<replaceable>&lt;lfs&gt;</replaceable>" &gt; /etc/hostname</userinput></screen>

     <para><replaceable>&lt;lfs&gt;</replaceable> needs to be replaced with the
     name given to the computer. Do not enter the Fully Qualified Domain Name
     (FQDN) here. That information is put in the
     <filename>/etc/hosts</filename> file.</para>

  </sect2-->

  <!--sect2 id="ch-scripts-hosts">
     <title>Customizing the /etc/hosts File</title>

     <indexterm zone="ch-scripts-hosts">
       <primary sortas="e-/etc/hosts">/etc/hosts</primary>
     </indexterm>
   
     <indexterm zone="ch-scripts-hosts">
       <primary sortas="d-localnet">localnet</primary>
       <secondary>/etc/hosts</secondary>
     </indexterm>
   
     <indexterm zone="ch-scripts-hosts">
       <primary sortas="d-network">network</primary>
       <secondary>/etc/hosts</secondary>
     </indexterm>
   
     <para>Decide on the IP address, fully-qualified domain name (FQDN), and
     possible aliases for use in the <filename>/etc/hosts</filename> file. The
     syntax is:</para>
   
<screen><literal>IP_address myhost.example.org aliases</literal></screen>

     <para>Unless the computer is to be visible to the Internet (i.e., there is
     a registered domain and a valid block of assigned IP addresses&mdash;most
     users do not have this), make sure that the IP address is in the private
     network IP address range. Valid ranges are:</para>

<screen><literal>Private Network Address Range      Normal Prefix
10.0.0.1 - 10.255.255.254           8
172.x.0.1 - 172.x.255.254           16
192.168.y.1 - 192.168.y.254         24</literal></screen>

     <para>x can be any number in the range 16-31. y can be any number in the
     range 0-255.</para>
   
     <para>A valid private IP address could be 192.168.1.1. A valid FQDN for
     this IP could be lfs.example.org.</para>
   
     <para>Even if not using a network card, a valid FQDN is still required.
     This is necessary for certain programs to operate correctly.</para>
   
     <para>Create the  <filename>/etc/hosts</filename> file by running:</para>

<screen><userinput>cat &gt; /etc/hosts &lt;&lt; "EOF"
<literal># Begin /etc/hosts

127.0.0.1 localhost
127.0.1.1 <replaceable>&lt;FQDN&gt;</replaceable> <replaceable>&lt;HOSTNAME&gt;</replaceable>
<replaceable>&lt;192.168.1.1&gt;</replaceable> <replaceable>&lt;FQDN&gt;</replaceable> <replaceable>&lt;HOSTNAME&gt;</replaceable> <replaceable>[alias1] [alias2 ...]</replaceable>
::1       localhost ip6-localhost ip6-loopback
ff02::1   ip6-allnodes
ff02::2   ip6-allrouters

# End /etc/hosts</literal>
EOF</userinput></screen>

     <para>The <replaceable>&lt;192.168.1.1&gt;</replaceable>,
     <replaceable>&lt;FQDN&gt;</replaceable>, and 
     <replaceable>&lt;HOSTNAME&gt;</replaceable> values need to be
     changed for specific uses or requirements (if assigned an IP address by a
     network/system administrator and the machine will be connected to an
     existing network). The optional alias name(s) can be omitted.</para-->

<!-- TRANSLATE:  I didn't translate the same thing again.  Just include
     the systemd version.  -->

<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
	href="networkd.xml"
	xpointer="xpointer(//*[@id='ch-scripts-hostname'])"/>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
	href="networkd.xml"
	xpointer="xpointer(//*[@id='ch-scripts-hosts'])"/>

<!--  This is not very useful

     <para>If a network card is not going to be configured, create the
     <filename>/etc/hosts</filename> file by running:</para>

<screen role="nodump"><userinput>cat &gt; /etc/hosts &lt;&lt; "EOF"
<literal># Begin /etc/hosts (no network card version)

127.0.0.1 localhost
127.0.1.1 <replaceable>&lt;FQDN&gt;</replaceable> <replaceable>&lt;HOSTNAME&gt;</replaceable>
::1       localhost ip6-localhost ip6-loopback
ff02::1   ip6-allnodes
ff02::2   ip6-allrouters

# End /etc/hosts (no network card version)</literal>
EOF</userinput></screen> -->

   <!--/sect2-->

</sect1>
