在Debian Linux中如何将网络接口配置为网桥/网络交换机

时间:2019-11-20 08:53:19  来源:igfitidea点击:

Debian服务器上有一个ADSL端口和多个以太网端口。
如何将服务器配置成一个网络交换机。其他电脑通过以太网端口上网?

我们可以在Debian中,设置IPv4网桥。
使用brctl命令桥接网络连接。

安装bridge-utils软件包

需要安装bridge-utils软件包来配置Linux以太网桥。

# apt-get install bridge-utils

Linux网桥配置

在下面的示例中,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

Linux启动网络服务:

# service networking start

验证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

Linux如何显示Mac地址列表?

# brctl showmacs br0

Linux如何查看网桥stp信息?

# brctl showstp br0

关于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

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