我是靠谱客的博主 玩命红酒,最近开发中收集的这篇文章主要介绍linux系统的网桥搭建,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  • 网络桥接的管理命令

brctl   # 桥接管理命令 brctl show ;brctl addbr br0 添加网桥;
show   # 显示
addbr   # 添加网桥
delbr   # 删除网桥
addif   # 添加网络桥接
delif  # 删除网络桥接

文件方式的网络桥接。

  • 可以在其真机中操作,操作前提是要将原本的 ifcfg-enp0s25,ifcfg-br0 转移阿到其他的地方,以备后期恢复
[root@foundation12 ~]# cd /etc/sysconfig/network-scripts/  在此目录下
[root@foundation12 network-scripts]# ls
ifcfg-br0       ifdown-ovs       ifup-eth     ifup-sit
ifcfg-enp0s25   ifdown-post      ifup-ib      ifup-Team
ifcfg-lo        ifdown-ppp       ifup-ippp    ifup-TeamPort
ifcfg-小米手机  ifdown-routes    ifup-ipv6    ifup-tunnel
ifdown          ifdown-sit       ifup-isdn    ifup-wireless
ifdown-bnep     ifdown-Team      ifup-ovs     init.ipv6-global
ifdown-eth      ifdown-TeamPort  ifup-plip    keys-小米手机
ifdown-ib       ifdown-tunnel    ifup-plusb   network-functions
ifdown-ippp     ifup             ifup-post    network-functions-ipv6
ifdown-ipv6     ifup-aliases     ifup-ppp
ifdown-isdn     ifup-bnep        ifup-routes
[root@foundation12 network-scripts]# mv ifcfg-br0 ifcfg-enp0s25   /opt      移动到其他地方
  • 在/etc/sysconfig/networl-scipts 目录下 操作新的网络桥接
[root@foundation12 network-scripts]# vim ifcfg-br0     设置真实主机的网卡
[root@foundation12 network-scripts]# ifconfig             查看真实物理网卡
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.12.250  netmask 255.255.255.0  broadcast 172.25.12.255
        inet6 fe80::221:ccff:fe6f:78a8  prefixlen 64  scopeid 0x20<link>
        ether 00:21:cc:6f:78:a8  txqueuelen 1000  (Ethernet)
        RX packets 106032  bytes 133746644 (127.5 MiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 45193  bytes 3062537 (2.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s25: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500             物理网卡
        ether 00:21:cc:6f:78:a8  txqueuelen 1000  (Ethernet)
        RX packets 149532  bytes 138685095 (132.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45246  bytes 3249632 (3.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf2600000-f2620000  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 850589  bytes 296471850 (282.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 850589  bytes 296471850 (282.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

privbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.25.250.250  netmask 255.255.255.0  broadcast 172.25.250.255
        ether 52:54:00:18:3d:3a  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:b1:87:41  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:d9:96:d6  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe00:c0a  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:00:0c:0a  txqueuelen 1000  (Ethernet)
        RX packets 167  bytes 29997 (29.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43833  bytes 3052031 (2.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe21:641b  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:21:64:1b  txqueuelen 1000  (Ethernet)
        RX packets 31  bytes 4118 (4.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58438  bytes 4125919 (3.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe67:d8a7  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:67:d8:a7  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 364 (364.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43808  bytes 3046200 (2.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.236  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::2d9f:805d:d4b5:f0d1  prefixlen 64  scopeid 0x20<link>
        ether 08:11:96:9a:1f:a4  txqueuelen 1000  (Ethernet)
        RX packets 28627  bytes 26833776 (25.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23792  bytes 8576662 (8.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@foundation12 network-scripts]# vim ifcfg-enp0s25       编写物理网卡。
[root@foundation12 network-scripts]# systemctl restart network     重启网络
[root@foundation12 network-scripts]# brctl show             查看网络信息
bridge name	bridge id		STP enabled	interfaces
br0		8000.0021cc6f78a8	no		enp0s25                      桥接搭建好
privbr0		8000.525400183d3a	yes		privbr0-nic 
virbr0		8000.525400b18741	yes		virbr0-nic
virbr1		8000.525400d996d6	yes		virbr1-nic
[root@foundation12 network-scripts]# cat ifcfg-br0                  
DEVICE=br0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.12
PREFIX=24
TYPE=Bridge
[root@foundation12 network-scripts]# cat ifcfg-enp0s25 
DEVICE=enp0s25
ONBOOT=yes
BOOTPROTO=none
BRIDGE=br0
 [root@foundation12 network-scripts]# rm  -rf  ifcfg-br0     
[root@foundation12 network-scripts]# rm  -rf ifcfg-enp0s25      删掉自己做的 
[root@foundation12 network-scripts]# mv   /opt/ifcfg-br0  .
[root@foundation12 network-scripts]# mv   /opt/ifcfg-enp0s25   .   将原本的和重新移动回来
[root@foundation12 network-scripts]# ls
ifcfg-br0       ifdown-ovs       ifup-eth     ifup-sit
ifcfg-enp0s25   ifdown-post      ifup-ib      ifup-Team

  • 网络接口的临时搭建
[root@localhost ~]# brctl show         查看网桥信息 
bridge name	bridge id		STP enabled	interfaces
[root@localhost ~]# brctl addbr br0     添加一块新的虚拟接口
[root@localhost ~]# brctl show     查看网桥信息  已经有了 br0 这个虚拟接口
bridge name	bridge id		STP enabled	interfaces
br0		8000.000000000000	no		
[root@localhost ~]# ifconfig br0 172.25.254.112/24    给 br0 虚拟接口 赋予一个IP地址
[root@localhost ~]# ifconfig       查看虚拟接口信息 有了IP地址
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.112  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::90b8:3ff:feae:295a  prefixlen 64  scopeid 0x20<link>
        ether 92:b8:03:ae:29:5a  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 2835 (2.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:00:0c:0a  txqueuelen 1000  (Ethernet)
        RX packets 23707  bytes 1686713 (1.6 MiB)
        RX errors 0  dropped 1228  overruns 0  frame 0
        TX packets 128  bytes 18818 (18.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:67:d8:a7  txqueuelen 1000  (Ethernet)
        RX packets 23718  bytes 1686113 (1.6 MiB)
        RX errors 0  dropped 4480  overruns 0  frame 0
        TX packets 3  bytes 182 (182.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 3598  bytes 305844 (298.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3598  bytes 305844 (298.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ping 172.25.254.12                ping一下主机 ,发现是ping不同的,因为是没有加载在网卡上的
PING 172.25.254.12 (172.25.254.12) 56(84) bytes of data.
From 172.25.254.112 icmp_seq=1 Destination Host Unreachable
From 172.25.254.112 icmp_seq=2 Destination Host Unreachable
From 172.25.254.112 icmp_seq=3 Destination Host Unreachable
From 172.25.254.112 icmp_seq=4 Destination Host Unreachable
^C
--- 172.25.254.12 ping statistics ---
5 packets transmitted, 0 received, +4 errors, 100% packet loss, time 4001ms
[root@localhost ~]# brctl addif br0 eth0           将这个虚拟接口加载到eth0 这块网卡上
[root@localhost ~]# brctl show    查看网桥信息
bridge name	bridge id		STP enabled	interfaces
br0		8000.525400000c0a	no		eth0       虚拟接口已经装到eth0网卡上
[root@localhost ~]# ping 172.25.254.12      ping 一下是可以ping 通的,因为已经有了网卡的
PING 172.25.254.12 (172.25.254.12) 56(84) bytes of data.
64 bytes from 172.25.254.12: icmp_seq=1 ttl=64 time=0.292 ms
64 bytes from 172.25.254.12: icmp_seq=2 ttl=64 time=0.143 ms
64 bytes from 172.25.254.12: icmp_seq=3 ttl=64 time=0.214 ms
64 bytes from 172.25.254.12: icmp_seq=4 ttl=64 time=0.220 ms
^C
--- 172.25.254.12 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.143/0.217/0.292/0.053 ms
[root@localhost ~]# 

  • 删除临时的网络接口
[root@localhost Desktop]# brctl show    查看网桥的信息 
bridge name	bridge id		STP enabled	interfaces
br0		8000.525400000c0a	no		eth0
[root@localhost Desktop]# brctl delif br0 eth0     将br0这个虚拟接口 从eth0这块网卡上删除掉
[root@localhost Desktop]# brctl show    再次查看网桥信息 已经没有了eth0 这块网卡
bridge name	bridge id		STP enabled	interfaces
br0		8000.000000000000	no		
[root@localhost Desktop]# ifconfig br0 down     因为正在工作的,所以是要让其down掉,才能删除
[root@localhost Desktop]# brctl delbr br0     删除虚拟接口
[root@localhost Desktop]# brctl  show   查看网桥信息 是已经全部没有了
bridge name	bridge id		STP enabled	interfaces
[root@localhost Desktop]# ifconfig    查看网络信息 ,已经没有了br0临时接口
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:00:0c:0a  txqueuelen 1000  (Ethernet)
        RX packets 44359  bytes 3095658 (2.9 MiB)
        RX errors 0  dropped 1228  overruns 0  frame 0
        TX packets 166  bytes 29931 (29.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:67:d8:a7  txqueuelen 1000  (Ethernet)
        RX packets 44334  bytes 3089827 (2.9 MiB)
        RX errors 0  dropped 4481  overruns 0  frame 0
        TX packets 3  bytes 182 (182.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 3859  bytes 328676 (320.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3859  bytes 328676 (320.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost Desktop]# 

最后

以上就是玩命红酒为你收集整理的linux系统的网桥搭建的全部内容,希望文章能够帮你解决linux系统的网桥搭建所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部