Debian Linux:将网络接口配置为网桥/网络交换机

时间:2020-01-09 14:16:14  来源:igfitidea点击:

我的服务器有五个以太网端口和一个ADSL端口。
如何使用Debian Linux操作系统设置IPv4软件桥,以便其余五个端口充当网络交换机?

您需要使用brctl命令在Debian Linux下桥接网络连接。
这对于以下用途很有用:

  • 在多个设备之间共享您的Internet连接。
  • 无需购买专用网络交换机即可增加以太网插孔的容量。
  • 将Debian设置为访问点等等。

安装bridge-utils软件包

您需要安装一个名为bridge-utils的软件包来配置Linux以太网桥。

# apt-get install bridge-utils

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  bridge-utils
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 35.5 kB of archives.
After this operation, 145 kB of additional disk space will be used.
Get:1 http://mirror.anl.gov/debian/ wheezy/main bridge-utils i386 1.5-6 [35.5 kB]
Fetched 35.5 kB in 1s (21.9 kB/s)       
Selecting previously unselected package bridge-utils.
(Reading database ... 23737 files and directories currently installed.)
Unpacking bridge-utils (from .../bridge-utils_1.5-6_i386.deb) ...
Processing triggers for man-db ...
Setting up bridge-utils (1.5-6) ...

配置

在下面的示例中,eth0到eth4充当开关。
编辑文件"/etc/network/interfaces",输入:

# cp -v /etc/network/{interfaces,interfaces.bak}
# vi /etc/network/interfaces

要使网桥配置永久保存,请编辑此文件。
追加/修改如下:

# The loopback network interface
auto lo 
iface lo inet loopback
 
# Eth0 to Eth5 network switch
allow-hotplug eth0
iface eth0 inet manual
   pre-up   ifconfig $IFACE up
   pre-down ifconfig $IFACE down
 
allow-hotplug eth1
iface eth1 inet manual
   pre-up   ifconfig $IFACE up
   pre-down ifconfig $IFACE down
 
allow-hotplug eth2
iface eth2 inet manual
   pre-up   ifconfig $IFACE up 
   pre-down ifconfig $IFACE down
 
allow-hotplug eth3
iface eth3 inet manual
   pre-up   ifconfig $IFACE up
   pre-down ifconfig $IFACE down
 
allow-hotplug eth4
iface eth4 inet manual
   pre-up   ifconfig $IFACE up
   pre-down ifconfig $IFACE down
 
# Setup an IP address for our bridge 
auto br0
iface br0 inet static
  bridge_ports eth0 eth1 eth2 eth3 eth4
  address 192.168.1.253
  broadcast 192.169.1.255
  netmask 255.255.255.0

保存并关闭文件。

重新启动网络服务

要停止当前的网络配置,请输入:

# service networking stop

输出示例:

Deconfiguring network interfaces...done.

要激活br0网络接口,请输入:

# service networking start

输出示例:

Configuring network interfaces...
Waiting for br0 to get ready (MAXWAIT is 32 seconds).
done.

验证br0配置

执行以下命令:

# ip addr show

输出示例:

1: lo:  mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
    link/ether 00:00:24:cf:69:68 brd ff:ff:ff:ff:ff:ff
3: eth1:  mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether 00:05:b4:09:ee:9c brd ff:ff:ff:ff:ff:ff
4: eth2:  mtu 1500 qdisc pfifo_fast master br0 state DOWN qlen 1000
    link/ether 00:00:24:cf:69:69 brd ff:ff:ff:ff:ff:ff
5: eth3:  mtu 1500 qdisc pfifo_fast master br0 state DOWN qlen 1000
    link/ether 00:00:24:cf:69:6a brd ff:ff:ff:ff:ff:ff
6: eth4:  mtu 1500 qdisc pfifo_fast master br0 state DOWN qlen 1000
    link/ether 00:00:24:cf:69:6b brd ff:ff:ff:ff:ff:ff
7: wlan0:  mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:1d:73:bc:e4:6e brd ff:ff:ff:ff:ff:ff
8: br0:  mtu 1500 qdisc noqueue state UP 
    link/ether 00:00:24:cf:69:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.253/24 brd 192.169.1.255 scope global br0
    inet6 fe80::200:24ff:fecf:6968/64 scope link 
       valid_lft forever preferred_lft forever

您可以使用以下brctl命令查看以太网桥的所有当前实例:

# brctl show

输出示例:

bridge name	bridge id		STP enabled	interfaces
br0		8000.000024cf6968	no		eth0
							eth1
							eth2
							eth3
							eth4

如何显示Mac地址列表?

# brctl showmacs br0

如何查看网桥stp信息?

# brctl showstp br0

其他选择

要查看所有其他受支持的选项,请执行以下命令

$ man brctl

或者

$ brctl --help

输出示例:

Usage: brctl [commands]
commands:
	addbr     			add bridge
	delbr     			delete bridge
	addif     	 	add interface to bridge
	delif     	 	delete interface from bridge
	hairpin   	  {on|off}	turn hairpin on/off
	setageing 	 		set ageing time
	setbridgeprio	 		set bridge priority
	setfd     	 		set bridge forward delay
	sethello  	 		set hello time
	setmaxage 	 		set max message age
	setpathcost	  	set path cost
	setportprio	  	set port priority
	show      	[  ]		show a list of bridges
	showmacs  			show a list of mac addrs
	showstp   			show bridge stp info
	stp       	 {on|off}	turn stp on/off

关于DHCPD服务器的说明

您可能需要设置DHCPD服务器,以允许台式机,笔记本电脑和移动设备等客户端从服务器/交换机本身请求并获取IP地址和许多其他参数。
有关更多信息,请参见如何为您的网络设置ISC DHCP服务器。

关于Iptables的说明

数据流经所有接口,因此您只需要在一个接口上进行过滤。
使用Linux内核和iptables(NAT)打开数据包转发。
假设eth6或ppp0是到Internet的连接。
首先,在内核中打开IP转发:

# sysctl -w net.ipv4.ip_forward=1

接下来,使用以下命令:

/sbin/iptables -t nat -A POSTROUTING -o eth6 -j MASQUERADE
### ppp0 ###
/sbin/iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

或设置IP转发和伪装(NAT):

/sbin/iptables --table nat --append POSTROUTING --out-interface eth6 -j MASQUERADE
/sbin/iptables --append FORWARD --in-interface br0 -j ACCEPT

随时根据您的设置修改规则。