我正在嘗試為我的家庭網路設定虛擬防火牆。架構如下:網際網路 -> 虛擬化 pfSense -> lan。有許多關於如何使用舊網路配置器執行此操作的範例,但沒有使用 Ubuntu 18.04 的網路管理器 netplan 的範例。
我的主機上有兩個網路卡,並且安裝了新安裝的ubuntu 18.04伺服器版本。
這是 /etc/netplan/50-cloud-init.yaml 檔案的內容
username@scarif:~$ cat /etc/netplan/50-cloud-init.yaml
network:
version: 2
renderer: networkd
ethernets:
eno1:
dhcp4: no
dhcp6: no
enp5s0:
dhcp4: no
dhcp6: no
bridges:
br_wan:
interfaces: [enp5s0]
dhcp4: no
dhcp6: no
addresses: [10.0.0.1/24]
br_lan:
interfaces: [eno1]
dhcp4: no
dhcp6: no
addresses: [192.168.1.29/24]
gateway4: 192.168.1.1
nameservers:
addresses: [192.168.1.1,8.8.8.8]
請注意,我相信我需要為 LAN 橋接器分配靜態 IP,並且我希望虛擬化 pfSense 從 ISP 請求 DHCP 位址,因此我為 WAN 橋接器分配了一個廢棄的 IP,並且故意不分配WAN 橋的網關或名稱伺服器。 (如果這是錯誤的,很高興在此網路規劃配置上修正)
我認為我需要創建第二個虛擬橋,所以我也這樣做了。我顯示了預設的(也稱為 LAN)和我創建的(我命名為 wan_bridge)
username@scarif:~$ sudo virsh net-dumpxml default
<network>
<name>default</name>
<uuid>5e5d35c8-c46a-43ed-9fc4-13dcb3853b34</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:dc:57:8f'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
username@scarif:~$ sudo virsh net-dumpxml wan_bridge
<network>
<name>wan_bridge</name>
<uuid>37a0163b-ae70-445d-a25f-c62cbe7d5b51</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='52:54:00:dc:57:8d'/>
<ip address='192.168.133.1' netmask='255.255.255.0'>
</ip>
</network>
兩者都在 virsh 網表中顯示得很好
username@scarif:~$ sudo virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
wan_bridge active yes yes
我也在 brctl 中看到了兩者
username@scarif:~$ sudo brctl show
bridge name bridge id STP enabled interfaces
br_lan 8000.aefa52b9a49b no eno1
br_wan 8000.22c9e0f24ba3 no enp5s0
virbr0 8000.525400dc578f yes virbr0-nic
virbr1 8000.525400dc578d yes virbr1-nic
在這一切之後執行 ifconfig 會導致:
username@scarif:~$ ifconfig
br_lan: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.29 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::acfa:52ff:feb9:a49b prefixlen 64 scopeid 0x20<link>
ether ae:fa:52:b9:a4:9b txqueuelen 1000 (Ethernet)
RX packets 20623 bytes 3453527 (3.4 MB)
RX errors 0 dropped 36 overruns 0 frame 0
TX packets 4756 bytes 5192472 (5.1 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
br_wan: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c9:e0ff:fef2:4ba3 prefixlen 64 scopeid 0x20<link>
ether 22:c9:e0:f2:4b:a3 txqueuelen 1000 (Ethernet)
RX packets 13 bytes 650 (650.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1996 (1.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 98:90:96:be:eb:02 txqueuelen 1000 (Ethernet)
RX packets 22673 bytes 4163339 (4.1 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9632 bytes 5540146 (5.5 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 20 memory 0xf7d00000-f7d20000
enp5s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 68:1c:a2:12:f8:e9 txqueuelen 1000 (Ethernet)
RX packets 849 bytes 128644 (128.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1996 (1.9 KB)
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 1000 (Local Loopback)
RX packets 347 bytes 26756 (26.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 347 bytes 26756 (26.7 KB)
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:dc:57:8f 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
inet 192.168.133.1 netmask 255.255.255.0 broadcast 192.168.133.255
ether 52:54:00:dc:57:8d 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
但是當我用這個命令字串安裝 pfSense 時:
sudo virt-install \
--name pfsense \
--memory 2048
--graphics vnc,listen=0.0.0.0 \
--disk /var/lib/libvirt/images/pfsense.qcow2,size=8,format=qcow2 \
--autostart \
--os-type linux\
--cdrom /home/dooguls/pfSense-CE-2.4.4-RELEASE-amd64.iso \
--debug
安裝順利,但 pfsense 只看到一個接口,即預設網路提供的接口,即我的 LAN 接口/網橋。
答案1
輸入所有問題後,我意識到問題在於虛擬機器未配置為“查看”另一個介面。因此,為了解決該問題,我執行了以下操作:
sudo virsh dumpxml pfsense > 20181201-pfsense.txt
這當然給了我的虛擬機器的完整 xml 定義。然後我複製了現有的介面條目:
<interface type='bridge'>
<mac address='52:54:00:3a:37:7d'/>
<source bridge='br_lan'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
並透過執行以下命令複製它以建立新介面: sudo virsh edit pfsense
並且新增這些行:(請注意,我將 mac 位址更改了一位,並將虛擬網路卡指派給插槽 0x06)
<interface type='bridge'>
<mac address='52:54:00:3a:37:7e'/>
<source bridge='br_wan'/>
<target dev='vnet1'/>
<model type='rtl8139'/>
<alias name='net1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</interface>
然後我要做的就是啟動 pfsense 虛擬機器並將我的介面指派給 WAN 和 LAN。 WAN 從 ISP 取得了 DHCP,我在 LAN 介面上設定了靜態 IP。
所以最後我認為虛擬機器管理程式/主機的三個關鍵步驟是:
- 在 netplan 中設定兩個網橋,每個 NIC 卡一個,並將 IP 位址指派給網橋,而不是實體 NIC 卡。
- 使用 virsh net-define 而不是 virsh net-create 建立新的虛擬網路。 (前者使其持久)
- 將第二個介面新增至 VM 的 XML 定義中,並將該介面指向 WAN 橋。