个人工具

“Quick HOWTO : Ch03 : Linux Networking/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
关于双工设置
 
(未显示4个用户的25个中间版本)
第1行: 第1行:
{{Translation}}<br>{{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch03_:_Linux_Networking}}<br>{{Translator|Apshai}}<br>{{Languages|Quick HOWTO : Ch03 : Linux Networking}}&nbsp;
+
{{Translation}}
 +
 
 +
{{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch03_:_Linux_Networking}}
 +
 
 +
{{Translator|Apshai}}
 +
 
 +
{{Languages|Quick HOWTO : Ch03 : Linux Networking}}
  
 
= 介绍 =
 
= 介绍 =
第5行: 第11行:
 
现在你已经牢牢掌握了许多最常用的网络概念,是时候应用它们来配置你的服务器了。其中一些设置在Linux安装过程中已经自动配置过了,但是你经常会发现你必须知道怎样修改这些初始设定。无论是你需要移动你的服务器到另一个网络中,还是添加一块新的网卡或者使用另一种方式连接到Internet。
 
现在你已经牢牢掌握了许多最常用的网络概念,是时候应用它们来配置你的服务器了。其中一些设置在Linux安装过程中已经自动配置过了,但是你经常会发现你必须知道怎样修改这些初始设定。无论是你需要移动你的服务器到另一个网络中,还是添加一块新的网卡或者使用另一种方式连接到Internet。
  
在第二章中, "[[Quick HOWTO : Ch02 : Introduction to Networking|Introduction to Networking]][[Quick HOWTO : Ch02 : Introduction to Networking|Ch02 : Introduction to Networking/zh]]",我们首先解释了什么是TCP/IP,所以我们也将从讨论怎样配置服务器的IP开始Linux的网络这一章。
+
在第二章中, [[Quick HOWTO : Ch02 : Introduction to Networking/zh|网络介绍]],我们首先解释了什么是TCP/IP,所以我们也将从讨论怎样配置服务器的IP开始Linux的网络这一章。
  
 
= 怎样配置网卡的IP地址 =
 
= 怎样配置网卡的IP地址 =
  
你需要知道的一切必要步骤来配置网卡的IP地址。网站的购物车应用可能需要额外的固定IP地址。你也可能需要增加第二块网卡,用于处理服务器的数据备份。最後,但并不少见,你可能只想在服务器上玩玩以测试你的技巧。
+
你需要知道的一切必要步骤来配置网卡的IP地址。网站的购物车应用可能需要额外的固定IP地址。你也可能需要增加第二块网卡,用于处理服务器的数据备份。最后,但并不少见,你可能只想在服务器上玩玩以测试你的技巧。
  
 
本节将展示怎样处理最常见的服务器IP操作。
 
本节将展示怎样处理最常见的服务器IP操作。
第34行: 第40行:
 
  collisions:0 txqueuelen:0
 
  collisions:0 txqueuelen:0
 
  RX bytes:82644 (80.7 Kb) TX bytes:82644 (80.7 Kb)
 
  RX bytes:82644 (80.7 Kb) TX bytes:82644 (80.7 Kb)
+
 
 
  wlan0 Link encap:Ethernet HWaddr 00:06:25:09:6A:B5
 
  wlan0 Link encap:Ethernet HWaddr 00:06:25:09:6A:B5
 
  inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
 
  inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
第51行: 第57行:
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
  
<br>在这个例子中,eth0是没有IP地址的,因为这台机器用无线网卡wlan0作为主网卡。接口wlan0有一个IP地址192.168.1.100和子网掩码255.255.255.0。
+
在这个例子中,eth0是没有IP地址的,因为这台机器用无线网卡wlan0作为主网卡。接口wlan0有一个IP地址192.168.1.100和子网掩码255.255.255.0。
  
 
你可以看到,这个命令提供了每块网卡的中断或者PCI总线ID等有用信息。在某些罕见情况中,你可能发现你的网卡不工作是因为它与其他一些设备共享了中断和内存访问地址。你可以通过查看/proc/interrupts文件来获得系统使用的全部中断请求(IRQs)。在下面的例子中, 我们可以看到从IRQ 0 到 15没有中断冲突,每个中断只有一条记录。设备eth0和eth1分别使用了中断10和5:
 
你可以看到,这个命令提供了每块网卡的中断或者PCI总线ID等有用信息。在某些罕见情况中,你可能发现你的网卡不工作是因为它与其他一些设备共享了中断和内存访问地址。你可以通过查看/proc/interrupts文件来获得系统使用的全部中断请求(IRQs)。在下面的例子中, 我们可以看到从IRQ 0 到 15没有中断冲突,每个中断只有一条记录。设备eth0和eth1分别使用了中断10和5:
  
 
  [root@bigboy tmp]# cat /proc/interrupts
 
  [root@bigboy tmp]# cat /proc/interrupts
  CPU0
+
              CPU0
  0: 2707402473 XT-PIC timer
+
    0: 2707402473         XT-PIC timer
  1: 67 XT-PIC i8042
+
    1:         67         XT-PIC i8042
  2: 0 XT-PIC cascade
+
    2:           0         XT-PIC cascade
  5: 411342 XT-PIC eth1
+
    5:     411342         XT-PIC eth1
  8: 1 XT-PIC rtc
+
    8:           1         XT-PIC rtc
  10: 1898752 XT-PIC eth0
+
    10:   1898752         XT-PIC eth0
  11: 0 XT-PIC uhci_hcd
+
    11:         0         XT-PIC uhci_hcd
  12: 58 XT-PIC i8042
+
    12:         58         XT-PIC i8042
  14: 5075806 XT-PIC ide0
+
    14:   5075806         XT-PIC ide0
  15: 506 XT-PIC ide1
+
    15:       506         XT-PIC ide1
NMI: 0
+
  NMI:         0
ERR: 43
+
  ERR:         43
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
  
第89行: 第95行:
 
{| cellspacing="0" cellpadding="5" align="center" border="1"
 
{| cellspacing="0" cellpadding="5" align="center" border="1"
 
|-
 
|-
| 固定IP地址
+
| style="background:#008000; color:white" | 固定IP地址
 
|- valign="top"
 
|- valign="top"
 
|  
 
|  
 
  [root@bigboy tmp]# cd /etc/sysconfig/network-scripts
 
  [root@bigboy tmp]# cd /etc/sysconfig/network-scripts
 
  [root@bigboy network-scripts]# cat ifcfg-eth0
 
  [root@bigboy network-scripts]# cat ifcfg-eth0
+
 
 
  #  
 
  #  
 
  # File: ifcfg-eth0
 
  # File: ifcfg-eth0
第109行: 第115行:
 
   
 
   
 
  [root@bigboy network-scripts]#
 
  [root@bigboy network-scripts]#
 
 
|}
 
|}
  
 
{| cellspacing="0" cellpadding="5" align="center" border="1"
 
{| cellspacing="0" cellpadding="5" align="center" border="1"
 
|-
 
|-
| 通过DHCP获得IP地址
+
| style="background:#008000; color:white" | 通过DHCP获得IP地址
 
|- valign="top"
 
|- valign="top"
 
|  
 
|  
第128行: 第133行:
 
   
 
   
 
  [root@bigboy network-scripts]#
 
  [root@bigboy network-scripts]#
 
 
|}
 
|}
  
第135行: 第139行:
 
缺省的RedHat/Fedora安装会在network-scripts文件包含广播和网络设定。这些都是可选的。
 
缺省的RedHat/Fedora安装会在network-scripts文件包含广播和网络设定。这些都是可选的。
  
在改变网卡的配置文件后,你需要停止然后激活它以使修改生效。ifdown和ifup命令可用于实现这个目的:
+
在改变网卡的配置文件後,你需要停止然後激活它以使修改生效。ifdown和ifup命令可用于实现这个目的:
  
 
  [root@bigboy network-scripts]# ifdown eth0
 
  [root@bigboy network-scripts]# ifdown eth0
 
  [root@bigboy network-scripts]# ifup eth0
 
  [root@bigboy network-scripts]# ifup eth0
  
你的服务器必须有一个默认网关,使它能够与Internet通信。这将在本章的稍后涉及。
+
你的服务器必须有一个默认网关,使它能够与Internet通信。这将在本章的稍後涉及。
  
 
=== DHCP如何影响你使用的DNS服务器 ===
 
=== DHCP如何影响你使用的DNS服务器 ===
第173行: 第177行:
 
'''注意:''' 关闭主网络设备也同时关闭其所有别名。而别名可以独立于其他网络设备关闭。
 
'''注意:''' 关闭主网络设备也同时关闭其所有别名。而别名可以独立于其他网络设备关闭。
  
完成这4个简单的步骤後,你应该能从网络的其他服务器上ping通这个新的IP别名。
+
完成这4个简单的步骤后,你应该能从网络的其他服务器上ping通这个新的IP别名。
  
 
== 为DSL连接设定IP地址 ==
 
== 为DSL连接设定IP地址 ==
  
如果你使用是有固定或静态IP地址的DSL连接,那么配置步骤和之前描述的是一样的。你把以太网接口连接到DSL modem上,配置ISP提供的
+
如果你使用是有固定或静态IP地址的DSL连接,那么配置步骤和之前描述的是一样的。你把以太网接口连接到DSL modem上,配置ISP提供的IP地址,子网掩码,广播地址,和网关信息。重启网络设备后你就应该连上网了。记住,你可能还需要正确地配置DNS服务器。
  
IP地址,子网掩码,广播地址,和网关信息。重启网络设备後你就应该连上网了。记住,你可能还需要正确地配置DNS服务器。
+
如果你使用的是有DHCP或或动态IP地址的DSL连接,那么过程就不一样了。你的ISP会为您提供一个PPPoE(PPP authentication over Ethernet)的用户名和密码,这将使您的电脑在每次启动时透明地登录到互联网上。Fedora的Linux需要安装rp-PPPoE的RPM软件包来支持这个功能。
  
如果你使用的是有DHCP或或动态IP地址的DSL连接,那么过程就不一样了。你的ISP会为您提供一个PPPoE(PPP authentication over
+
注意:除非你特别申请静态IP地址,你的ISP会向你提供一个基于DHCP的连接。而DHCP分配给你的机器或者互联网路由的IP地址可能很多天都不会改变,你可能会误为是静态的。
  
Ethernet)的用户名和密码,这将使您的电脑在每次启动时透明地登录到互联网上。Fedora的Linux需要安装rp-PPPoE的RPM软件包来支持这
+
下载和安装RPM包并不难。如果你需要复习,第6章"[[Quick HOWTO : Ch06 : Installing Linux Software|Installing Linux Software]]"中涵盖了关于RPM操作的详细内容。 当寻找该文件时,记得PPPoE的RPM文件名通常是以rp-pppoe开头,後面是版本号,例如:rp-pppoe-3.5-8.i386.rpm。
  
个功能。
+
在安装RPM後,你需要经过若干步骤来完成连接。PPPoE配置将创建立一个名为ppp0的虚拟接口,它将通过物理网络接口eth0的来连接。下面是你需要做的:
 
+
注意:除非你特别申请静态IP地址,你的ISP会向你提供一个基于DHCP的连接。而DHCP分配给你的机器或者互联网路由的IP地址可能很多天
+
 
+
都不会改变,你可能会误为是静态的。
+
 
+
下载和安装RPM包并不难。如果你需要复习,第6章"[[Quick HOWTO : Ch06 : Installing Linux Software|Installing Linux Software]]"
+
 
+
中涵盖了关于RPM操作的详细内容。 当寻找该文件时,记得PPPoE的RPM文件名通常是以rp-pppoe开头,后面是版本号,例如:rp-pppoe-
+
 
+
3.5-8.i386.rpm。
+
 
+
在安装RPM后,你需要经过若干步骤来完成连接。PPPoE配置将创建立一个名为ppp0的虚拟接口,它将通过物理网络接口eth0的来连接。下面
+
 
+
是你需要做的:
+
  
 
* 备份你的ifcfg-eth0文件。
 
* 备份你的ifcfg-eth0文件。
第247行: 第237行:
 
   
 
   
 
  Enter the demand value (default no):
 
  Enter the demand value (default no):
 
:接着会提示你输入你的DNS服务器信息。这步会编辑你的/etc/resolv.conf文件。如果你在服务器上以缓存DNS模式运行BIND,就不用输入
 
  
直接回车。如果你希望你的ISP的DNS服务器自动提供IP地址,就输入单词server。
 
  
:Please refer to Chapter 18, "[[Quick HOWTO : Ch18 : Configuring DNS|Configuring DNS]]", for more information on BIND and
+
:接着会提示你输入你的DNS服务器信息。这步会编辑你的/etc/resolv.conf文件。如果你在服务器上以缓存DNS模式运行BIND,就不用输入直接回车。如果你希望你的ISP的DNS服务器自动提供IP地址,就输入单词server。
  
DNS.
+
:请参阅第18章, "[[Quick HOWTO : Ch18 : Configuring DNS|Configuring DNS]]", 以获得BIND和DNS的更详细信息。
  
 
  DNS
 
  DNS
+
 
 
  Please enter the IP address of your ISP's primary DNS server.
 
  Please enter the IP address of your ISP's primary DNS server.
 
  If your ISP claims that 'the server will provide dynamic DNS addresses', enter 'server' (all lower-case) here.
 
  If your ISP claims that 'the server will provide dynamic DNS addresses', enter 'server' (all lower-case) here.
  If you just press enter, I will assume you know what you are doing and not modify your DNS setup.  
+
  If you just press enter, I will assume you know what you are doing and not modify your DNS setup.  
 
  Enter the DNS information here:
 
  Enter the DNS information here:
  
第270行: 第257行:
 
  Please re-enter your Password:
 
  Please re-enter your Password:
  
:然后它会询问是否后允许常规用户(不是超级用户root)激活/停止这个新的ppp0设备。这可能是需要的,如果你家里有非root的成员需要
+
:然後它会询问是否後允许常规用户(不是超级用户root)激活/停止这个新的ppp0设备。这可能是需要的,如果你家里有非root的成员需要访问Internet的话:
 
+
访问Internet的话:
+
  
 
  USERCTRL
 
  USERCTRL
 
   
 
   
  Please enter 'yes' (two letters, lower-case.) if you want to allow normal user to start or stop DSL connection (default  
+
  Please enter 'yes' (two letters, lower-case.)  
 
+
if you want to allow normal user to start or stop DSL connection (default yes):
yes):
+
 
+
:rp-pppoe包在/etc/ppp目录下有两个样例ip表防火墙脚本,文件名是firewall-standalone和firewall-masq。它们都非常基本并不涵盖将
+
 
+
你的Linux机器设置成web服务器,DNS服务器或者mail服务器的规则。我都推荐选择NONE,然后使用第14章"[[Quick HOWTO : Ch14 : Linux
+
  
Firewalls Using iptables|Linux Firewalls Using iptables]]"中的一个基本脚本样例,或者附录II中的更复杂脚本
+
:rp-pppoe包在/etc/ppp目录下有两个样例ip表防火墙脚本,文件名是firewall-standalone和firewall-masq。它们都非常基本并不涵盖将你的Linux机器设置成web服务器,DNS服务器或者mail服务器的规则。我都推荐选择NONE,然後使用第14章"[[Quick HOWTO : Ch14 : Linux Firewalls Using iptables|Linux Firewalls Using iptables]]"中的一个基本脚本样例,或者附录II中的更复杂脚本
  
 
  FIREWALLING
 
  FIREWALLING
  
  Please choose the firewall rules to use. Note that these rules are very basic. You are strongly encouraged to use a more  
+
  Please choose the firewall rules to use. Note that these rules are very basic. You are strongly
 
+
encouraged to use a more sophisticated firewall setup; however, these will provide basic security.
sophisticated firewall setup; however, these will provide basic security. If you are running any servers on your machine,  
+
If you are running any servers on your machine, you must choose 'NONE' and set up firewalling
 
+
yourself. Otherwise, the firewall rules will deny access to all standard servers like Web, e-mail,
you must choose 'NONE' and set up firewalling yourself. Otherwise, the firewall rules will deny access to all standard  
+
ftp, etc. If you are using SSH, the rules will block outgoing SSH connections which allocate a
 
+
privileged source port.
servers like Web, e-mail, ftp, etc. If you are using SSH, the rules will block outgoing SSH connections which allocate a  
+
 
+
privileged source port.
+
 
   
 
   
 
  The firewall choices are:
 
  The firewall choices are:
 
   
 
   
 
  0 - NONE: This script will not set any firewall rules. You are responsible
 
  0 - NONE: This script will not set any firewall rules. You are responsible
for ensuring the security of your machine. You are STRONGLY
+
          for ensuring the security of your machine. You are STRONGLY
recommended to use some kind of firewall rules.
+
          recommended to use some kind of firewall rules.
 
  1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
 
  1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
 
  2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
 
  2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
for a LAN
+
                for a LAN
 
   
 
   
 
  Choose a type of firewall (0-2): 0
 
  Choose a type of firewall (0-2): 0
第320行: 第297行:
 
  ** Summary of what you entered **
 
  ** Summary of what you entered **
 
   
 
   
  <br>Ethernet Interface: eth0
+
  Ethernet Interface: eth0
 
   
 
   
  User name: bigboy-login@isp
+
  User name:         bigboy-login@isp
  Activate-on-demand: No
+
  Activate-on-demand: No
  DNS: Do not adjust
+
  DNS:               Do not adjust
  Firewalling: NONE  
+
  Firewalling:       NONE  
  User Control: yes
+
  User Control:       yes
 
  Accept these settings and adjust configuration files (y/n)? y
 
  Accept these settings and adjust configuration files (y/n)? y
 
   
 
   
 
  Adjusting /etc/sysconfig/network-scripts/ifcfg-ppp0
 
  Adjusting /etc/sysconfig/network-scripts/ifcfg-ppp0
 
  Adjusting /etc/ppp/chap-secrets and /etc/ppp/pap-secrets
 
  Adjusting /etc/ppp/chap-secrets and /etc/ppp/pap-secrets
(But first backing it up to /etc/ppp/chap-secrets.bak)
+
  (But first backing it up to /etc/ppp/chap-secrets.bak)
(But first backing it up to /etc/ppp/pap-secrets.bak)
+
  (But first backing it up to /etc/ppp/pap-secrets.bak)
  
:在最后,它会告诉你激活/关闭这个新的ppp0设备,以及获得设备状态的命令。
+
:在最後,它会告诉你激活/关闭这个新的ppp0设备,以及获得设备状态的命令。
  
 
  Congratulations, it should be all set up!
 
  Congratulations, it should be all set up!
第341行: 第318行:
 
  Type '/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0' to see the link status.
 
  Type '/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0' to see the link status.
  
 +
:'''注意:''' 这个例子中推荐使用adsl-status命令後面跟上PPPoE接口配置文件名字。不过这个命令会缺省显示ppp0设备的信息,因此在大部分系统中没有必要加上ifcfg-ppp0文件名。
  
:'''注意:''' 这个例子中推荐使用adsl-status命令后面跟上PPPoE接口配置文件名字。不过这个命令会缺省显示ppp0设备的信息,因此在
+
在你完成安装配置rp-pppoe後,你应该可以通过DSL的DHCP连接到Internet上。
 
+
大部分系统中没有必要加上ifcfg-ppp0文件名。
+
 
+
在你完成安装配置rp-pppoe后,你应该可以通过DSL的DHCP连接到Internet上。
+
  
 
=== adsl-setup创建的一些重要文件 ===
 
=== adsl-setup创建的一些重要文件 ===
  
 
有三个adsl-setup脚本创建的文件你可能会感兴趣。第一个是ifcfg-pppo文件,包含了设备链路层的连接参数。
 
有三个adsl-setup脚本创建的文件你可能会感兴趣。第一个是ifcfg-pppo文件,包含了设备链路层的连接参数。
 
  
 
  [root@bigboy network-scripts]# more ifcfg-ppp0
 
  [root@bigboy network-scripts]# more ifcfg-ppp0
第381行: 第354行:
 
  [root@bigboy network-scripts]# more /etc/ppp/pap-secrets
 
  [root@bigboy network-scripts]# more /etc/ppp/pap-secrets
 
  # Secrets for authentication using PAP
 
  # Secrets for authentication using PAP
  # client server secret IP addresses
+
  # client       server secret                 IP addresses
  "bigboy-login@isp" * "password" [root@bigboy network-scripts]#
+
  "bigboy-login@isp" *       "password"
 +
[root@bigboy network-scripts]#
  
 
=== 简单的故障诊断 ===
 
=== 简单的故障诊断 ===
第393行: 第367行:
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
  
激活后,设备即正常工作。
+
激活後,设备即正常工作。
  
 
  [root@bigboy tmp]# ifup ppp0
 
  [root@bigboy tmp]# ifup ppp0
第403行: 第377行:
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
  
关于更多的故障诊断信息,你可以访问rp-pppoe的网站Roaring Penguin (www.roaringpenguin.com)。那里有一些如果避免VPN客户端问题
+
关于更多的故障诊断信息,你可以访问rp-pppoe的网站Roaring Penguin (www.roaringpenguin.com)。那里有一些如果避免VPN客户端问题的很好的提示。
 
+
的很好的提示。
+
  
 
== 为Cable Modem设置IP地址 ==
 
== 为Cable Modem设置IP地址 ==
第413行: 第385行:
 
= 怎样激活/关闭你的网卡 =
 
= 怎样激活/关闭你的网卡 =
  
命令ifup和ifdown分别用于激活和停止一块网络设备。你必须有一个相关的ifcfg文件在/etc/sysconfig/network-scripts目录下来确保这些命令工作。下面是一个eth0设备的例子:
+
命令ifup和ifdown分别用于激活和停止一块网络设备。你必须有一个相关的ifcfg文件在/etc/sysconfig/network-scripts目录下来确保
  
<br>
+
这些命令工作。下面是一个eth0设备的例子:
  
 
  [root@bigboy tmp]# ifdown eth0
 
  [root@bigboy tmp]# ifdown eth0
第422行: 第394行:
 
= 怎样查看你当前的路由表 =
 
= 怎样查看你当前的路由表 =
  
netstat -nr命令会显示路由表的内容。网关为0.0.0.0的网络通常是直连到网络设备上的。因为到自己的直连设备上是不需要网关的,所以0.0.0.0的网关地址是正常的。目的地址是0.0.0.0的路由是你的缺省网关。
+
netstat -nr命令会显示路由表的内容。网关为0.0.0.0的网络通常是直连到网络设备上的。因为到自己的直连设备上是不需要网关的,所
 +
 
 +
以0.0.0.0的网关地址是正常的。目的地址是0.0.0.0的路由是你的缺省网关。
 +
 
 +
* 在这个例子里,有两个网关,缺省的并且是到255.255.255.255通常是DHCP服务器添加的。在这个例子里bigbogy服务器也是一个DHCP服
  
* 在这个例子里,有两个网关,缺省的并且是到255.255.255.255通常是DHCP服务器添加的。在这个例子里bigbogy服务器也是一个DHCP服务器。
+
务器。
  
 
  [root@bigboy tmp]# netstat -nr
 
  [root@bigboy tmp]# netstat -nr
 
   
 
   
 
  Kernel IP routing table
 
  Kernel IP routing table
  Destination Gateway Genmask Flags MSS Window irtt Iface
+
  Destination     Gateway Genmask Flags MSS Window irtt Iface
 
  255.255.255.255 0.0.0.0 255.255.255.255 UH    40  0      0    wlan0
 
  255.255.255.255 0.0.0.0 255.255.255.255 UH    40  0      0    wlan0
 
  192.168.1.0 0.0.0.0 255.255.255.0 U    40  0 0    wlan0
 
  192.168.1.0 0.0.0.0 255.255.255.0 U    40  0 0    wlan0
 
  127.0.0.0 0.0.0.0 255.0.0.0 U    40  0 0    lo
 
  127.0.0.0 0.0.0.0 255.0.0.0 U    40  0 0    lo
  0.0.0.0 192.168.1.1 0.0.0.0 UG    40  0 0    wlan0
+
  0.0.0.0 192.168.1.1 0.0.0.0         UG    40  0 0    wlan0
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
  
第446行: 第422行:
 
  172.16.68.32 172.16.69.193 255.255.255.224 UG    40  0 0    eth1
 
  172.16.68.32 172.16.69.193 255.255.255.224 UG    40  0 0    eth1
 
  172.16.67.0 172.16.67.135 255.255.255.224 UG    40  0 0    eth0
 
  172.16.67.0 172.16.67.135 255.255.255.224 UG    40  0 0    eth0
  172.16.69.192 0.0.0.0 255.255.255.192 U    40  0 0    eth1
+
  172.16.69.192 0.0.0.0         255.255.255.192 U    40  0 0    eth1
  172.16.67.128 0.0.0.0 255.255.255.128 U    40  0 0    eth0
+
  172.16.67.128 0.0.0.0         255.255.255.128 U    40  0 0    eth0
 
  172.160.0 172.16.67.135 255.255.0.0 UG    40  0 0    eth0
 
  172.160.0 172.16.67.135 255.255.0.0 UG    40  0 0    eth0
 
  172.16.0.0 172.16.67.131 255.240.0.0 UG    40  0 0    eth0
 
  172.16.0.0 172.16.67.131 255.240.0.0 UG    40  0 0    eth0
 
  127.0.0.0 0.0.0.0 255.0.0.0 U    40  0 0    lo
 
  127.0.0.0 0.0.0.0 255.0.0.0 U    40  0 0    lo
  0.0.0.0 172.16.69.193 0.0.0.0 UG    40  0 0    eth1
+
  0.0.0.0       172.16.69.193 0.0.0.0 UG    40  0 0    eth1
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
  
= How to Change Your Default Gateway =
+
= 怎样改变你的缺省网关 =
  
Your server needs to have a single default gateway. DHCP servers will automatically assign a default gateway to DHCP configured NICs, but NICs with configured static IP addresses will need to have a manually configured default gateway. This can be done with a simple command. This example uses a newly installed wireless interface called wlan0, most PCs would be using the standard Ethernet interface eth0.
+
你的服务器需要有一个单一缺省网关。配置了DHCP的网卡会自动由DHCP服务器分配一个缺省网关,但是配置了静态IP地址的网卡就需要手工配置缺省网关。这可以通过一个简单的命令实现。这个例子中缺省路由使用一个新安装的无线网卡wlan0,而大部分PC都是使用标准的以太网卡eth0。
  
 
  [root@bigboy tmp]# route add default gw 192.168.1.1 wlan0
 
  [root@bigboy tmp]# route add default gw 192.168.1.1 wlan0
  
In this case, make sure that the router/firewall with IP address 192.168.1.1 is connected to the same network as interface wlan0!
+
在这个例子中,请确保IP地址为192.168.1.1的路由/防火墙和wlan0设备在一个网络中!
  
Once done, you'll need to update your <code>/etc/sysconfig/network</code> file to reflect the change. This file is used to configure your default gateway each time Linux boots.
+
完成后,你需要更新你的<code>/etc/sysconfig/network</code>文件来反映这个变化。这个文件会在每次Linux启动时配置你的缺省网关。
  
 
  NETWORKING=yes
 
  NETWORKING=yes
HOSTNAME=bigboy
+
HOSTNAME=bigboy
GATEWAY=192.168.1.1
+
GATEWAY=192.168.1.1
  
Note: In Debian based systems the default gateway is permanently defined in the <code>/etc/network/interfaces</code> file. See the section "Debian / Ubuntu Network Configuration" later in this chapter for more details.
+
注意:在以Debian为基础的系统中,缺省网关是固定定义在文件<code>/etc/network/interfaces</code>中。请参阅后面章节"Debian / Ubuntu的网络配置" 以了解更多详情。
  
Some people don't bother modifying network specific files and just place the <code>route add</code> command in the script file <code>/etc/rc.d/rc.local</code> which is run at the end of each reboot.
+
有些人可能不愿意修改特定的网络文件,而只是把<code>route add</code>命令添加到<code>/etc/rc.d/rc.local</code>脚本文件中,这样每次启动时就会自动执行了。
  
It is possible to define default gateways in the NIC configuration file in the <code>/etc/sysconfig/network-scripts</code> directory, but you run the risk of inadvertently assigning more than one default gateway when you have more than one NIC. This could cause connectivity problems. If one of the default gateways has no route to the intended destination, every other packet will become lost. Firewalls that are designed to block packets with irregular sequence numbers and unexpected origins could also obstruct your data flow.
+
也可以在<code>/etc/sysconfig/network-scripts</code>目录下网卡配置文件中定义缺省网关,但这样是有风险的,如果你有多块网卡可能导致定义多个缺省网关。这样会导致连接问题。如果其中一个缺省网关没有到目的地的路由,所有的包都会丢失。而且防火墙会阻挡带有不规则序号和不明来源的包,这也会妨碍你的数据流。
= How to Configure Two Gateways =
+
  
Some networks may have multiple router/firewalls providing connectivity. Here's a typical scenario:
+
= 怎样配置两个网关 =
  
* You have one router providing access to the Internet that you'd like to have as your default gateway (see the default gateway example earlier)
+
一些网络可能拥有多个路由器/防火墙提供连接。这里有一个典型的情况:
  
* You also have another router providing access to your corporate network using addresses in the range 10.0.0.0 to 10.255.255.255. Let's assume that this router has an IP address of 192.168.1.254
+
* 你有一个路由可以访问Internet,你愿意用它来做你的缺省网关(参看前面的缺省网关样例)
  
The Linux box used in this example uses interface wlan0 for its Internet connectivity. You might be most likely using interface eth0, please adjust your steps accordingly.
+
* 有还有另一个路由用来访问你的公司网络,公司网络的IP范围是10.0.0.0到10.255.255.255。让我们假定这个路由的IP地址是192.168.1.254。
  
There are a number of ways to add this new route.
+
这个例子中的Linux机器是使用wlan0提供Internet连接的。你可能是使用eth0设备,请据此调整你的操作。
  
<br>
+
有多种方法可以添加这个新的路由。
  
== Adding Temporary Static Routes ==
+
== 添加临时的静态路由 ==
  
The route add command can be used to add new routes to your server that will last till the next reboot. It has the advantage of being univeral to all versions of Linux and is well documented in the man pages. In our example the reference to the 10.0.0.0 network has to be preceded with a -net switch and the subnet mask and gateway values also have to be preceded by the netmask and gw switches respectively.
+
route add命令可以用来添加一条新路由到你的服务器,直到下一次重启。这个方法的优势是在所有版本的Linux中都通用,而且在man里面都有详细的说明。在我们的例子里,10.0.0.0网络前面有-net参数,子网掩码和网关前面也分别有netmask和gw参数。
  
 
  [root@bigboy tmp]# route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0
 
  [root@bigboy tmp]# route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0
  
If you wanted to add a route to an individual server, then the "-host" switch would be used with no netmask value. (The route command automatically knows the mask should be 255.255.255.255). Here is an example for a route to host 10.0.0.1.
+
如果你想为一个单独的服务器添加路由,那么应该使用-host参数,而且不设子网掩码值(route命令会自动知道子网掩码是255.255.255.255)。这里是为机器10.0.0.1添加路由的例子。
  
 
  [root@bigboy tmp]# route add -host 10.0.0.1 gw 192.168.1.254 wlan0
 
  [root@bigboy tmp]# route add -host 10.0.0.1 gw 192.168.1.254 wlan0
  
A universal way of making this change persistent after a reboot would be to place this route add command in the file <code>/etc/rc.d/rc.local</code>, which is always run at the end of the booting process.
+
如果想要这个变化在重启后也能持续,一个通用的方法是把route add命令添加到<code>/etc/rc.d/rc.local</code>文件中,它会始终在启动过程结束前运行。
  
== Adding Permanent Static Routes ==
+
== 添加永久静态路由 ==
  
In Fedora Linux, permanent static routes are added on a per interface basis in files located in the <code>/etc/sysconfig/network-scripts</code> directory. The filename format is route-interface-name so the filename for interface <code>wlan0</code> would be <code>route-wlan0</code>.
+
在Fedora Linux中,永久静态路由是添加在<code>/etc/sysconfig/network-scripts</code>目录下对应设备文件中的。文件名的格式是route-网络设备名称,所以<code>wlan0</code>设备对应的文件名是<code>route-wlan0</code>
  
The format of the file is quite intuitive with the target network coming in the first column followed by the word via and then the gateway's IP address. In our routing example, to set up a route to network 10.0.0.0 with a subnet mask of 255.0.0.0 (a mask with the first 8 bits set to 1) via the 192.168.1.254 gateway, we would have to configure file <code>/etc/sysconfig/network-scripts/route-wlan0</code> to look like this:
+
该文件的格式很直观,第一行是目标网络加上单词via和网关的IP地址。在我们的例子中,要设置一个到通过网关192.168.1.254
 +
到网络10.0.0.0的路由,子网掩码是255.0.0.0(一个头8位都是1的掩码),我们像这样设置<code>/etc/sysconfig/network-scripts/route-wlan0</code>文件:
  
 
  #
 
  #
# File /etc/sysconfig/network-scripts/route-wlan0
+
# File /etc/sysconfig/network-scripts/route-wlan0
#
+
#  
10.0.0.0/8 via 192.168.1.254
+
10.0.0.0/8 via 192.168.1.254
  
'''Note:''' The <code>/etc/sysconfig/network-scripts/route-*</code> filename is very important. Adding the wrong interface extension at the end will result in the routes not being added after the next reboot. There will also be no reported errors on the screen or any of the log files in the <code>/var/log/</code> directory.
+
'''注意:''' 文件名<code>/etc/sysconfig/network-scripts/route-*</code>非常重要。在route-后面添加了错误的设备后缀会导致下次启动后路由不能被正确添加。而且不会在屏幕上报告错误或者在<code>/var/log/</code>目录下生成任何日志文件。
  
You can test the new file by running the <code>/etc/sysconfig/network-scripts/ifup-routes</code> command with the interface name as the sole argument. In the next example we check the routing table to see no routes to the 10.0.0.0 network and execute the <code>ifup-routes</code> command, which then adds the route:
+
你可以通过运行<code>/etc/sysconfig/network-scripts/ifup-routes</code>命令加设备名作为参数测试新建的文件。在下面例子里,我们先检查路由表查看没有到网络10.0.0.0的路由,然后执行<code>ifup-routes</code>命名把路由加上。
  
 +
[root@bigboy tmp]# netstat -nr
 +
Kernel IP routing table
 +
Destination Gateway   Genmask   Flags MSS Window irtt Iface
 +
192.168.1.0 0.0.0.0      255.255.255.0  U    0  0      0    wlan0
 +
169.254.0.0 0.0.0.0      255.255.0.0    U    0  0      0    wlan0
 +
0.0.0.0    192.168.1.1  0.0.0.0        UG    0  0      0    wlan0
 +
[root@bigboy tmp]# ./ifup-routes wlan0
 
  [root@bigboy tmp]# netstat -nr
 
  [root@bigboy tmp]# netstat -nr
 +
Kernel IP routing table
 +
Destination Gateway   Genmask Flags MSS Window irtt Iface
 +
192.168.1.0 0.0.0.0      255.255.255.0 U    0  0      0    wlan0
 +
169.254.0.0 0.0.0.0      255.255.0.0  U    0  0      0    wlan0
 +
10.0.0.0    192.168.1.254 255.0.0.0    UG    0  0      0    wlan0
 +
0.0.0.0    192.168.1.1  0.0.0.0      UG    0  0      0    wlan0
 +
[root@bigboy tmp]#
  
Kernel IP routing table
+
'''注意:''' 在基于Debian的系统中,永久静态路由是配置在文件<code>/etc/network/interfaces</code>中的。请参阅后面章节"Debian / Ubuntu网络配置"以了解更多详情。
 
+
Destination Gateway Genmask Flags MSS Window irtt Iface
+
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
+
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 wlan0
+
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 wlan0
+
[root@bigboy tmp]# ./ifup-routes wlan0
+
[root@bigboy tmp]# netstat -nr
+
Kernel IP routing table
+
Destination Gateway Genmask Flags MSS Window irtt Iface
+
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
+
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 wlan0
+
10.0.0.0 192.168.1.254 255.0.0.0 UG 0 0 0 wlan0
+
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 wlan0
+
[root@bigboy tmp]#
+
  
'''Note:''' In Debian based systems, permanent static routes are configured using the <code>/etc/network/interfaces</code> file. See the section "Debian / Ubuntu Network Configuration" later in this chapter for more details.
+
= 怎样删除路由 =
= How to Delete a Route =
+
  
Here's how to delete the routes added in the previous section.
+
这里介绍了怎样删除之前章节添加的路由。
  
 
  [root@bigboy tmp]# route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0
 
  [root@bigboy tmp]# route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0
  
The file <code>/etc/sysconfig/network-scripts/route-wlan0</code> will also have to be updated so that when you reboot the server will not reinsert the route. Delete the line that reads:
+
文件 <code>/etc/sysconfig/network-scripts/route-wlan0</code>也需要被更新,这样你重启服务器後路由才不会被重新加回来。删除如下的行:
  
 
  10.0.0.0/8 via 192.168.1.254
 
  10.0.0.0/8 via 192.168.1.254
= Changing NIC Speed and Duplex =
 
  
There is no better Linux investment than the purchase of a fully Linux compatible NIC card. Most Linux vendors will have a list of compatible hardware on their Web sites: read this carefully before you start hooking up you machine to the network. If you can't find any of the desired models in your local computer store, then a model in the same family or series should be sufficient. Most cards will work, but only the fully compatible ones will provide you with error-free, consistent throughput.
+
= 改变网卡的速度和双工 =
  
Linux defaults to automatically negotiating the speed and duplex of it's NIC automatically with that of the switch to which it is attached. Configuring a switch port to auto-negotiate the speed and duplex often isn't sufficient because there are frequently differences in the implementation of the protocol standard.
+
没有比购买一个完全的Linux兼容网卡更好的Linux投资了。大部分的Linux厂商在他们网站上都有一份兼容的硬件列表:在开始连接你的机器到网络之前请仔细阅读它。如果你没有找到任何在本地的计算机商店可以买到的型号,那么同系列也是可以的。大多数网卡都能正常工作,但只有完全兼容的可为您提供无差错的,持续的吞吐量。
  
Typically, NICs with failed negotiation will work, but this is usually accompanied by many collision type errors being seen on the NIC when using the ifconfig -a command and only marginal performance. Don't limit your troubleshooting of these types of errors to just failed negotiation; the problem could also be due to a bad NIC card, switch port, or cabling.
+
Linux缺省会在网卡连接到路由器上时自动协商判断网卡的速度和双工。配置一个路由器端口自动协商速度和双工往往是不足够的,因为不同的协议标准执行上常会有分歧。
  
<br>
+
实际上,网卡即使协商失败也会工作,但这通常会导致在网卡上使用ifconfig -a命令时看到许多冲突类型的错误和极低的性能。在故障排除时不要只把这类错误局限于协商失败;问题也可能是坏的网卡,路由器端口,或者网线导致的。
  
== Using mii-tool ==
+
== 使用mii-tool ==
  
One of the original Linux tools for setting the speed and duplex of your NIC card was the mii-tool command. It is destined to be deprecated and replaced by the newer ethtool command, but many older NICs support only mii-tool so you'll need to be aware of it. Issuing the command without any arguments gives a brief status report, as seen in the next example, with unsupported NICs providing an Operation not supported message. NICs that are not compatible with mii-tool often will still work, but you have to refer to the manufacturer's guides to set the speed and duplex to anything but auto-negotiate.
+
mii-tool是一个原始的设置网卡速度和双工的Linux工具。它以注定会被抛弃,而由新的ethtool命令取而代之,但是很多老的网卡只支持mii-toll,所以你还需要知道它。不带任何参数的执行命令会产生一个简要的状态报告,如下面样例所示,对于不支持的网卡将提示Operation not supported信息。mii-tool不兼容的网卡往往仍然可以工作,但是你需要根据制造商的指南来设定速度和双工,而不能自动协商。
  
 
  [root@bigboy tmp]# mii-tool
 
  [root@bigboy tmp]# mii-tool
SIOCGMIIPHY on 'eth0' failed: Operation not supported
+
SIOCGMIIPHY on 'eth0' failed: Operation not supported
eth1: 100 Mbit, half duplex, link ok
+
eth1: 100 Mbit, half duplex, link ok
[root@bigboy tmp]#
+
[root@bigboy tmp]#
  
 
+
通过-v参数使用verbose模式,你可以获得更多更详细的信息。在这个例子中,协商正常,网卡选择100Mbps,全双工模式(FD)。
By using the verbose mode -v switch you can get much more information. In this case, negotiation was OK, with the NIC selecting 100Mbps, full duplex mode (FD):
+
  
 
  [root@bigboy tmp]# mii-tool -v
 
  [root@bigboy tmp]# mii-tool -v
eth1: negotiated 100baseTx-FD, link ok
+
eth1: negotiated 100baseTx-FD, link ok
product info: vendor 00:10:18, model 33 rev 2
+
  product info: vendor 00:10:18, model 33 rev 2
basic mode: autonegotiation enabled
+
  basic mode: autonegotiation enabled
basic status: autonegotiation complete, link ok
+
  basic status: autonegotiation complete, link ok
capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
+
  capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
advertising: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
+
  advertising: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
link partner: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control
+
  link partner: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control  
[root@bigboy tmp]#
+
[root@bigboy tmp]#
 
+
<br>
+
  
=== Setting Your NIC's Speed Parameters with mii-tool ===
+
=== 用mii-tool设置网卡的速度参数 ===
  
You can set your NIC to force itself to a particular speed and duplex by using the -F switch with any of the following options: 100baseTx-FD, 100baseTx-HD, 10baseT-FD, or 10baseT-HD. Remember that you could lose all network connectivity to your server if you force your NIC to a particular speed/duplex that doesn't match that of your switch:
+
你可以通过-F参数强行指定网卡的速度和双工,选项包括:100baseTx-FD,100baseTx-HD,10baseT-FD,或者10baseT-HD。记住,你可能丢失网络连接如果你网卡的强制速度/双工模式与路由器不匹配的话:
  
 
  [root@bigboy tmp]# mii-tool -F 100baseTx-FD eth0
 
  [root@bigboy tmp]# mii-tool -F 100baseTx-FD eth0
  
Unfortunately there is no way to set this on reboot permanently except by placing it the command in the /etc/rc.local file to let it be run at the very end of the booting process or by creating your own startup script if you need it set earlier. Creating your own startup scripts is covered in Chapter 7, "[[Quick HOWTO : Ch07 : The Linux Boot Process|The Linux Boot Process]]".
+
不幸的是,除了把这个命令放到/etc/rc.local文件中让它在每次启动结束前执行,或者创建你自己的启动脚本如果你需要它更早被执行,我们没有办法让它在重启后永久生效。如何创建你自己的启动脚本在第7章涵盖, "[[Quick HOWTO : Ch07 : The Linux Boot process|The Linux Boot Process]]"
  
== Using ethtool ==
+
== 使用ethtool ==
  
The ethtool command is slated to be the replacement for mii-tool in the near future and tends to be supported by newer NIC cards.
+
ethtool命令被指定在不久的将来替代mii-tool,目的是支持新的网卡。
  
The command provides the status of the interface you provide as its argument. Here we see interface eth0 not doing autonegotiation and set to a speed of 100 Mbps, full duplex. A list of supported modes is also provided at the top of the output.
+
用设备名称作为参数,命令可以提供其状态。这里我们可以看到设备eth0没有设置自动协商并设定为100 Mbps速度,全双工。在输出的顶端列出了其所支持的模式。
  
 
  [root@bigboy tmp]# ethtool eth0
 
  [root@bigboy tmp]# ethtool eth0
Settings for eth0:
+
Settings for eth0:
Supported ports: [ TP MII ]
+
        Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
+
        Supported link modes:   10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
+
                                100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
+
        Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
+
        Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
+
                                100baseT/Half 100baseT/Full
Advertised auto-negotiation: No
+
        Advertised auto-negotiation: No
Speed: 100Mb/s
+
        Speed: 100Mb/s
Duplex: Full
+
        Duplex: Full
Port: MII
+
        Port: MII
PHYAD: 1
+
        PHYAD: 1
Transceiver: internal
+
        Transceiver: internal
Auto-negotiation: off
+
        Auto-negotiation: off
Supports Wake-on: g
+
        Supports Wake-on: g
Wake-on: g
+
        Wake-on: g
Current message level: 0x00000007 (7)
+
        Current message level: 0x00000007 (7)
Link detected: yes
+
        Link detected: yes
[root@bigboy tmp]#
+
[root@bigboy tmp]#
  
 +
=== 用ethtool设置网卡的速度参数 ===
  
=== Setting Your NIC's Speed Parameters with ethtool ===
+
不同于mii-tool,ethtool可以通过设定设备配置脚本里的ETHTOOL_OPTS变量而永久生效。在下面例子里,设定了在下次重启时设置100 Mbps,全双工和不自动协商:
 
+
Unlike mii-tool, ethtool settings can be permanently set as part of the interface's configuration script with the ETHTOOL_OPTS variable. In our next example, the settings will be set to 100 Mbps, full duplex with no chance for auto-negotiation on the next reboot:
+
 
+
<br>
+
  
 
  #
 
  #
# File: /etc/sysconfig/network-scripts/ifcfg-eth0
+
# File: /etc/sysconfig/network-scripts/ifcfg-eth0
#
+
#  
DEVICE=eth0
+
DEVICE=eth0
IPADDR=192.168.1.100
+
IPADDR=192.168.1.100
NETMASK=255.255.255.0
+
NETMASK=255.255.255.0
BOOTPROTO=static
+
BOOTPROTO=static
ONBOOT=yes
+
ONBOOT=yes
ETHTOOL_OPTS="speed 100 duplex full autoneg off"
+
ETHTOOL_OPTS="speed 100 duplex full autoneg off"
 
+
  
You can test the application of these parameters by shutting down the interface and activating it again with the ifup and ifdown commands. These settings can also be changed from the command line using the -s switch followed by the interface name and its desired configuration parameters.
+
你可以通过ifup和ifdown命令关闭和再激活设备,来测试这些参数。这些设定也可以通过在命令行中使用-s参数改变,后面跟上设备名称和相关配置参数。
  
 
  [root@bigboy tmp]# ethtool -s eth1 speed 100 duplex full autoneg off
 
  [root@bigboy tmp]# ethtool -s eth1 speed 100 duplex full autoneg off
[root@bigboy tmp]#
+
[root@bigboy tmp]#
  
The Linux man pages give more details on other ethtool options, but you can get a quick guide by just entering the ethtool command alone, which provides a quicker summary.
+
Linux的man命令提供了ethtool选项的更详细说明,但你可以通过只输入ethtool命令获得一个快速的摘要。
  
 
  [root@bigboy tmp]# ethtool
 
  [root@bigboy tmp]# ethtool
...
+
...
...
+
...
ethtool -s DEVNAME \
+
        ethtool -s DEVNAME \
[ speed 10|100|1000 ] \
+
                [ speed 10|100|1000 ] \
[ duplex half|full ] \
+
                [ duplex half|full ] \
[ port tp|aui|bnc|mii|fibre ] \
+
                [ port tp|aui|bnc|mii|fibre ] \
...
+
...
...
+
...
[root@bigboy tmp]#
+
[root@bigboy tmp]#
  
=== A Note About Duplex Settings ===
+
=== 关于双工设置 ===
  
By default, Linux NICs negotiate their speed and duplex settings with the switch. This is done by exchanging electronic signals called Fast Link Pulses (FLP). When the speed and duplex are forced to a particular setting the FLPs are not sent. When a NIC is in auto-negotiation mode and detects a healthy, viable link but receives no FLPs, it errs on the side of caution and sets its duplex to half-duplex and sometimes it will also set its speed to the lowest configurable value. It is therefore possible to force a switch port to 100 Mbps full duplex, but have the auto-negotiating server NIC set itself to 100Mbps half-duplex which will result in errors. The same is true for the switch if the switch port is set to auto-negotiate and server NIC is set to 100 Mbps full duplex. It is best to either force both the switch port and server NIC to either auto-negotiate or the same forced speed and duplex values.
+
缺省地,Linux网卡会跟交换机协商速度和双工设定。这通过交换叫做FLP(Fast Link Pules)的电子信号实现。当速度和双工被强制指定的时FLP就不会发送。当网卡使用自动协商模式而且检测到一个健康的,可用的连接时却没有收到FLP信号,它会报错并把自己的双工设为半双工模式,有时它也会设置速度到低一级的值。因此,这样可能交换机端口是强制100 Mbps全双工,但是自动协商的网卡把自己设为100Mbps半双工,结果产生错误。同样情况也可能发生在交换机上,如果交换机端口设为自动协商而服务器网卡强制100 Mbps全双工。最好是要么强制交换机端口和网卡都是自动协商,要么强制设定相同的速度和双工值。
= How to Convert Your Linux Server into a Simple Router =
+
  
Router/firewall appliances that provide basic Internet connectivity for a small office or home network are becoming more affordable every day, but when budgets are tight you might seriously want to consider modifying an existing Linux server to do the job.
+
= 怎样简单的将您的Linux服务器变成一个简单的路由器 =
  
Details on how to configure Linux firewall security are covered in Chapter 14, "[[Quick HOWTO : Ch14 : Linux Firewalls Using iptables|Linux Firewalls Using iptables]]", but you need to understand how to activate routing through the firewall before it can become a functioning networking device.
+
由路由器/防火墙设备为一个小型办公室或家庭网络提供基本的互联网连接正日益变得可以承受。但是当预算紧张时,你可能会认真地考虑修改现有的Linux服务器来做完成这项任务。
  
<br>
+
详细介绍如何配置Linux防火墙的安全是在第14章涵盖, "[[Quick HOWTO : Ch14 : Linux Firewalls Using iptables|Linux Firewalls Using iptables]]",但你需要了解如何激活路由通过防火墙,然后它才可以成为一个正常运作的网络设备。
  
== Configuring IP Forwarding ==
+
== 配置IP转发 ==
  
For your Linux server to become a router, you have to enable '''packet forwarding'''. In simple terms packet forwarding enables packets to flow through the Linux box from one network to another. The Linux kernel configuration parameter to activate this is named <code>net.ipv4.ip_forward</code> and can be found in the file <code>/etc/sysctl.conf</code>. Remove the "#" from the line related to packet forwarding.
+
要想要你的Linux服务器成为路由器,你必须启用'''包转发(packet forwarding)'''。简单说包转发就是让数据包通过Linux机器从一个网络传输到另一个网络。找到文件<code>/etc/sysctl.conf</code>中的Linux内核参数<code>net.ipv4.ip_forward</code>,通过删除关于包转发相关行前的"#"来激活它。
  
 
  Before:
 
  Before:
 
   
 
   
# Disables packet forwarding  
+
# Disables packet forwarding
net.ipv4.ip_forward=0
+
net.ipv4.ip_forward=0
 
   
 
   
After:
+
After:
 
   
 
   
# Enables packet forwarding  
+
# Enables packet forwarding
net.ipv4.ip_forward=1
+
net.ipv4.ip_forward=1
  
 
+
只有你重新启动的时候包转发才会启用,那时Linux将在特殊的基于RAM内存的<code>/proc</code>文件系统的子目录中创建一个文件。如果要立即激活这个功能,你必须通过sysctl命令加上-p参数来强迫Linux读取code>/etc/sysctl.conf</code>文件。下面是如何操作的:
This enables packet forwarding only when you reboot at which time Linux will create a file in one of the subdirectories of the special RAM memory-based <code>/proc</code> filesystem. To activate the feature immediately you have to force Linux to read the <code>/etc/sysctl.conf</code> file with the sysctl command using the <code>-p</code> switch. Here is how it's done:
+
  
 
  [root@bigboy tmp] sysctl -p
 
  [root@bigboy tmp] sysctl -p
sysctl -p
+
sysctl -p
net.ipv4.ip_forward = 1
+
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
+
net.ipv4.conf.default.rp_filter = 1
kernel.sysrq = 0
+
kernel.sysrq = 0
kernel.core_uses_pid = 1
+
kernel.core_uses_pid = 1
[root@bigboy tmp]#
+
[root@bigboy tmp]#
  
 +
详情请参阅附录I以获得更多调整内核参数的信息,就调整内核参数。
  
Please refer to Appendix I for more information on adjusting kernel parameters.
+
== 配置ARP代理 ==
  
<br>
+
如果服务器需要在同一个网络里把一个包传送到另一个设备上,它会在网络上发一个ARP(Address Resolution Protocol)请求来询问目的设备的MAC地址。
  
== Configuring Proxy ARP ==
+
如果同一服务器需要把一个包传送到远程网络的另一个设备上,过程就不一样了。服务器首先查看路由表来寻找网络上的最佳路由器的IP地址,由它把包传送的目的地。服务器随后发送一个ARP请求来询问匹配路由器IP地址的MAC地址。然后通过路由器的MAC地址把包和远程服务器的IP地址发送给路由器。
  
If a server needs to send a packet to another device on the same network, it sends out an ARP request to the network asking for the MAC address of the other device.
+
如果在网络上没有合适的路由器,服务器会发送一个ARP请求来询问远程服务器的MAC地址。一些配置过的路由器可以回答这些关于远程网络的ARP请求,这个功能就叫做'''ARP代理'''。这种做法有一些缺点。其中一个最普遍的问题是在网络上的两个路由器都配置了ARP代理。在这种情况下,它们都可能会回答本地服务器关于远程网络MAC地址的ARP请求。如果其中一个路由器有远程服务器的不正确路由表记录,那么到远程服务器的传输就有偶尔丢失的风险。换句话说你失去了对路由的控制。
  
If the same server needs to send a packet to another device on a remote network the process is different. The server first takes a look at its routing table to find out the IP address of the best router on its network that will be able to relay the packet to the destination. The server then sends an ARP request for the MAC address that matches the router's IP address. It then sends the packet to the router using the router's MAC address and a destination IP address of the remote server.
+
'''注意:''' 因为这个和其他一些原因,一般在路由器上配置ARP代理不是一个好的选择。比较好的做法是始终在服务器上配置一个缺省网关,而对缺省网关可能不知道的网络则使用通过其他路由器的单独路由记录。
  
If there is no suitable router on its network, the server will then send out an ARP request for the MAC address of the remote server. Some routers can be configured to answer these types of ARP requests for remote networks. This feature is called '''proxy ARP'''. There are some disadvantages with this. One of the most common problems occurs if two routers are on the network configured for proxy ARP. In this scenario there is the possibility that either one will answer the local server's ARP request for the MAC address of the remote server. If one of the routers has an incorrect routing table entry for the remote network, then there is the risk that traffic to the remote server will occasionally get lost. In other words you can lose routing control.
+
有些类型的桥接模式防火墙,必须要ARP代理来正常运作。这些设备一般作为串行链的一部分,把一个局域网上的多个网络交换机连接在一起,用来保护局域网上的某一部分不受其他部分的流量的影响。这种防火墙一般不配置局域网中的IP地址,看起来就像一段智能的电缆在有选择的屏蔽数据包。
  
'''Note:''' It is for this and other reasons that it is generally not a good idea to configure proxy ARP on a router. It is also good to always configure a default gateway on your server and use separate routing entries via other routers for all networks your default gateway may not know about.
+
如果你需要启动Linux服务器上的ARP代理,/proc文件系统就再次发挥作用了。ARP代理是由<code>/proc/sys/net/ipv4/conf/</code>目录下的文件控制的。这个目录下有服务器中每块运转网卡的对应子目录。每个子目录下都有个<code>proxy_arp</code>文件。如各这个文件中的值是0,那么这个接口上的ARP代理就是被禁用的;如果这个值是1,那么就是启用的。
  
Some types of bridging mode firewalls need to have proxy ARP enabled to operate properly. These devices are typically inserted as part of a daisy chain connecting multiple network switches together on the same LAN while protecting one section of a LAN from traffic originating on another section. The firewall typically isn't configured with an IP address on the LAN and appears to be an intelligent cable capable of selectively blocking packets.
+
你可以使用附录II提到的<code>/etc/sysctl.conf</code>文件来激活或禁用ARP代理。下面是激活ARP代理的例子,首先是对所有接口的,其次是对<code>eth0</code><code>wlan0</code>接口的。
 
+
If you need to enable proxy ARP on a Linux server the /proc filesystem comes into play again. Proxy ARP is handled by files in the <code>/proc/sys/net/ipv4/conf/</code> directory. This directory then has subdirectories corresponding to each functioning NIC card on your server. Each subdirectory then has a file called <code>proxy_arp</code>. If the value within this file is 0, then proxy ARP on the interface is disabled; if the value is <code>1</code> then it is enabled.
+
 
+
You can use the <code>/etc/sysctl.conf file</code> mentioned in Appendix II to activate or disable proxy ARP. The next example activates proxy ARP, first for all interfaces and then for interfaces <code>eth0</code> and <code>wlan0</code>.
+
  
 
  #
 
  #
# File: /etc/sysctl.conf
+
# File: /etc/sysctl.conf
#
+
#  
 
   
 
   
# Enables Proxy ARP on all interfaces
+
# Enables Proxy ARP on all interfaces
net/ipv4/conf/all/proxy_arp = 1
+
net/ipv4/conf/all/proxy_arp = 1
 
   
 
   
# Enables Proxy ARP on interfaces eth1 and wlan0
+
# Enables Proxy ARP on interfaces eth1 and wlan0
net/ipv4/conf/eth1/proxy_arp = 1
+
net/ipv4/conf/eth1/proxy_arp = 1
net/ipv4/conf/wlan0/proxy_arp = 1
+
net/ipv4/conf/wlan0/proxy_arp = 1
 
+
  
You can then activate these settings with the sysctl command.
+
你可以使用sysctl命令激活这些设置。
  
 
  [root@bigboy tmp] sysctl -p
 
  [root@bigboy tmp] sysctl -p
= Configuring Your /etc/hosts File =
 
  
The /etc/hosts file is just a list of IP addresses and their corresponding server names. Your server will typically check this file before referencing DNS. If the name is found with a corresponding IP address then DNS won't be queried at all. Unfortunately, if the IP address for that host changes, you also have to also update the file. This may not be much of a concern for a single server, but can become laborious if it has to be done companywide. For ease of management, it is often easiest to limit entries in this file to just the loopback interface and also the server's own hostname, and use a centralized DNS server to handle most of the rest. Sometimes you might not be the one managing the DNS server, and in such cases it may be easier to add a quick /etc/hosts file entry till the centralized change can be made.
+
= 配置你的/etc/hosts文件 =
 +
 
 +
/etc/hosts文件只是一个IP地址和对应服务器名的列表。服务器一般会在查询DNS前检查这个文件。如果找到了有对应IP地址的名称,那么就根本不会去查询DNS。不幸的是,如果主机的IP地址变化了,你也必须更新这个文件。这对单机来说不是什么大问题,但是如果是更新整个公司的话就很艰巨了。为了易于管理,通常是在文件只放loopback接口和本机机器名的记录,然后使用集中的DNS服务器来处理剩下的部分。有时你可能不能控制DNS服务器,在这种情况下,向/etc/hosts文件中添加一条记录就简单多了,直到其加入集中的DNS中。
  
 
  192.168.1.101 smallfry
 
  192.168.1.101 smallfry
  
 
+
在上面的例子中,服务器smallfry的IP地址是192.168.1.101。你可以在ping,telnet或者其他网络相关程序中使用smallfry来代替192.168.1.101。下面的例子是使用ping命令查看smallfry是不是在网络上存在:
In the example above server smallfry has an IP address of 192.168.1.101. You can access 192.168.1.101 using the ping, telnet or any other network aware program by referring to it as smallfry. Here is an example using the ping command to see whether smallfry is alive and well on the network:
+
  
 
  [root@bigboy tmp]# ping smallfry
 
  [root@bigboy tmp]# ping smallfry
PING zero (192.168.1.101) 56(84) bytes of data.
+
PING zero (192.168.1.101) 56(84) bytes of data.
64 bytes from smallfry (192.168.1.101): icmp_seq=0 ttl=64 time=0.197 ms
+
64 bytes from smallfry (192.168.1.101): icmp_seq=0 ttl=64 time=0.197 ms  
64 bytes from smallfry (192.168.1.101): icmp_seq=1 ttl=64 time=0.047 ms
+
64 bytes from smallfry (192.168.1.101): icmp_seq=1 ttl=64 time=0.047 ms
 +
 +
--- smallfry ping statistics ---
 +
2 packets transmitted, 2 received, 0% packet loss, time 2017ms rtt
 +
min/avg/max/mdev = 0.034/0.092/0.197/0.074 ms, pipe 2
 +
[root@bigboy tmp]#
  
 
+
你也可以在行后面添加别名,把其他名字和你的服务器关联起来。这里我们设置smallfry也可以用<code>tiny</code><code>littleguy</code>访问。
--- smallfry ping statistics ---
+
2 packets transmitted, 2 received, 0% packet loss, time 2017ms
+
rtt min/avg/max/mdev = 0.034/0.092/0.197/0.074 ms, pipe 2
+
[root@bigboy tmp]#
+
 
+
 
+
You can also add aliases to the end of the line which enable you to refer to the server using other names. Here we have set it up so that smallfry can also be accessed using the names <code>tiny</code> and <code>littleguy</code>.
+
  
 
  192.168.1.101 smallfry tiny littleguy
 
  192.168.1.101 smallfry tiny littleguy
  
 +
你永远不要在文件中重复使用同一个IP地址,因为Linux只会使用它找到的第一个记录值。
  
You should never have an IP address more than once in this file because Linux will use only the values in the first entry it finds.
+
  192.168.1.101 smallfry # (Wrong)
 
+
192.168.1.101 tiny     # (Wrong)
  192.168.1.101 smallfry # (Wrong)
+
192.168.1.101 littleguy # (Wrong)
192.168.1.101 tiny # (Wrong)
+
192.168.1.101 littleguy # (Wrong)
+
 
+
<br>
+
  
== The loopback Interface's localhost Entry ==
+
== localhost里面的loopback接口 ==
  
Usually the first entry in /etc/hosts defines the IP address of the server's virtual loopback interface. This is usually mapped to the name localhost.localdomain (the universal name used when a server refers to itself) and localhost (the shortened alias name). By default, Fedora inserts the hostname of the server between the 127.0.0.1 and the localhost entries like this:
+
通常/etc/hosts里面的第一条记录会定义服务器虚拟loopback接口的IP地址。它通常映射到名称localhost.localdomain(代替服务器自己的通用名称)和localhost(短别名)。默认情况下,Fedora会把主机名插到127.0.0.1和localhost之间:
  
 
  127.0.0.1 bigboy localhost.localdomain localhost
 
  127.0.0.1 bigboy localhost.localdomain localhost
  
 
+
当服务器连接到互联网上的时候,第一条记录的127.0.0.1后面应该是服务器的FQDN名称(fully qualified domain name)。例如,<code>bigboy.mysite.com</code>
When the server is connected to the Internet this first entry after the 127.0.0.1 needs to be the fully qualified domain name (FQDN) of the server. For example, <code>bigboy.mysite.com</code>, like this:
+
  
 
  127.0.0.1 bigboy.my-site.com localhost.localdomain localhost
 
  127.0.0.1 bigboy.my-site.com localhost.localdomain localhost
  
 +
有些程序例如Sendmail非常敏感,如果觉得检测到了不正确的FQDN名称,它会缺省使用localhost.localdomain联络网络上的其他服务器。这会导致混淆,因为其他服务器也认为自己是localhost.localdomain.
  
Some programs such as Sendmail are very sensitive to this and if they detect what they feel is an incorrect FQDN they will default to using the name localhost.localdomain when communicating with another server on the network. This can cause confusion, as the other server also feels it is localhost.localdomain.
+
'''注意:''' 你必须始终有一条把localhost和localhost。localdomain映射到127.0.0.1上的记录,以使Linux正常地安全地工作。
  
'''Note:''' You must always have a localhost and localhost.localdomain entry mapping to 127.0.0.1 for Linux to work properly and securely.
+
= Debian / Ubuntu的网络配置 =
= Debian / Ubuntu Network Configuration =
+
  
Many of the core Fedora / Redhat commands and configuration files covered in this chapter can be used in Debian based operating systems, but there are some key differences.
+
这章涉及的大部分Fedora / Redhat命令和配置文件也可一个用于基于Debian的操作系统,但也有一些关键的差别。
  
== The /etc/network/interfaces File ==
+
== /etc/network/interfaces文件 ==
  
The main network configuration file is the /etc/network/interfaces file in which all the network interface parameters are defined. The file is divided into stanzas:
+
主网络配置文件是/etc/network/interfaces ,所有网络接口相关的参数都是在这里定义的。文件划分成多个小节:
  
=== The auto Stanza ===
+
=== auto小节 ===
  
The auto stanza defines the interfaces that should be automatically initialized when the system boots up.
+
auto小节定义了接口是不是要在系统启动时自动初始化。
  
=== The mapping Stanza ===
+
=== mapping小节 ===
  
This stanza maps configuration parameters for an interface depending on the output of a script. For example, on booting the script could prompt you as to whether your laptop Linux system is at home or work with the mapping statement using the answer to configure the appropriate IP address.
+
本小节根据脚本的输出结果为接口映射配置参数。例如,在启动时,可以弹出一个脚本询问你的笔记本Linux系统是在家使用还是在单位使用,预设的映射语句通过答案来配置相应的IP地址。
  
By default the much simpler hotplug system is used which assumes that the interfaces will have only one purpose. Typical hotplug configurations simply assign each physical interface with a matching logical interface name (nick name).
+
缺省的,当假定接口只有一个目的时使用简单得多的热插拔系统。典型的热插拔配置就是把每个物理接口分配一个逻辑接口名称(别名)。
  
 
  mapping hotplug
 
  mapping hotplug
script grep
+
        script grep
map eth0 eth0
+
        map eth0 eth0
map eth1
+
        map eth1
  
In this case interface eth0 is specifically given the logical name eth0, while the logical name for eth1 is implied to be the same.
+
在这个例子里,接口eth0被专门给与了逻辑名称eth0,逻辑名称eth1也是一样的。
  
=== The iface Stanza ===
+
=== iface小节 ===
  
The iface stanza defines the characteristics of a logical interface. Typically the first line of these stanzas starts with the word iface, followed by the logical name of the interface, the protocol used, and finally the type of addressing scheme to be used, such as DHCP or static. Protocol keywords include inet for regular TCP/IP, inet6 for IPv6, ipx for the older IPX protocol used by Novell, and loopback for loopback addresses.
+
iface小节定义的逻辑接口的特性。通常小节的第一行是以单词iface开头,跟着接口的逻辑名称,使用的协议,最后是使用的寻址模式,例如DHCP或者静态。协议关键词包括对应常规TCP/IP协议的inet,IPv6的inet6,Novell旧的IPX协议的ipx,和loopback地址的loopback。
  
Subsequent lines in the stanza define protocol characteristics such as addresses, subnet masks, and default gateways. In this example, interface eth1 is given the IP address 216.10.119.240/27 while interface eth0 gets its IP address using DHCP.
+
小节后面的行定义了协议的特性,例如地址,子网掩码,和缺省网关。在这个例子中,eth1接口被赋予IP地址216.10.119.240/27,而eth0接口使用DHCP。
  
 
  # The primary network interface
 
  # The primary network interface
auto eth1
+
auto eth1
iface eth1 inet static
+
iface eth1 inet static
address 216.10.119.240
+
        address 216.10.119.240
netmask 255.255.255.224
+
        netmask 255.255.255.224
network 216.10.119.224
+
        network 216.10.119.224
broadcast 216.10.119.255
+
        broadcast 216.10.119.255
gateway 216.10.119.241
+
        gateway 216.10.119.241
dns-nameservers 216.10.119.241
+
        dns-nameservers 216.10.119.241
 +
 +
# The secondary network interface
 +
auto eth0
 +
iface eth0 inet dhcp
  
# The secondary network interface
+
'''注意:''' 当使用静态IP地址时,通常也要定义一个缺省网关。记得把网关定义和对应路由器IP地址放在正确的小节。
auto eth0
+
iface eth0 inet dhcp
+
  
'''Note:''' When static IP addresses are used, a default gateway usually needs to be defined. Remember to place the gateway statement in the correct stanza with the appropriate router IP address.
+
=== 创建接口别名 ===
  
=== Creating Interface Aliases ===
+
一旦主接口被定义了,在/etc/network/interfaces文件中定义IP别名就容易了。只需要一个修改过的主接口的iface小节副本。第一行要加上冒号和子接口编号,而且后面只需要跟上子网掩码和新的IP地址。在这个例子中接口eth1:1的IP地址是216.10.119.239。
 
+
IP aliases can be easily created in the /etc/network/interfaces file once the main interface has already been defined. A modified duplicate of the main interfaces' iface stanza is required. A colon followed by the sub interface number needs to be added to the first line, and only the subnet mask and the new IP address needs to follow as can be seen in this example for interface eth1:1 with the IP address 216.10.119.239.
+
  
 
  auto eth1:1
 
  auto eth1:1
iface eth1:1 inet static
+
iface eth1:1 inet static
address 216.10.119.239
+
        address 216.10.119.239
netmask 255.255.255.224
+
        netmask 255.255.255.224
  
=== Adding Permanent Static Routes ===
+
=== 添加永久静态路由 ===
  
The <code>up</code> option in the appropriate <code>iface</code> stanza of the <code>/etc/network/interfaces</code> file allows you to selectively run commands once the specified interface becomes activated with the <code>ifup</code> command. This makes it useful when adding permanent static routes.
+
<code>/etc/network/interfaces</code>文件的恰当<code>iface</code>小节中添加<code>up</code>选项,可以让你在制定接口被激活时有选择性的用<code>ifup</code>执行命令。这在添加永久静态路由是很有用。
  
In this example, a route to the 10.0.0.0/8 network via router address 216.10.119.225 has been added. Remember, the <code>up</code> option and the command must reside on the same line of the stanza.
+
在这个例子中,添加了一个通过路由器216.10.119.225到网络10.0.0.0/8的路由。记住,<code>up</code>选项和此命令必须在小组的同一行。
  
 
  # The primary network interface
 
  # The primary network interface
auto eth1
+
auto eth1
iface eth1 inet static
+
iface eth1 inet static
...
+
        ...
...
+
        ...
...
+
        ...
up route add -net 10.0.0.0 netmask 255.0.0.0 gw 216.10.119.225 eth1
+
        up route add -net 10.0.0.0 netmask 255.0.0.0 gw 216.10.119.225 eth1
  
=== A complete /etc/network/interfaces file ===
+
=== 一个完整的/etc/network/interfaces文件 ===
  
We can now construct a complete file based on the previous examples we discussed. Just like in Fedora, interfaces can be activated with the ifup and ifdown commands.
+
现在我们可以用根据之前讨论的例子构造一个完整的文件。就像在Fedora中一样,可用ifup和ifdown命令激活接口。
  
 
  #  
 
  #  
# Debian / Ubuntu  
+
# Debian / Ubuntu
#
+
#  
 
+
#
+
#  
# File: /etc/network/interfaces
+
# File: /etc/network/interfaces
#
+
#  
 
+
# The loopback network interface
+
# The loopback network interface
auto lo
+
auto lo
iface lo inet loopback
+
iface lo inet loopback
 
+
# This is a list of hotpluggable network interfaces.
+
# This is a list of hotpluggable network interfaces.
# They will be activated automatically by the hotplug subsystem.
+
# They will be activated automatically by the hotplug subsystem.
mapping hotplug
+
mapping hotplug
script grep
+
        script grep
map eth0 eth0
+
        map eth0 eth0
map eth1 eth1
+
        map eth1 eth1
 
+
# The primary network interface
+
# The primary network interface
auto eth1
+
auto eth1
iface eth1 inet static
+
iface eth1 inet static
address 216.10.119.240
+
        address 216.10.119.240
netmask 255.255.255.224
+
        netmask 255.255.255.224
network 216.10.119.224
+
        network 216.10.119.224
broadcast 216.10.119.255
+
        broadcast 216.10.119.255
gateway 216.10.119.241
+
        gateway 216.10.119.241
# dns-* options are implemented by the resolvconf package, if installed
+
        # dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 216.10.119.241
+
        dns-nameservers 216.10.119.241
wireless-key 98d126d5ac
+
        wireless-key 98d126d5ac
wireless-essid schaaffe
+
        wireless-essid schaaffe
 
+
up route add -net 10.0.0.0 netmask 255.0.0.0 gw 216.10.119.225 eth1
+
        up route add -net 10.0.0.0 netmask 255.0.0.0 gw 216.10.119.225 eth1
 
+
auto eth1:1
+
auto eth1:1
iface eth1:1 inet static
+
iface eth1:1 inet static
address 216.10.119.239
+
        address 216.10.119.239
netmask 255.255.255.224
+
        netmask 255.255.255.224
 
+
# The secondary network interface
+
# The secondary network interface
auto eth0
+
auto eth0
iface eth0 inet dhcp
+
iface eth0 inet dhcp
  
 +
关于更多关于<code>/etc/network/interfaces</code>文件的信息,只要在命令行执行<code>man interfaces</code>就可以了。
  
For more information on the <code>/etc/network/interfaces</code> file just issue the command <code>man interfaces</code> from the command line.
 
 
= 结论 =
 
= 结论 =
  
 
你可以想象,配置Linux的网络只是提供互联网接入到您的服务器第一步。实际操作中,总是存在一些事情可能出错,可完全脱离你的控制。好的系统管理员了解能够找出这些问题可能原因的所需工具,使他们能够找到相应类型的帮助以解决它。未来两章向您展示如何在事情出错时自信地调试您的网络和Linux服务器应用。对你的公司和事业来说,你查明和纠正这些问题的能力将会被证明是非常宝贵的。
 
你可以想象,配置Linux的网络只是提供互联网接入到您的服务器第一步。实际操作中,总是存在一些事情可能出错,可完全脱离你的控制。好的系统管理员了解能够找出这些问题可能原因的所需工具,使他们能够找到相应类型的帮助以解决它。未来两章向您展示如何在事情出错时自信地调试您的网络和Linux服务器应用。对你的公司和事业来说,你查明和纠正这些问题的能力将会被证明是非常宝贵的。
 +
[[Category:Quick_HOWTO]]

2015年6月2日 (二) 16:28的最新版本


介绍

现在你已经牢牢掌握了许多最常用的网络概念,是时候应用它们来配置你的服务器了。其中一些设置在Linux安装过程中已经自动配置过了,但是你经常会发现你必须知道怎样修改这些初始设定。无论是你需要移动你的服务器到另一个网络中,还是添加一块新的网卡或者使用另一种方式连接到Internet。

在第二章中, 网络介绍,我们首先解释了什么是TCP/IP,所以我们也将从讨论怎样配置服务器的IP开始Linux的网络这一章。

怎样配置网卡的IP地址

你需要知道的一切必要步骤来配置网卡的IP地址。网站的购物车应用可能需要额外的固定IP地址。你也可能需要增加第二块网卡,用于处理服务器的数据备份。最后,但并不少见,你可能只想在服务器上玩玩以测试你的技巧。

本节将展示怎样处理最常见的服务器IP操作。

确定你的IP地址

大多数现代电脑都有一个以太网端口。在Linux安装中,这个设备被称为eth0。你可以通过ifconfig命令确定这个设备的IP地址。

[root@bigboy tmp]# ifconfig -a

eth0 Link encap:Ethernet HWaddr 00:08:C7:10:74:A8
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:11 Base address:0x1820

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:787 errors:0 dropped:0 overruns:0 frame:0
TX packets:787 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:82644 (80.7 Kb) TX bytes:82644 (80.7 Kb)
wlan0 Link encap:Ethernet HWaddr 00:06:25:09:6A:B5
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:47379 errors:0 dropped:0 overruns:0 frame:0
TX packets:107900 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:4676853 (4.4 Mb) TX bytes:43209032 (41.2 Mb)
Interrupt:11 Memory:c887a000-c887b000

wlan0:0 Link encap:Ethernet HWaddr 00:06:25:09:6A:B5
inet addr:192.168.1.99 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:11 Memory:c887a000-c887b000

[root@bigboy tmp]#

在这个例子中,eth0是没有IP地址的,因为这台机器用无线网卡wlan0作为主网卡。接口wlan0有一个IP地址192.168.1.100和子网掩码255.255.255.0。

你可以看到,这个命令提供了每块网卡的中断或者PCI总线ID等有用信息。在某些罕见情况中,你可能发现你的网卡不工作是因为它与其他一些设备共享了中断和内存访问地址。你可以通过查看/proc/interrupts文件来获得系统使用的全部中断请求(IRQs)。在下面的例子中, 我们可以看到从IRQ 0 到 15没有中断冲突,每个中断只有一条记录。设备eth0和eth1分别使用了中断10和5:

[root@bigboy tmp]# cat /proc/interrupts
             CPU0
   0:  2707402473          XT-PIC timer
   1:          67          XT-PIC i8042
   2:           0          XT-PIC cascade
   5:      411342          XT-PIC eth1
   8:           1          XT-PIC rtc
   10:    1898752          XT-PIC eth0
   11:          0          XT-PIC uhci_hcd
   12:         58          XT-PIC i8042
   14:    5075806          XT-PIC ide0
   15:        506          XT-PIC ide1
  NMI:          0
  ERR:         43
[root@bigboy tmp]#

如果有冲突,您可能需要参考引发问题设备的手册,以确定是否可以用另一个中断或内存I/O地址。

改变你的IP地址

如果需要,你可以使用ifconfig命令为eth0指定一个IP地址。

[root@bigboy tmp]# ifconfig eth0 10.0.0.1 netmask 255.255.255.0 up

命令行结尾的"up"会激活这个设备。要让这个设定在你每次启动时都生效,你需要把这个命令加到/etc/rc.local文件中。它会在每次启动时执行。

Fedora Linux通过在/etc/sysconfig/network-scripts目录下的配置文件,使这个设定变得简单。设备eth0对应文件ifcfg-eth0,eth1对应ifcfg-eth1,等等。你可以把IP地址信息放到这些文件中,它们会在Linux启动时自动配置你的网卡。见Figure 3-1两个eth0设备的例子。一个假设该网卡有一个固定IP地址,另一个假设它需要通过DHCP获得IP地址。


图 3-1 - network-scripts的文件格式

固定IP地址
[root@bigboy tmp]# cd /etc/sysconfig/network-scripts
[root@bigboy network-scripts]# cat ifcfg-eth0
# 
# File: ifcfg-eth0
# 
DEVICE=eth0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
BOOTPROTO=static ONBOOT=yes
# 
# The following settings are optional
# 
BROADCAST=192.168.1.255
NETWORK=192.168.1.0

[root@bigboy network-scripts]#
通过DHCP获得IP地址
[root@bigboy tmp]# cd /etc/sysconfig/network-scripts
[root@bigboy network-scripts]# cat ifcfg-eth0

# 
# File: ifcfg-eth0
# 
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

[root@bigboy network-scripts]#

你可以看到eth0将在启动时被激活,因为参数ONBOOT的值是yes不是no。你可以在第2章中"Introduction to Networking"阅读更多关于netmask和DHCP内容。

缺省的RedHat/Fedora安装会在network-scripts文件包含广播和网络设定。这些都是可选的。

在改变网卡的配置文件後,你需要停止然後激活它以使修改生效。ifdown和ifup命令可用于实现这个目的:

[root@bigboy network-scripts]# ifdown eth0
[root@bigboy network-scripts]# ifup eth0

你的服务器必须有一个默认网关,使它能够与Internet通信。这将在本章的稍後涉及。

DHCP如何影响你使用的DNS服务器

DHCP服务器不仅为你的Linux机器提供IP地址,也包括所需的DNS服务器。当为一个网络设备设置DHCP时,确保在/etc/resolv.conf文件注释掉相关的服务器配置,以防止任何冲突。

为一个网卡设定多个IP地址

在上一节“确定你的IP地址中”大家可能注意到有两个无线网络设备:wlan0和wlan0:0。设备wlan0:0实际上是wlan0的子设备,一个虚拟的子设备也被称为IP别名。IP别名是为一个网卡创建多个IP地址的最常见方法。别名的命名方式是父设备名称:X,X是你选择的子设备号。

创建一个IP别名的过程与上一节“改变你的IP地址”中描述的非常相似:

  • 首先,确保真实的父网络设备存在。
  • 确认你计划使用的名字没有同名的其他IP别名存在。这里我们要创建一个网络设备wlan0:0。
  • 用ifconfig命令创建虚拟网络设备
[root@bigboy tmp]# ifconfig wlan0:0 192.168.1.99 netmask 255.255.255.0 up
  • 你也应该一个文件/etc/sysconfig/network-scripts/ifcfg-wlan0:0,这样这个别名就可以被ifup和ifdown命令自动管理。这个是一个配置样例:
DEVICE=wlan0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.99
NETMASK=255.255.255.0

下面命令用于激活和停止这个别名设备:

[root@bigboy tmp]# ifup wlan0:0
[root@bigboy tmp]# ifdown wlan0:0

注意: 关闭主网络设备也同时关闭其所有别名。而别名可以独立于其他网络设备关闭。

完成这4个简单的步骤后,你应该能从网络的其他服务器上ping通这个新的IP别名。

为DSL连接设定IP地址

如果你使用是有固定或静态IP地址的DSL连接,那么配置步骤和之前描述的是一样的。你把以太网接口连接到DSL modem上,配置ISP提供的IP地址,子网掩码,广播地址,和网关信息。重启网络设备后你就应该连上网了。记住,你可能还需要正确地配置DNS服务器。

如果你使用的是有DHCP或或动态IP地址的DSL连接,那么过程就不一样了。你的ISP会为您提供一个PPPoE(PPP authentication over Ethernet)的用户名和密码,这将使您的电脑在每次启动时透明地登录到互联网上。Fedora的Linux需要安装rp-PPPoE的RPM软件包来支持这个功能。

注意:除非你特别申请静态IP地址,你的ISP会向你提供一个基于DHCP的连接。而DHCP分配给你的机器或者互联网路由的IP地址可能很多天都不会改变,你可能会误为是静态的。

下载和安装RPM包并不难。如果你需要复习,第6章"Installing Linux Software"中涵盖了关于RPM操作的详细内容。 当寻找该文件时,记得PPPoE的RPM文件名通常是以rp-pppoe开头,後面是版本号,例如:rp-pppoe-3.5-8.i386.rpm。

在安装RPM後,你需要经过若干步骤来完成连接。PPPoE配置将创建立一个名为ppp0的虚拟接口,它将通过物理网络接口eth0的来连接。下面是你需要做的:

  • 备份你的ifcfg-eth0文件。
[root@bigboy tmp]#
[root@bigboy tmp]# cd /etc/sysconfig/network-scripts/
[root@bigboy network-scripts]# ls ifcfg-eth0 ifcfg-eth0
[root@bigboy network-scripts]# cp ifcfg-eth0 DISABLED.ifcfg-eth0
  • 编辑你的ifcfg-eth0文件,删除IP信息,并且设定在启动时激活。
DEVICE=eth0
ONBOOT=no
  • 关闭你的eth0设备。
[root@bigboy network-scripts]# ifdown eth0
[root@bigboy network-scripts]#
  • 运行adsl-setup配置脚本
[root@bigboy network-scripts]# adsl-setup
它会提示你输入你的ISP用户名,要使用的网络设备(eth0)和是否你需要连接长期保持。如果可能我们都使用缺省设置。
Welcome to the ADSL client setup.� First, I will run some checks on

your system to make sure the PPPoE client is installed properly...

LOGIN NAME

Enter your Login Name (default root): bigboy-login@isp

INTERFACE

Enter the Ethernet interface connected to the ADSL modem
For Solaris, this is likely to be something like /dev/hme0.
For Linux, it will be ethX, where 'X' is a number.
(default eth0):

Do you want the link to come up on demand, or stay up continuously?
If you want it to come up on demand, enter the idle time in seconds
after which the link should be dropped.� If you want the link to
stay up permanently, enter 'no' (two letters, lower-case.)
NOTE: Demand-activated links do not interact well with dynamic IP
addresses. You might have some problems with demand-activated links.

Enter the demand value (default no):


接着会提示你输入你的DNS服务器信息。这步会编辑你的/etc/resolv.conf文件。如果你在服务器上以缓存DNS模式运行BIND,就不用输入直接回车。如果你希望你的ISP的DNS服务器自动提供IP地址,就输入单词server。
请参阅第18章, "Configuring DNS", 以获得BIND和DNS的更详细信息。
DNS
Please enter the IP address of your ISP's primary DNS server.
If your ISP claims that 'the server will provide dynamic DNS addresses', enter 'server' (all lower-case) here.
If you just press enter, I will assume you know what you are doing and not modify your DNS setup. 
Enter the DNS information here:
脚本会提示你输入你的ISP密码
PASSWORD

Please enter your Password:
Please re-enter your Password:
然後它会询问是否後允许常规用户(不是超级用户root)激活/停止这个新的ppp0设备。这可能是需要的,如果你家里有非root的成员需要访问Internet的话:
USERCTRL

Please enter 'yes' (two letters, lower-case.) 
if you want to allow normal user to start or stop DSL connection (default yes):
rp-pppoe包在/etc/ppp目录下有两个样例ip表防火墙脚本,文件名是firewall-standalone和firewall-masq。它们都非常基本并不涵盖将你的Linux机器设置成web服务器,DNS服务器或者mail服务器的规则。我都推荐选择NONE,然後使用第14章"Linux Firewalls Using iptables"中的一个基本脚本样例,或者附录II中的更复杂脚本
FIREWALLING
Please choose the firewall rules to use. Note that these rules are very basic. You are strongly
encouraged to use a more sophisticated firewall setup; however, these will provide basic security.
If you are running any servers on your machine, you must choose 'NONE' and set up firewalling
yourself. Otherwise, the firewall rules will deny access to all standard servers like Web, e-mail,
ftp, etc. If you are using SSH, the rules will block outgoing SSH connections which allocate a
privileged source port.

The firewall choices are:

0 - NONE: This script will not set any firewall rules. You are responsible
         for ensuring the security of your machine. You are STRONGLY
         recommended to use some kind of firewall rules.
1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
                for a LAN

Choose a type of firewall (0-2): 0
接着你会被询问是否在启动时激活连接。大部分人都会选择yes.
Start this connection at boot time

Do you want to start this connection at boot time?
Please enter no or yes (default no):yes
在退出前,你会获得一个摘要,包括你输入参数,那些配置文件将根据你的选择更新:
** Summary of what you entered **

Ethernet Interface: eth0

User name:          bigboy-login@isp
Activate-on-demand: No
DNS:                Do not adjust
Firewalling:        NONE 
User Control:       yes
Accept these settings and adjust configuration files (y/n)? y

Adjusting /etc/sysconfig/network-scripts/ifcfg-ppp0
Adjusting /etc/ppp/chap-secrets and /etc/ppp/pap-secrets
 (But first backing it up to /etc/ppp/chap-secrets.bak)
 (But first backing it up to /etc/ppp/pap-secrets.bak)
在最後,它会告诉你激活/关闭这个新的ppp0设备,以及获得设备状态的命令。
Congratulations, it should be all set up!

Type '/sbin/ifup ppp0' to bring up your xDSL link and '/sbin/ifdown ppp0'to bring it down.
Type '/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0' to see the link status.
注意: 这个例子中推荐使用adsl-status命令後面跟上PPPoE接口配置文件名字。不过这个命令会缺省显示ppp0设备的信息,因此在大部分系统中没有必要加上ifcfg-ppp0文件名。

在你完成安装配置rp-pppoe後,你应该可以通过DSL的DHCP连接到Internet上。

adsl-setup创建的一些重要文件

有三个adsl-setup脚本创建的文件你可能会感兴趣。第一个是ifcfg-pppo文件,包含了设备链路层的连接参数。

[root@bigboy network-scripts]# more ifcfg-ppp0
USERCTL=yes
BOOTPROTO=dialup
NAME=DSLppp0
DEVICE=ppp0
TYPE=xDSL
ONBOOT=yes
PIDFILE=/var/run/pppoe-adsl.pid
FIREWALL=NONE
PING=.
PPPOE_TIMEOUT=20
LCP_FAILURE=3
LCP_INTERVAL=80
CLAMPMSS=1412
CONNECT_POLL=6
CONNECT_TIMEOUT=60
DEFROUTE=yes
SYNCHRONOUS=no
ETH=eth0
PROVIDER=DSLppp0
USER= bigboy-login@isp
PEERDNS=no
[root@bigboy network-scripts]#

另外两个内容相同的文件时/etc/ppp/pap-secrets和/etc/ppp/chap-secrets,包含了登录ISP的用户名和密码:

[root@bigboy network-scripts]# more /etc/ppp/pap-secrets
# Secrets for authentication using PAP
# client        server  secret                  IP addresses
"bigboy-login@isp" *       "password"
[root@bigboy network-scripts]#

简单的故障诊断

你可以运行adsl-status命令来检查你的连接状态。在这个例子中,可以看到包已经装好但是设备没有被激活。

[root@bigboy tmp]# adsl-status
Note: You have enabled demand-connection; adsl-status may be inaccurate.
adsl-status: Link is attached to ppp0, but ppp0 is down
[root@bigboy tmp]#

激活後,设备即正常工作。

[root@bigboy tmp]# ifup ppp0
[root@bigboy tmp]# adsl-status
adsl-status: Link is up and running on interface ppp0
ppp0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1462 inet
...
...
[root@bigboy tmp]#

关于更多的故障诊断信息,你可以访问rp-pppoe的网站Roaring Penguin (www.roaringpenguin.com)。那里有一些如果避免VPN客户端问题的很好的提示。

为Cable Modem设置IP地址

Cable modems使用DHCP来获得IP地址,你可以据此配置你服务器的以太网卡。

怎样激活/关闭你的网卡

命令ifup和ifdown分别用于激活和停止一块网络设备。你必须有一个相关的ifcfg文件在/etc/sysconfig/network-scripts目录下来确保

这些命令工作。下面是一个eth0设备的例子:

[root@bigboy tmp]# ifdown eth0
[root@bigboy tmp]# ifup eth0

怎样查看你当前的路由表

netstat -nr命令会显示路由表的内容。网关为0.0.0.0的网络通常是直连到网络设备上的。因为到自己的直连设备上是不需要网关的,所

以0.0.0.0的网关地址是正常的。目的地址是0.0.0.0的路由是你的缺省网关。

  • 在这个例子里,有两个网关,缺省的并且是到255.255.255.255通常是DHCP服务器添加的。在这个例子里bigbogy服务器也是一个DHCP服

务器。

[root@bigboy tmp]# netstat -nr

Kernel IP routing table
Destination     Gateway	Genmask		Flags MSS Window irtt Iface
255.255.255.255 0.0.0.0	255.255.255.255 UH    40  0      0    wlan0
192.168.1.0	 0.0.0.0	255.255.255.0 	U     40  0 	 0    wlan0
127.0.0.0 	 0.0.0.0 	255.0.0.0 	U     40  0 	 0    lo
0.0.0.0 	 192.168.1.1 	0.0.0.0         UG    40  0 	 0    wlan0
[root@bigboy tmp]#
  • 在这个例子里,有多个网关负责把数据从不同网络设备上传输到不同的目的地。
[root@bigboy tmp]# netstat -nr

Kernel IP routing table 
Destination	Gateway		Genmask		Flags MSS Window irtt Iface
172.16.68.64 	172.16.69.193 	255.255.255.224 UG    40  0 	 0    eth1
172.16.11.96 	172.16.69.193 	255.255.255.224 UG    40  0	 0    eth1
172.16.68.32 	172.16.69.193 	255.255.255.224 UG    40  0	 0    eth1
172.16.67.0 	172.16.67.135 	255.255.255.224 UG    40  0	 0    eth0
172.16.69.192 	0.0.0.0         255.255.255.192 U     40  0	 0    eth1
172.16.67.128 	0.0.0.0         255.255.255.128 U     40  0	 0    eth0
172.160.0 	172.16.67.135 	255.255.0.0 	UG    40  0	 0    eth0
172.16.0.0 	172.16.67.131 	255.240.0.0 	UG    40  0	 0    eth0
127.0.0.0 	0.0.0.0 	255.0.0.0 	U     40  0	 0    lo
0.0.0.0        172.16.69.193 	0.0.0.0 	UG    40  0	 0    eth1
[root@bigboy tmp]#

怎样改变你的缺省网关

你的服务器需要有一个单一缺省网关。配置了DHCP的网卡会自动由DHCP服务器分配一个缺省网关,但是配置了静态IP地址的网卡就需要手工配置缺省网关。这可以通过一个简单的命令实现。这个例子中缺省路由使用一个新安装的无线网卡wlan0,而大部分PC都是使用标准的以太网卡eth0。

[root@bigboy tmp]# route add default gw 192.168.1.1 wlan0

在这个例子中,请确保IP地址为192.168.1.1的路由/防火墙和wlan0设备在一个网络中!

完成后,你需要更新你的/etc/sysconfig/network文件来反映这个变化。这个文件会在每次Linux启动时配置你的缺省网关。

NETWORKING=yes
HOSTNAME=bigboy
GATEWAY=192.168.1.1

注意:在以Debian为基础的系统中,缺省网关是固定定义在文件/etc/network/interfaces中。请参阅后面章节"Debian / Ubuntu的网络配置" 以了解更多详情。

有些人可能不愿意修改特定的网络文件,而只是把route add命令添加到/etc/rc.d/rc.local脚本文件中,这样每次启动时就会自动执行了。

也可以在/etc/sysconfig/network-scripts目录下网卡配置文件中定义缺省网关,但这样是有风险的,如果你有多块网卡可能导致定义多个缺省网关。这样会导致连接问题。如果其中一个缺省网关没有到目的地的路由,所有的包都会丢失。而且防火墙会阻挡带有不规则序号和不明来源的包,这也会妨碍你的数据流。

怎样配置两个网关

一些网络可能拥有多个路由器/防火墙提供连接。这里有一个典型的情况:

  • 你有一个路由可以访问Internet,你愿意用它来做你的缺省网关(参看前面的缺省网关样例)
  • 有还有另一个路由用来访问你的公司网络,公司网络的IP范围是10.0.0.0到10.255.255.255。让我们假定这个路由的IP地址是192.168.1.254。

这个例子中的Linux机器是使用wlan0提供Internet连接的。你可能是使用eth0设备,请据此调整你的操作。

有多种方法可以添加这个新的路由。

添加临时的静态路由

route add命令可以用来添加一条新路由到你的服务器,直到下一次重启。这个方法的优势是在所有版本的Linux中都通用,而且在man里面都有详细的说明。在我们的例子里,10.0.0.0网络前面有-net参数,子网掩码和网关前面也分别有netmask和gw参数。

[root@bigboy tmp]# route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0

如果你想为一个单独的服务器添加路由,那么应该使用-host参数,而且不设子网掩码值(route命令会自动知道子网掩码是255.255.255.255)。这里是为机器10.0.0.1添加路由的例子。

[root@bigboy tmp]# route add -host 10.0.0.1 gw 192.168.1.254 wlan0

如果想要这个变化在重启后也能持续,一个通用的方法是把route add命令添加到/etc/rc.d/rc.local文件中,它会始终在启动过程结束前运行。

添加永久静态路由

在Fedora Linux中,永久静态路由是添加在/etc/sysconfig/network-scripts目录下对应设备文件中的。文件名的格式是route-网络设备名称,所以wlan0设备对应的文件名是route-wlan0

该文件的格式很直观,第一行是目标网络加上单词via和网关的IP地址。在我们的例子中,要设置一个到通过网关192.168.1.254 到网络10.0.0.0的路由,子网掩码是255.0.0.0(一个头8位都是1的掩码),我们像这样设置/etc/sysconfig/network-scripts/route-wlan0文件:

#
# File /etc/sysconfig/network-scripts/route-wlan0
# 
10.0.0.0/8 via 192.168.1.254

注意: 文件名/etc/sysconfig/network-scripts/route-*非常重要。在route-后面添加了错误的设备后缀会导致下次启动后路由不能被正确添加。而且不会在屏幕上报告错误或者在/var/log/目录下生成任何日志文件。

你可以通过运行/etc/sysconfig/network-scripts/ifup-routes命令加设备名作为参数测试新建的文件。在下面例子里,我们先检查路由表查看没有到网络10.0.0.0的路由,然后执行ifup-routes命名把路由加上。

[root@bigboy tmp]# netstat -nr 
Kernel IP routing table 
Destination Gateway	   Genmask 	  Flags MSS Window irtt Iface
192.168.1.0 0.0.0.0       255.255.255.0  U     0   0      0    wlan0
169.254.0.0 0.0.0.0       255.255.0.0    U     0   0      0    wlan0
0.0.0.0     192.168.1.1   0.0.0.0        UG    0   0      0    wlan0
[root@bigboy tmp]# ./ifup-routes wlan0
[root@bigboy tmp]# netstat -nr
Kernel IP routing table
Destination Gateway 	   Genmask 	 Flags MSS Window irtt Iface
192.168.1.0 0.0.0.0       255.255.255.0 U     0   0      0    wlan0
169.254.0.0 0.0.0.0       255.255.0.0   U     0   0      0    wlan0
10.0.0.0    192.168.1.254 255.0.0.0     UG    0   0      0    wlan0
0.0.0.0     192.168.1.1   0.0.0.0       UG    0   0      0    wlan0
[root@bigboy tmp]#

注意: 在基于Debian的系统中,永久静态路由是配置在文件/etc/network/interfaces中的。请参阅后面章节"Debian / Ubuntu网络配置"以了解更多详情。

怎样删除路由

这里介绍了怎样删除之前章节添加的路由。

[root@bigboy tmp]# route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 wlan0

文件 /etc/sysconfig/network-scripts/route-wlan0也需要被更新,这样你重启服务器後路由才不会被重新加回来。删除如下的行:

10.0.0.0/8 via 192.168.1.254

改变网卡的速度和双工

没有比购买一个完全的Linux兼容网卡更好的Linux投资了。大部分的Linux厂商在他们网站上都有一份兼容的硬件列表:在开始连接你的机器到网络之前请仔细阅读它。如果你没有找到任何在本地的计算机商店可以买到的型号,那么同系列也是可以的。大多数网卡都能正常工作,但只有完全兼容的可为您提供无差错的,持续的吞吐量。

Linux缺省会在网卡连接到路由器上时自动协商判断网卡的速度和双工。配置一个路由器端口自动协商速度和双工往往是不足够的,因为不同的协议标准执行上常会有分歧。

实际上,网卡即使协商失败也会工作,但这通常会导致在网卡上使用ifconfig -a命令时看到许多冲突类型的错误和极低的性能。在故障排除时不要只把这类错误局限于协商失败;问题也可能是坏的网卡,路由器端口,或者网线导致的。

使用mii-tool

mii-tool是一个原始的设置网卡速度和双工的Linux工具。它以注定会被抛弃,而由新的ethtool命令取而代之,但是很多老的网卡只支持mii-toll,所以你还需要知道它。不带任何参数的执行命令会产生一个简要的状态报告,如下面样例所示,对于不支持的网卡将提示Operation not supported信息。mii-tool不兼容的网卡往往仍然可以工作,但是你需要根据制造商的指南来设定速度和双工,而不能自动协商。

[root@bigboy tmp]# mii-tool
SIOCGMIIPHY on 'eth0' failed: Operation not supported
eth1: 100 Mbit, half duplex, link ok
[root@bigboy tmp]#

通过-v参数使用verbose模式,你可以获得更多更详细的信息。在这个例子中,协商正常,网卡选择100Mbps,全双工模式(FD)。

[root@bigboy tmp]# mii-tool -v
eth1: negotiated 100baseTx-FD, link ok
  product info: vendor 00:10:18, model 33 rev 2
  basic mode: autonegotiation enabled
  basic status: autonegotiation complete, link ok
  capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
  advertising: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
  link partner: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control 
[root@bigboy tmp]#

用mii-tool设置网卡的速度参数

你可以通过-F参数强行指定网卡的速度和双工,选项包括:100baseTx-FD,100baseTx-HD,10baseT-FD,或者10baseT-HD。记住,你可能丢失网络连接如果你网卡的强制速度/双工模式与路由器不匹配的话:

[root@bigboy tmp]# mii-tool -F 100baseTx-FD eth0

不幸的是,除了把这个命令放到/etc/rc.local文件中让它在每次启动结束前执行,或者创建你自己的启动脚本如果你需要它更早被执行,我们没有办法让它在重启后永久生效。如何创建你自己的启动脚本在第7章涵盖, "The Linux Boot Process"。

使用ethtool

ethtool命令被指定在不久的将来替代mii-tool,目的是支持新的网卡。

用设备名称作为参数,命令可以提供其状态。这里我们可以看到设备eth0没有设置自动协商并设定为100 Mbps速度,全双工。在输出的顶端列出了其所支持的模式。

[root@bigboy tmp]# ethtool eth0
Settings for eth0:
       Supported ports: [ TP MII ]
       Supported link modes:   10baseT/Half 10baseT/Full
                               100baseT/Half 100baseT/Full
       Supports auto-negotiation: Yes
       Advertised link modes:  10baseT/Half 10baseT/Full
                               100baseT/Half 100baseT/Full
       Advertised auto-negotiation: No
       Speed: 100Mb/s
       Duplex: Full
       Port: MII
       PHYAD: 1
       Transceiver: internal
       Auto-negotiation: off
       Supports Wake-on: g
       Wake-on: g
       Current message level: 0x00000007 (7)
       Link detected: yes
[root@bigboy tmp]#

用ethtool设置网卡的速度参数

不同于mii-tool,ethtool可以通过设定设备配置脚本里的ETHTOOL_OPTS变量而永久生效。在下面例子里,设定了在下次重启时设置100 Mbps,全双工和不自动协商:

#
# File: /etc/sysconfig/network-scripts/ifcfg-eth0
# 
DEVICE=eth0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
BOOTPROTO=static
ONBOOT=yes
ETHTOOL_OPTS="speed 100 duplex full autoneg off"

你可以通过ifup和ifdown命令关闭和再激活设备,来测试这些参数。这些设定也可以通过在命令行中使用-s参数改变,后面跟上设备名称和相关配置参数。

[root@bigboy tmp]# ethtool -s eth1 speed 100 duplex full autoneg off
[root@bigboy tmp]#

Linux的man命令提供了ethtool选项的更详细说明,但你可以通过只输入ethtool命令获得一个快速的摘要。

[root@bigboy tmp]# ethtool
...
...
        ethtool -s DEVNAME \
                [ speed 10|100|1000 ] \
                [ duplex half|full ] \
                [ port tp|aui|bnc|mii|fibre ] \
...
...
[root@bigboy tmp]#

关于双工设置

缺省地,Linux网卡会跟交换机协商速度和双工设定。这通过交换叫做FLP(Fast Link Pules)的电子信号实现。当速度和双工被强制指定的时FLP就不会发送。当网卡使用自动协商模式而且检测到一个健康的,可用的连接时却没有收到FLP信号,它会报错并把自己的双工设为半双工模式,有时它也会设置速度到低一级的值。因此,这样可能交换机端口是强制100 Mbps全双工,但是自动协商的网卡把自己设为100Mbps半双工,结果产生错误。同样情况也可能发生在交换机上,如果交换机端口设为自动协商而服务器网卡强制100 Mbps全双工。最好是要么强制交换机端口和网卡都是自动协商,要么强制设定相同的速度和双工值。

怎样简单的将您的Linux服务器变成一个简单的路由器

由路由器/防火墙设备为一个小型办公室或家庭网络提供基本的互联网连接正日益变得可以承受。但是当预算紧张时,你可能会认真地考虑修改现有的Linux服务器来做完成这项任务。

详细介绍如何配置Linux防火墙的安全是在第14章涵盖, "Linux Firewalls Using iptables",但你需要了解如何激活路由通过防火墙,然后它才可以成为一个正常运作的网络设备。

配置IP转发

要想要你的Linux服务器成为路由器,你必须启用包转发(packet forwarding)。简单说包转发就是让数据包通过Linux机器从一个网络传输到另一个网络。找到文件/etc/sysctl.conf中的Linux内核参数net.ipv4.ip_forward,通过删除关于包转发相关行前的"#"来激活它。

Before:

# Disables packet forwarding
net.ipv4.ip_forward=0

After:

# Enables packet forwarding
net.ipv4.ip_forward=1

只有你重新启动的时候包转发才会启用,那时Linux将在特殊的基于RAM内存的/proc文件系统的子目录中创建一个文件。如果要立即激活这个功能,你必须通过sysctl命令加上-p参数来强迫Linux读取code>/etc/sysctl.conf</code>文件。下面是如何操作的:

[root@bigboy tmp] sysctl -p
sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
kernel.sysrq = 0
kernel.core_uses_pid = 1
[root@bigboy tmp]#

详情请参阅附录I以获得更多调整内核参数的信息,就调整内核参数。

配置ARP代理

如果服务器需要在同一个网络里把一个包传送到另一个设备上,它会在网络上发一个ARP(Address Resolution Protocol)请求来询问目的设备的MAC地址。

如果同一服务器需要把一个包传送到远程网络的另一个设备上,过程就不一样了。服务器首先查看路由表来寻找网络上的最佳路由器的IP地址,由它把包传送的目的地。服务器随后发送一个ARP请求来询问匹配路由器IP地址的MAC地址。然后通过路由器的MAC地址把包和远程服务器的IP地址发送给路由器。

如果在网络上没有合适的路由器,服务器会发送一个ARP请求来询问远程服务器的MAC地址。一些配置过的路由器可以回答这些关于远程网络的ARP请求,这个功能就叫做ARP代理。这种做法有一些缺点。其中一个最普遍的问题是在网络上的两个路由器都配置了ARP代理。在这种情况下,它们都可能会回答本地服务器关于远程网络MAC地址的ARP请求。如果其中一个路由器有远程服务器的不正确路由表记录,那么到远程服务器的传输就有偶尔丢失的风险。换句话说你失去了对路由的控制。

注意: 因为这个和其他一些原因,一般在路由器上配置ARP代理不是一个好的选择。比较好的做法是始终在服务器上配置一个缺省网关,而对缺省网关可能不知道的网络则使用通过其他路由器的单独路由记录。

有些类型的桥接模式防火墙,必须要ARP代理来正常运作。这些设备一般作为串行链的一部分,把一个局域网上的多个网络交换机连接在一起,用来保护局域网上的某一部分不受其他部分的流量的影响。这种防火墙一般不配置局域网中的IP地址,看起来就像一段智能的电缆在有选择的屏蔽数据包。

如果你需要启动Linux服务器上的ARP代理,/proc文件系统就再次发挥作用了。ARP代理是由/proc/sys/net/ipv4/conf/目录下的文件控制的。这个目录下有服务器中每块运转网卡的对应子目录。每个子目录下都有个proxy_arp文件。如各这个文件中的值是0,那么这个接口上的ARP代理就是被禁用的;如果这个值是1,那么就是启用的。

你可以使用附录II提到的/etc/sysctl.conf文件来激活或禁用ARP代理。下面是激活ARP代理的例子,首先是对所有接口的,其次是对eth0wlan0接口的。

#
# File: /etc/sysctl.conf
# 

# Enables Proxy ARP on all interfaces
net/ipv4/conf/all/proxy_arp = 1

# Enables Proxy ARP on interfaces eth1 and wlan0
net/ipv4/conf/eth1/proxy_arp = 1
net/ipv4/conf/wlan0/proxy_arp = 1

你可以使用sysctl命令激活这些设置。

[root@bigboy tmp] sysctl -p

配置你的/etc/hosts文件

/etc/hosts文件只是一个IP地址和对应服务器名的列表。服务器一般会在查询DNS前检查这个文件。如果找到了有对应IP地址的名称,那么就根本不会去查询DNS。不幸的是,如果主机的IP地址变化了,你也必须更新这个文件。这对单机来说不是什么大问题,但是如果是更新整个公司的话就很艰巨了。为了易于管理,通常是在文件只放loopback接口和本机机器名的记录,然后使用集中的DNS服务器来处理剩下的部分。有时你可能不能控制DNS服务器,在这种情况下,向/etc/hosts文件中添加一条记录就简单多了,直到其加入集中的DNS中。

192.168.1.101 smallfry

在上面的例子中,服务器smallfry的IP地址是192.168.1.101。你可以在ping,telnet或者其他网络相关程序中使用smallfry来代替192.168.1.101。下面的例子是使用ping命令查看smallfry是不是在网络上存在:

[root@bigboy tmp]# ping smallfry
PING zero (192.168.1.101) 56(84) bytes of data.
64 bytes from smallfry (192.168.1.101): icmp_seq=0 ttl=64 time=0.197 ms 
64 bytes from smallfry (192.168.1.101): icmp_seq=1 ttl=64 time=0.047 ms

--- smallfry ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2017ms rtt
min/avg/max/mdev = 0.034/0.092/0.197/0.074 ms, pipe 2
[root@bigboy tmp]#

你也可以在行后面添加别名,把其他名字和你的服务器关联起来。这里我们设置smallfry也可以用tinylittleguy访问。

192.168.1.101 smallfry tiny littleguy

你永远不要在文件中重复使用同一个IP地址,因为Linux只会使用它找到的第一个记录值。

192.168.1.101 smallfry  # (Wrong)
192.168.1.101 tiny      # (Wrong)
192.168.1.101 littleguy # (Wrong)

localhost里面的loopback接口

通常/etc/hosts里面的第一条记录会定义服务器虚拟loopback接口的IP地址。它通常映射到名称localhost.localdomain(代替服务器自己的通用名称)和localhost(短别名)。默认情况下,Fedora会把主机名插到127.0.0.1和localhost之间:

127.0.0.1 bigboy localhost.localdomain localhost

当服务器连接到互联网上的时候,第一条记录的127.0.0.1后面应该是服务器的FQDN名称(fully qualified domain name)。例如,bigboy.mysite.com

127.0.0.1 bigboy.my-site.com localhost.localdomain localhost

有些程序例如Sendmail非常敏感,如果觉得检测到了不正确的FQDN名称,它会缺省使用localhost.localdomain联络网络上的其他服务器。这会导致混淆,因为其他服务器也认为自己是localhost.localdomain.

注意: 你必须始终有一条把localhost和localhost。localdomain映射到127.0.0.1上的记录,以使Linux正常地安全地工作。

Debian / Ubuntu的网络配置

这章涉及的大部分Fedora / Redhat命令和配置文件也可一个用于基于Debian的操作系统,但也有一些关键的差别。

/etc/network/interfaces文件

主网络配置文件是/etc/network/interfaces ,所有网络接口相关的参数都是在这里定义的。文件划分成多个小节:

auto小节

auto小节定义了接口是不是要在系统启动时自动初始化。

mapping小节

本小节根据脚本的输出结果为接口映射配置参数。例如,在启动时,可以弹出一个脚本询问你的笔记本Linux系统是在家使用还是在单位使用,预设的映射语句通过答案来配置相应的IP地址。

缺省的,当假定接口只有一个目的时使用简单得多的热插拔系统。典型的热插拔配置就是把每个物理接口分配一个逻辑接口名称(别名)。

mapping hotplug
        script grep
        map eth0 eth0
        map eth1

在这个例子里,接口eth0被专门给与了逻辑名称eth0,逻辑名称eth1也是一样的。

iface小节

iface小节定义的逻辑接口的特性。通常小节的第一行是以单词iface开头,跟着接口的逻辑名称,使用的协议,最后是使用的寻址模式,例如DHCP或者静态。协议关键词包括对应常规TCP/IP协议的inet,IPv6的inet6,Novell旧的IPX协议的ipx,和loopback地址的loopback。

小节后面的行定义了协议的特性,例如地址,子网掩码,和缺省网关。在这个例子中,eth1接口被赋予IP地址216.10.119.240/27,而eth0接口使用DHCP。

# The primary network interface
auto eth1
iface eth1 inet static
        address 216.10.119.240
        netmask 255.255.255.224
        network 216.10.119.224
        broadcast 216.10.119.255
        gateway 216.10.119.241
        dns-nameservers 216.10.119.241

# The secondary network interface
auto eth0
iface eth0 inet dhcp

注意: 当使用静态IP地址时,通常也要定义一个缺省网关。记得把网关定义和对应路由器IP地址放在正确的小节。

创建接口别名

一旦主接口被定义了,在/etc/network/interfaces文件中定义IP别名就容易了。只需要一个修改过的主接口的iface小节副本。第一行要加上冒号和子接口编号,而且后面只需要跟上子网掩码和新的IP地址。在这个例子中接口eth1:1的IP地址是216.10.119.239。

auto eth1:1
iface eth1:1 inet static
       address 216.10.119.239
       netmask 255.255.255.224

添加永久静态路由

/etc/network/interfaces文件的恰当iface小节中添加up选项,可以让你在制定接口被激活时有选择性的用ifup执行命令。这在添加永久静态路由是很有用。

在这个例子中,添加了一个通过路由器216.10.119.225到网络10.0.0.0/8的路由。记住,up选项和此命令必须在小组的同一行。

# The primary network interface
auto eth1
iface eth1 inet static
        ...
        ...
        ...
        up route add -net 10.0.0.0 netmask 255.0.0.0 gw 216.10.119.225 eth1

一个完整的/etc/network/interfaces文件

现在我们可以用根据之前讨论的例子构造一个完整的文件。就像在Fedora中一样,可用ifup和ifdown命令激活接口。

# 
# Debian / Ubuntu
# 

# 
# File: /etc/network/interfaces
# 

# The loopback network interface
auto lo
iface lo inet loopback

# This is a list of hotpluggable network interfaces.
# They will be activated automatically by the hotplug subsystem.
mapping hotplug
        script grep
        map eth0 eth0
        map eth1 eth1

# The primary network interface
auto eth1
iface eth1 inet static
        address 216.10.119.240
        netmask 255.255.255.224
        network 216.10.119.224
        broadcast 216.10.119.255
        gateway 216.10.119.241
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 216.10.119.241
        wireless-key 98d126d5ac
        wireless-essid schaaffe

        up route add -net 10.0.0.0 netmask 255.0.0.0 gw 216.10.119.225 eth1

auto eth1:1
iface eth1:1 inet static
        address 216.10.119.239
        netmask 255.255.255.224

# The secondary network interface
auto eth0
iface eth0 inet dhcp

关于更多关于/etc/network/interfaces文件的信息,只要在命令行执行man interfaces就可以了。

结论

你可以想象,配置Linux的网络只是提供互联网接入到您的服务器第一步。实际操作中,总是存在一些事情可能出错,可完全脱离你的控制。好的系统管理员了解能够找出这些问题可能原因的所需工具,使他们能够找到相应类型的帮助以解决它。未来两章向您展示如何在事情出错时自信地调试您的网络和Linux服务器应用。对你的公司和事业来说,你查明和纠正这些问题的能力将会被证明是非常宝贵的。