桥接命令brctl的用法

虚拟机连网是头等大事,所以我们先解决这个问题。通过桥接可以实现联网,主要用到的命令有:brctl,ifconfig,tunctl,route

brctl是一个以太网桥接工具,常见的用法有:

桥接命令brctl的用法_第1张图片

如果发现无法删除网桥,那么可能是还没有将其关闭:

yao@twomoon:~$ sudo brctl delbr br0
bridge br0 is still up; can't delete it
yao@twomoon:~$ sudo ifconfig br0 down
yao@twomoon:~$ sudo brctl delbr br0

说了这么多,到这才是重点。首先,将物理网卡桥接:

yao@twomoon:~$ ifconfig -a |grep eth
eth0      Link encap:Ethernet  HWaddr 90:fb:a6:14:cd:42
yao@twomoon:~$ brctl addbr br0
yao@twomoon:~$ brctl addif br0 eth0
yao@twomoon:~$ ifconfig eth0 0.0.0.0
yao@twomoon:~$ ifconfig br0 192.168.1.51 up

这样又多了一个网络设备br0:

yao@twomoon:~$ LANG=C ifconfig -a |grep Ethernet
br0       Link encap:Ethernet  HWaddr 2a:24:d3:aa:99:e7
eth0      Link encap:Ethernet  HWaddr 90:fb:a6:14:cd:42

如果没有弄明白为什么,可以上网找找桥接的资料和brctl的用法。

当然不能每次开机后都要手工输入这么多命令。你可以将这些命令写成一个脚本,每次开机就执行这个脚本:

#!/bin/bash
# networking.sh
# change eth0 to your network interface(eg. eth1)
ifconfig lo 127.0.0.1 up
if [ ! -z "`ifconfig -a | grep br0`" ]; then
    ifconfig br0 down
    brctl delif br0 eth0
    brctl delbr br0
fi
brctl addbr br0
brctl addif br0 eth0
ifconfig eth0 0.0.0.0 up
ifconfig br0 192.168.1.51 up
route add default gw 192.168.1.1
exit 0

 

你可能感兴趣的:(桥接,brctl,虚拟机上网,运维,linux,脚本)