前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在 Debian Linux 上设置和配置网桥

在 Debian Linux 上设置和配置网桥

原创
作者头像
用户8989785
修改2021-09-10 17:39:11
4.8K0
修改2021-09-10 17:39:11
举报
文章被收录于专栏:网站技术分享

如何你想为你的虚拟机分配 IP 地址并使其可从你的局域网访问,则需要设置网络桥接器。默认情况下,虚拟机使用 KVM 创建的专用网桥。但你需要手动设置接口,避免与网络管理员发生冲突。

怎样安装 brctl

输入以下 apt-get 命令:

代码语言:javascript
复制
$ sudo apt install bridge-utils

怎样在 Debian Linux 上设置网桥

你需要编辑 /etc/network/interface 文件。不过,我建议在 /etc/network/interface.d/ 目录下放置一个全新的配置。在 Debian Linux 配置网桥的过程如下:

步骤 1 - 找出你的物理接口

使用 ip 命令:

代码语言:javascript
复制
$ ip -f inet a s

示例输出如下:

代码语言:javascript
复制
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 192.168.2.23/24 brd 192.168.2.255 scope global eno1 valid_lft forever preferred_lft forever

eno1 是我的物理网卡。

步骤 2 - 更新 /etc/network/interface 文件

确保只有 lo(loopback 在 /etc/network/interface 中处于活动状态)。(LCTT 译注:loopback 指本地环回接口,也称为回送地址)删除与 eno1 相关的任何配置。这是我使用 cat 命令 打印的配置文件:

代码语言:javascript
复制
$ cat /etc/network/interface
代码语言:javascript
复制
# This file describes the network interfaces available on your system# and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interfaceauto loiface lo inet loopback
步骤 3 - 在 /etc/network/interfaces.d/br0 中配置网桥(br0)

使用文本编辑器创建一个文本文件,比如 vi 命令:

代码语言:javascript
复制
$ sudo vi /etc/network/interfaces.d/br0

在其中添加配置:

代码语言:javascript
复制
## static ip config file for br0 ##auto br0iface br0 inet static    address 192.168.2.23    broadcast 192.168.2.255    netmask 255.255.255.0    gateway 192.168.2.254    # If the resolvconf package is installed, you should not edit    # the resolv.conf configuration file manually. Set name server here    #dns-nameservers 192.168.2.254    # If you have muliple interfaces such as eth0 and eth1    # bridge_ports eth0 eth1    bridge_ports eno1    bridge_stp off # disable Spanning Tree Protocol    bridge_waitport 0 # no delay before a port becomes available    bridge_fd 0 # no forwarding delay

如果你想使用 DHCP 来获得 IP 地址:

代码语言:javascript
复制
## DHCP ip config file for br0 ##auto br0 # Bridge setup iface br0 inet dhcp  bridge_ports eno1

在 vi/vim 中保存并关闭文件。

步骤 4 - 重新启动网络服务

在重新启动网络服务之前,请确保防火墙已关闭。防火墙可能会引用较老的接口,例如 eno1。一旦服务重新启动,你必须更新 br0 接口的防火墙规则。键入以下命令重新启动防火墙:

代码语言:javascript
复制
$ sudo systemctl restart network-manager

确认服务已经重新启动:

代码语言:javascript
复制
$ systemctl status network-manager

借助 ip 命令寻找新的 br0 接口和路由表:

代码语言:javascript
复制
$ ip a s $ ip r $ ping -c 2 cyberciti.biz

示例输出:

你可以使用 brctl 命令查看网桥有关信息:

代码语言:javascript
复制
$ brctl show

显示当前网桥:

代码语言:javascript
复制
$ bridge link

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 怎样安装 brctl
  • 怎样在 Debian Linux 上设置网桥
    • 步骤 1 - 找出你的物理接口
      • 步骤 2 - 更新 /etc/network/interface 文件
        • 步骤 3 - 在 /etc/network/interfaces.d/br0 中配置网桥(br0)
          • 步骤 4 - 重新启动网络服务
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档