如何在Debian Linux上設(shè)置和配置網(wǎng)橋
Q:我是一個(gè)新 Debian Linux 用戶,我想為 Debian Linux 上運(yùn)行的虛擬化環(huán)境(KVM)設(shè)置網(wǎng)橋。那么我該如何在 Debian Linux 9.x 服務(wù)器上的 /etc/network/interfaces
中設(shè)置橋接網(wǎng)絡(luò)呢?
如何你想為你的虛擬機(jī)分配 IP 地址并使其可從你的局域網(wǎng)訪問,則需要設(shè)置網(wǎng)絡(luò)橋接器。默認(rèn)情況下,虛擬機(jī)使用 KVM 創(chuàng)建的專用網(wǎng)橋。但你需要手動(dòng)設(shè)置接口,避免與網(wǎng)絡(luò)管理員發(fā)生沖突。
怎樣安裝 brctl
輸入以下 apt-get 命令:
$ sudo apt install bridge-utils
怎樣在 Debian Linux 上設(shè)置網(wǎng)橋
你需要編輯 /etc/network/interface
文件。不過,我建議在 /etc/network/interface.d/
目錄下放置一個(gè)全新的配置。在 Debian Linux 配置網(wǎng)橋的過程如下:
步驟 1 - 找出你的物理接口
使用 ip 命令:
$ ip -f inet a s
示例輸出如下:
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
是我的物理網(wǎng)卡。
步驟 2 - 更新 /etc/network/interface 文件
確保只有 lo
(loopback 在 /etc/network/interface
中處于活動(dòng)狀態(tài))。(LCTT 譯注:loopback 指本地環(huán)回接口,也稱為回送地址)刪除與 eno1
相關(guān)的任何配置。這是我使用 cat 命令 打印的配置文件:
$ cat /etc/network/interface
# 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 interface
auto lo
iface lo inet loopback
步驟 3 - 在 /etc/network/interfaces.d/br0 中配置網(wǎng)橋(br0)
使用文本編輯器創(chuàng)建一個(gè)文本文件,比如 vi
命令:
$ sudo vi /etc/network/interfaces.d/br0
在其中添加配置:
## static ip config file for br0 ##
auto br0
iface 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 地址:
## DHCP ip config file for br0 ##
auto br0
# Bridge setup
iface br0 inet dhcp
bridge_ports eno1
步驟 4 - 重新啟動(dòng)網(wǎng)絡(luò)服務(wù)
在重新啟動(dòng)網(wǎng)絡(luò)服務(wù)之前,請確保防火墻已關(guān)閉。防火墻可能會(huì)引用較老的接口,例如 eno1
。一旦服務(wù)重新啟動(dòng),你必須更新 br0
接口的防火墻規(guī)則。鍵入以下命令重新啟動(dòng)防火墻:
$ sudo systemctl restart network-manager
確認(rèn)服務(wù)已經(jīng)重新啟動(dòng):
$ systemctl status network-manager
借助 ip 命令尋找新的 br0
接口和路由表:
$ ip a s $ ip r $ ping -c 2 cyberciti.biz
示例輸出:
你可以使用 brctl 命令查看網(wǎng)橋有關(guān)信息:
$ brctl show
顯示當(dāng)前網(wǎng)橋:
$ bridge link