linux interfaces配置文件详解

转自:http://blog.csdn.net/mountzf/article/details/52035499

===================================================

配置文件基本格式

一个基本的配置大概是下面这个样子:

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address 192.168.0.42
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
  上面的配置中, 
  第1行跟第5行说明lo接口跟eth0接口会在系统启动时被自动配置; 
  好像不同的接口之间配置部分必须留有一个空格,比如第3行的空格 
  第2行将lo接口设置为一个本地回环(loopback)地址; 
  第6行指出eth0接口具有一个静态的(static)IP配置; 
  第7行-第11行分别设置eth0接口的ip、网络号、掩码、广播地址和网关。


复杂一点

  再来看一个更复杂点的
auto eth0
iface eth0 inet static
    address 192.168.1.42
    network 192.168.1.0
    netmask 255.255.255.128
    broadcast 192.168.1.0
    up route add -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2
    up route add default gw 192.168.1.200
    down route del default gw 192.168.1.200
    down route del -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2
  这次,有了一个复杂一些的掩码,和一个比较奇怪的广播地址。还有就是增加的接口启用、禁用时的路由设置; 
  第7行和8行配置的左右是在接口启用的时候,添加一条静态路由和一个缺省路由; 
  第9行和10行会在接口禁用的时候,删掉这两条路由配置。 
  至于配置路由的写法,仔细看,它就是route命令嘛。


一个物理网卡上多个接口

  继续,下面是一个物理网卡上多个接口的配置方法:
auto eth0 eth0:1
iface eth0 inet static
    address 192.168.0.100
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
iface eth0:1 inet static
    address 192.168.0.200
    network 192.168.0.0
    netmask 255.255.255.0
  8行到11行在eth0上配置了另外一个地址,这种配置方法在配置一块网卡多个地址的时候很常见:有几个地址就配置几个接口。冒号后面的数字可以随便写的,只要几个配置的名字不重复就可以。 
  下面是pre-up和post-down命令时间。这是一组命令(pre-up、up、post-up、pre-down、down、post-down),分别定义在对应的时刻需要执行的命令。

auto eth0
iface eth0 inet dhcp
    pre-up [ -f /etc/local-network-ok ]
  第3行会在激活eth0之前检查/etc/local-network-ok文件是否存在,如果不存在,则不会激活eth0。


再更进一步的例子

  再更进一步的例子:
auto eth0 eth1
iface eth0 inet static
    address 192.168.42.1
    netmask 255.255.255.0
    pre-up /path/to/check-mac-address.sh eth0 11:22:33:44:55:66
    pre-up /usr/local/sbin/enable-masq
iface eth1 inet dhcp
    pre-up /path/to/check-mac-address.sh eth1 AA:BB:CC:DD:EE:FF
    pre-up /usr/local/sbin/firewall
  第5行和第8行中,check-mac-address.sh放在/usr/share/doc/ifupdown/examples/目录 中,使用的时候需要给它加上可执行权限。这两行命令会检测两块网卡的MAC地址是否为11:22:33:44:55:66和 AA:BB:CC:DD:EE:FF,如果正确,则启用网卡。如果MAC地址错误,就不会启用这两块网卡。 
  第6行和第9行是假定在这两块网卡上分别执行的命令,你可以把它们替换成你想要的任何玩意 :) 
  手册上说,这种方法主要是用来检测两块网卡的MAC地址交换(If their MAC addresses get swapped),其实就是两块网卡名互换了,这种情况在debian系统上再常见不过了,主要是因为内核识别网卡的顺序发生了变化。这个问题可以用下面 的这种方法来避免。
auto eth0 eth1
mapping eth0 eth1
    script /path/to/get-mac-address.sh
    map 11:22:33:44:55:66 lan
    map AA:BB:CC:DD:EE:FF internet
iface lan inet static
    address 192.168.42.1
    netmask 255.255.255.0
    pre-up /usr/local/sbin/enable-masq $IFACE
iface internet inet dhcp
    pre-up /usr/local/sbin/firewall $IFACE
  第3行中的get-mac-address.sh也在/usr/share/doc/ifupdown/examples/目录里,也同样要加可执行权限。这个脚本的作用,就是获得每块网卡的MAC地址。 
  这段配置首先配置了两个逻辑接口(这个名词的定义请参见debian参考手册 http://www.debian.org/doc/manuals/reference/ch-gateway.zh-cn.html)lan和internet,然后根据网卡的MAC地址,将逻辑接口映射(mapped)到物理接口上去。 
  再来看下面这段配置:
auto eth0
iface eth0 inet manual
    up ifconfig $IFACE 0.0.0.0 up
    up /usr/local/bin/myconfigscript
    down ifconfig $IFACE down

  这段配置只是启用一个网卡,但是ifupdown不对这个网卡设置任何ip,而是由外部程序来设置ip。 

  最后一段配置,这段配置启用了网卡的混杂模式,用来当监听接口。
auto eth0
iface eth0 inet manual
    up ifconfig $IFACE 0.0.0.0 up
    up ip link set $IFACE promisc on
    down ip link set $IFACE promisc off
    down ifconfig $IFACE down
  好了,interfaces中对于以太网卡的配置基本上介绍完了 

添加dns服务器

还可以在interfaces文件中添加dns服务器

# 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
auto lo
iface lo inet loopback


# The primary network interface
auto eth0
iface eth0 inet static
      address 10.112.18.106
      network 10.112.18.0
      netmask 255.255.255.0
      broadcast 10.112.18.255
      gateway 10.112.18.254
      dns-nameservers 10.112.18.1

就是最后一行dns-nameservers,可以添加多个,用空格分开。


附网卡设置相关命令:

查看网卡信息: ifconfigsu

设定一个网卡IP:ifconfig eth0 192.168.1.10 netmask 255.255.255.0
重启网卡使设定生效:sudo /etc/init.d/networking restart
更改MAC地址:ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx

查看路由相关信息:route -n

ifconfig 网卡名 <IP地址> netmask <子网掩码>  此命令会立即生效,但不会修改配置文件。

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值