Routing

使用 nftables 路由(Fedora33、nmcli、brctl)

  • January 5, 2021

A/ 帶有乙太網的主機 (Fedora 33) 如果eth0. ip 192.168.18.11(/24)

B1/ Kvm guest with ifvnet0被奴役到virbr101. ip 192.168.101.88(/24)(手動路由,靜態 IP)

B2/Kvm guest with ifvnet1被奴役到virbr102. ip 192.168.102.210(/24) (virt-manager 路由, DHCP)

使用下面顯示的配置,我似乎缺少讓它工作的步驟。不知道如何調試我的設置。

會發生什麼:

  1. 從 A,我可以 ping B 和 ssh 進入 B
  2. 從 B,我可以 ping A
  3. 從A,我可以ping 192.168.18.1
  4. 從 B,我無法 ping 192.168.18.1,這就是我現階段要解決的問題

B1 和 B2 之間的區別在於,如果我 ping 一個命名伺服器,例如 google.com,用 B1 它說沒有到主機的路由,用 B2 它找到 IP 地址,但沒有 ping 回复。

nmcli使用+ brctl+的路由網路nft

在主機上,virbr101

ip link add virbr101-mac address 52:54:41:0b:00:01 type dummy
brctl addif virbr101 virbr101-mac

文件ifcfg-virbr101

DEVICE=virbr101
NAME=virbr101
# Change to 'no' to disable NetworkManager for this interface.
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Bridge
DELAY=2
STP=on

IPADDR=192.168.101.1
NETMASK=255.255.255.0

啟動此界面:

nmcli connection load /etc/sysconfig/network-scripts/ifcfg-virbr101
nmcli connection up virbr101

啟用 ip 轉發:

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.forwarding=1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding=1" >> /etc/sysctl.conf
sysctl -p

使用以下nft命令路由介面:

delete table ip filter
add table ip filter
add chain ip filter INPUT
add chain ip filter FORWARD
add chain ip filter OUTPUT
add rule ip filter FORWARD oifname "virbr101" ip daddr 192.168.101.0/24 counter accept
add rule ip filter FORWARD iifname "virbr101" ip saddr 192.168.101.0/24 counter accept
add rule ip filter FORWARD iifname "virbr101" oifname "virbr101" counter accept
add rule ip filter FORWARD iifname "virbr101" counter reject
add rule ip filter FORWARD oifname "virbr101" counter reject

路由網路使用virt-manager

網路:

<network>
 <name>bridged102</name>
 <uuid>2e8d6e42-b70e-43c8-8523-02008070f03c</uuid>
 <forward dev="ens3" mode="route">
   <interface dev="ens3"/>
 </forward>
 <bridge name="virbr0" stp="on" delay="0"/>
 <mac address="52:54:00:42:1d:e4"/>
 <domain name="bridged102"/>
 <ip address="192.168.102.1" netmask="255.255.255.0">
   <dhcp>
     <range start="192.168.102.128" end="192.168.102.254"/>
   </dhcp>
 </ip>
</network>

訪客界面:

<interface type="network">
 <mac address="52:54:00:ee:a6:67"/>
 <source network="bridged102" portid="238df934-14ac-422e-bfbd-a4047a9444fb" bridge="virbr0"/>
 <target dev="vnet1"/>
 <model type="virtio"/>
 <link state="up"/>
 <alias name="net1"/>
 <address type="pci" domain="0x0000" bus="0x00" slot="0x08" function="0x0"/>
</interface>

對於virt-manager解決方案,我遵循了以下範例:libvirt docs // Network XML format // Routed network config

缺少route的是從我的 LAN 上的各種設備返回到我的 VM 的 NW。

如:

route add -net 192.168.102.0/24 gw 192.168.18.11
# Or other gateway as appropriate for the device and nw distance

引用自:https://serverfault.com/questions/1046992