Linux 配置interfaces进行网卡配置

  在Linux中,如果你的Linux系统是有界面的可以用vim /etc/sysconfig/network-scripts/ifcfg-eth0 命令,打开文件编辑界面,其中ifcfg-eth0表示配置eth0这个网卡,假如其他网卡,则使用ifcfg-eth1、ifcfg-eth2诸如此类的。

  如果你的Linux系统比较小,是没有界面的,就可以用本文的方法,直接配置interfaces配置网卡,先进入到根目录的etc/network文件夹,打开interfaces添加配置,配置格式如下:

基本的配置格式

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、网络号、掩码、广播地址和网关。

添加多个网卡的时候也安照上面的格式,在文本后面添加即可,注意添加多一个网卡不可以设置同一个网段的,这里网口1设置了192.168.0.42,另外的网口就要设置192.168.x.*(x代表不同网段)

复杂一点的配置格式

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命令嘛。

在一个物理网卡上配置多个网口(多个IP)的配置格式

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服务可以在里面多进行一步下面的配置:

# 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服务,如果你要直接ping域名地址就需要开启dns服务
#如果配置好dns服务后,你使用 udhcpc -i eth0 指令,系统会重新给ech0分配个临时IP,这时候使用eth0就用分配的临时IP,要用会原来的IP则需要重启系统才行

最后附网卡设置相关命令 

  查看网卡信息: 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 <子网掩码>  此命令会立即生效,但不会修改配置文件。

原文地址:https://www.cnblogs.com/xingboy/p/14955700.html