在Debian中网卡的设置

    在Debian中网卡的设置可以通过/etc/network/interfaces文件来进行,具体可分为三种不同的配置方式:DHCP自动获取、静态分配IP地址和PPPoE宽带拨号。

    具体设置如下: 在进行配置之前,首先进入/etc/network目录中,用nano命令编辑interfaces文件:

     网卡通过DHCP自动获取IP地址
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

#
# The loopback network interface(配置环回口)

# 开机自动激lo接口
auto lo
# 配置lo接口为环回口
iface lo inet loopback

#
# The primary network interface (配置主网络接口)

#开机自动激活eth0接口
auto eth0
#配置eth0接口为DHCP自动获取
iface eth0 inet dhcp

网卡静态分配IP地址

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

#
# The loopback network interface(配置环回口)

# 开机自动激lo接口
auto lo
# 配置lo接口为环回口
iface lo inet loopback

#
# The primary network interface (配置主网络接口)

#开机自动激活eth0接口
auto eth0
#配置eth0接口为静态设置IP地址
iface eth0 inet static
address 10.16.3.99
netmask 255.255.255.0
network 10.16.3.0
broadcast 10.16.3.255
gateway 10.16.3.1

#单网卡配置多个ip,设置第二个ip地址

auto eth0:1

iface eth0:1 inet static
address x.x.x.x
netmask 255.255.255.0
network 10.16.3.0
broadcast 10.16.3.255
gateway 10.16.3.1

# dns-* options are implemented by the resolvconf package, if installed(DNS设置)
dns-nameservers 61.153.177.196 61.153.177.197
dns-search feelnet.org

配置好后推出编辑/etc/resolv.conf,配置DNS,加入以下信息:
nameserver 219.146.0.130

基本文件格式
Debian的网络配置文件在目录/etc/network中,其中的interfaces文件中保存了每一个网络设备在启动时的属性。下面是一个很简单的配置文件:

例子 1. 简单的配置文件

auto lo eth0 ①
iface lo inet loopback ②
iface eth0 inet dhcp ③
iface eth1 inet static ④
address 10.1.133.165
netmask 255.255.255.0
gateway 10.1.133.1
① 表示系统中的lo和eth0两个网络设备在系统启动网络时自动启动。
② 表示网络设备lo使用TCP/IP网络并且是一个loopback设备,如果是IPV6网络则使用"inet6",IPX网络使用"ipx"。
③ 表示网络设备eth0使用TCP/IP网络,同时使用DHCP自动获取IP地址。
④ 表示网络设备eth1使用TCP/IP网络,并且是占用固定的IP
10.1.33.165,子网掩码是255.255.255.0,网关是10.1.133.1。

上面的这种配置方式,可以使用于大多数的情况,但在一些特殊的情况下,就需要一些更为灵活的手段来配置网络。

通过PING配置网络

Linux在处理PCMCIA卡的时候有比较好的方式,可以在PCMICA卡插入时通过一个配置脚本来确定网络地址。但是,笔记本上的网卡是笔记本自带的,并非PCMCIA卡,由于经常需要奔波于办公室、实验室和家之间,就经常需要修改网络地址。如果我去的每一个地方都安装了DHCP,那么我就可以把 eth0设定成为DHCP的方式,然而我的情况却是:在家可以使用DHCP,在办公室和实验室都要使用固定地址。

为了解决这个问题,我们可以使用一种mapping机制,这种方法的基本原理是通过运行一个程序来确定目前所处的环境,并为这个环境选择一套配置。我现在使用的就是通过ping一个网络的网关来确定当前网卡究竟连接在哪个网络上,然后再选择这个网络的配置。

首先,在/usr/share/doc/ifupdown/examples中有一个文件ping-places.sh,把它复制到/etc/network目录中,然后chmod
+x /etc/network/ping-places.sh。下面就是编辑/etc/network/interfaces文件,下面是一个例子:

例子 2.

mapping eth0 ①
script /etc/network/ping-places.sh
map 192.168.0.107/24 192.168.0.1 home
map 10.1.133.165/24 10.1.133.1 office
map 10.1.0.107/24 10.1.0.1 lab
iface home inet dhcp ②
iface office inet static ③
address 10.1.133.165
netmask 255.255.255.0
gateway 10.1.133.1
up cp /etc/resolv.conf.school /etc/resolv.conf ④
iface lab inet static
address 10.1.0.107
netmask 255.255.255.0
gateway 10.1.0.1
up cp /etc/resolv.conf.school /etc/resolv.conf
① 表示对于网络设备调用脚本/etc/network/ping-places.sh,如果能够用地址192.168.0.107/24
ping通地址192.168.0.1,则将eth0映射为设备home,即启动home的配置。后面的office和lab与其类似。
② 表示虚拟设备home使用DHCP分配的地址。
③ 表示虚拟设备office使用固定地址。
④ 表示在启动这个网络设备后还要执行cp命令,从而指定一个域名解析方法。除了up以外,还有pre-up、down和post-down可以用来指定在启动或停止网络设备前后执行的命令。

在/usr/share/doc/ifupdown/examples中有一些配置网络的例子和需要的脚本。
posted @ 2012-05-21 16:33  microsoft_kk  阅读(22301)  评论(1编辑  收藏  举报