如何设置ubuntu和redhat网络接口

环境:
包括较为常用的ubuntu和redhat,二者在配置细节上可能不同,必要时将分别阐述。
OS:Ubuntu 12.04.1 LTS
内核:3.2.0-33-generic-pae #52-Ubuntu SMP Thu Oct 18 16:39:21 UTC 2012 i686 i686 i386 GNU/Linux
OS:Red Hat Enterprise Linux Server release 6.2 (Santiago)
内核:Linux blade4 3.7.1 #2 SMP Thu Dec 20 20:22:51 CST 2012 x86_64 x86_64 x86_64 GNU/Linux

一、网络接口(包含网桥)
1、手动添加、修改网络接口、网桥
启动、关闭网络接口(含网桥):

 
 
  1. ifconfig <接口> up

  2. ifconfig <接口> down

删除网桥:

 
 
  1. brctl delbr <网桥名>

显示网桥:

 
 
  1. brctl show


2、通过配置文件永久修改网络接口、网桥
1)ubuntu 设置网络接口
编辑网络接口、网桥等 sudo vim /etc/network/interfaces
重启网络 sudo /etc/init.d/networking restart
网卡eth0

 
 
  1. auto lo

  2. iface lo inet loopback

  3. auto eth0

  4. iface eth0 inet static

  5.    address 192.168.120.101

  6.    netmask 255.255.0.0

  7.    network 192.168.0.0

  8.    broadcast 192.168.255.255

  9.    gateway 192.168.120.254

  10.    dns-nameservers 8.8.8.8

网桥br0

 
 
  1. auto lo

  2. iface lo inet loopback

  3. auto eth0

  4. iface eth0 inet manual

  5. auto br0

  6. iface br0 inet static

  7.    address 192.168.120.101

  8.    netmask 255.255.0.0

  9.    gateway 192.168.120.254

  10.    bridge_ports eth0

  11.    bridge_stp off

  12.    bridge_fd 0

  13.    bridge_maxwait 0

  14.    dns-nameservers 8.8.8.8  

2) Redhat 设置网络接口

编辑网络接口、网桥,在目录/etc/sysconfig/network-scripts,每一个网络接口、网桥都有一个单独的配置文件,如ifcfg-eth0、ifcfg-lo、ifcfg-br0等。

重启网络/etc/rc.d/init.d/network restart

接口eth0
vim /etc/sysconfig/network-scripts/ifcfg-eth0

 
 
  1. DEVICE="eth0"

  2. HWADDR="8C:89:A5:9F:B5:A6"

  3. NM_CONTROLLED="no"

  4. ONBOOT="yes"

  5. BOOTPROTO=static

  6. IPADDR=192.168.120.101

  7. NETMASK=255.255.0.0

  8. GATEWAY=192.168.120.254  

网桥br0
假设eth0接在br0上,需要同时修改eth0、br0
vim /etc/sysconfig/network-scripts/ifcfg-eth0

 
 
  1. DEVICE="eth0"

  2. HWADDR="8C:89:A5:9F:B5:A6"

  3. NM_CONTROLLED="no"

  4. ONBOOT="yes"

  5. BRIDGE=br0

vim /etc/sysconfig/network-scripts/ifcfg-br0

 
 
  1. DEVICE="br0"

  2. TYPE=Bridge

  3. NM_CONTROLLED="no"

  4. ONBOOT="yes"

  5. BOOTPROTO="static"

  6. IPADDR="192.168.120.101"

  7. NETMASK="255.255.0.0"

  8. GATEWAY="192.168.120.254"


二、路由route
1、手动添加、修改

通过route 命令实现,机器重启或者网卡重启后路由将失效。

1)添加到主机路由

 
 
  1. route add -host 192.168.120.102 dev eth0

  2. route add -host 192.168.120.102 gw 192.168.120.254  

2)添加到网络的路由

 
 
  1. route add -net 192.168.120.0 netmask 255.255.255.0 dev eth0

  2. route add -net 192.168.120.0/24 dev eth0  

注意 -net后面的网络IP要和子网掩码netmask对应
3)添加默认网关(并不覆盖掉之前的默认网关)  

 
 
  1. route add default gw IP  

4)删除路由:  

 
 
  1. route del –host 192.168.120.102 dev eth0

  2. route del -net 192.168.120.0 netmask 255.255.255.0 dev eth0  


2、在linux下设置永久路由的方法:
1)在/etc/rc.local里添加
方法:

 
 
  1. route add -net 192.168.120.0/24 dev eth0

  2. route add -net 192.168.120.0/24 gw 192.168.120.254  

2)在/etc/sysconfig/network里添加到末尾
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
3)/etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.y

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值