Networking

iptables 埠轉發不適用於 libvirtd VM

  • November 22, 2020

出於某種原因,我無法從外界連接到我在 VM 內的伺服器,但我絕對可以從虛擬機管理程序連接到我的伺服器。伺服器應用程序在埠 27500 和 27015 上執行。我對 iptables 有點缺乏經驗,所以可能是我做了一些明顯的錯誤。這是我的 iptables-save:

[root@FW-H01 ~]# iptables-save
# Generated by iptables-save v1.8.4 on Fri Apr  3 21:34:51 2020
*mangle
:PREROUTING ACCEPT [10974504:113682765318]
:INPUT ACCEPT [9998403:109052061915]
:FORWARD ACCEPT [963508:4628352252]
:OUTPUT ACCEPT [11423761:2389165771]
:POSTROUTING ACCEPT [12387067:7017507319]
:LIBVIRT_PRT - [0:0]
-A POSTROUTING -j LIBVIRT_PRT
-A LIBVIRT_PRT -o virbr0 -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
COMMIT
# Completed on Fri Apr  3 21:34:51 2020
# Generated by iptables-save v1.8.4 on Fri Apr  3 21:34:51 2020
*nat
:PREROUTING ACCEPT [7429:1219873]
:INPUT ACCEPT [1302:77639]
:OUTPUT ACCEPT [321:22443]
:POSTROUTING ACCEPT [321:22443]
:LIBVIRT_PRT - [0:0]
-A PREROUTING -d 116.202.163.214/32 -p tcp -m tcp --dport 27500 -j DNAT --to-destination 192.168.122.233:27500
-A PREROUTING -d 116.202.163.214/32 -p tcp -m tcp --dport 27015 -j DNAT --to-destination 192.168.122.233:27015
-A PREROUTING -d 116.202.163.214/32 -p udp -m udp --dport 27015 -j DNAT --to-destination 192.168.122.233:27015
-A PREROUTING -d 116.202.163.214/32 -p udp -m udp --dport 27500 -j DNAT --to-destination 192.168.122.233:27500
-A POSTROUTING -j LIBVIRT_PRT
-A POSTROUTING -s 192.168.122.233/32 -p tcp -m tcp --sport 27500 -j SNAT --to-source 116.202.163.214
-A POSTROUTING -s 192.168.122.233/32 -p tcp -m tcp --sport 27015 -j SNAT --to-source 116.202.163.214
-A POSTROUTING -s 192.168.122.233/32 -p udp -m udp --sport 27015 -j SNAT --to-source 116.202.163.214
-A POSTROUTING -s 192.168.122.233/32 -p udp -m udp --sport 27500 -j SNAT --to-source 116.202.163.214
-A LIBVIRT_PRT -s 192.168.122.0/24 -d 224.0.0.0/24 -j RETURN
-A LIBVIRT_PRT -s 192.168.122.0/24 -d 255.255.255.255/32 -j RETURN
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE
COMMIT
# Completed on Fri Apr  3 21:34:51 2020
# Generated by iptables-save v1.8.4 on Fri Apr  3 21:34:51 2020
*filter
:INPUT ACCEPT [173397:141627735]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [164473:257858146]
:LIBVIRT_FWI - [0:0]
:LIBVIRT_FWO - [0:0]
:LIBVIRT_FWX - [0:0]
:LIBVIRT_INP - [0:0]
:LIBVIRT_OUT - [0:0]
-A INPUT -j LIBVIRT_INP
-A FORWARD -j LIBVIRT_FWX
-A FORWARD -j LIBVIRT_FWI
-A FORWARD -j LIBVIRT_FWO
-A FORWARD -d 192.168.122.233/32 -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 27500 -j ACCEPT
-A FORWARD -d 192.168.122.233/32 -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 27015 -j ACCEPT
-A FORWARD -d 192.168.122.233/32 -p udp -m state --state NEW,RELATED,ESTABLISHED -m udp --dport 27015 -j ACCEPT
-A FORWARD -d 192.168.122.233/32 -p udp -m state --state NEW,RELATED,ESTABLISHED -m udp --dport 27500 -j ACCEPT
-A OUTPUT -j LIBVIRT_OUT
-A LIBVIRT_FWI -d 192.168.122.0/24 -o virbr0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A LIBVIRT_FWI -o virbr0 -j REJECT --reject-with icmp-port-unreachable
-A LIBVIRT_FWO -s 192.168.122.0/24 -i virbr0 -j ACCEPT
-A LIBVIRT_FWO -i virbr0 -j REJECT --reject-with icmp-port-unreachable
-A LIBVIRT_FWX -i virbr0 -o virbr0 -j ACCEPT
-A LIBVIRT_INP -i virbr0 -p udp -m udp --dport 53 -j ACCEPT
-A LIBVIRT_INP -i virbr0 -p tcp -m tcp --dport 53 -j ACCEPT
-A LIBVIRT_INP -i virbr0 -p udp -m udp --dport 67 -j ACCEPT
-A LIBVIRT_INP -i virbr0 -p tcp -m tcp --dport 67 -j ACCEPT
-A LIBVIRT_OUT -o virbr0 -p udp -m udp --dport 53 -j ACCEPT
-A LIBVIRT_OUT -o virbr0 -p tcp -m tcp --dport 53 -j ACCEPT
-A LIBVIRT_OUT -o virbr0 -p udp -m udp --dport 68 -j ACCEPT
-A LIBVIRT_OUT -o virbr0 -p tcp -m tcp --dport 68 -j ACCEPT
COMMIT
# Completed on Fri Apr  3 21:34:51 2020

任何人都可以看到任何不正常的東西嗎?請注意,大多數規則是由 libvirtd 生成的,我不確定這與我的規則之間是否存在任何中斷的互動,或者如果我使用 libvirtd NAT 設置,我是否應該完全不同地做這件事. 讓我知道是否需要添加其他資訊。非常歡迎任何想法,我一直在努力解決這個問題好幾個小時,謝謝!

這裡的問題來自以下鏈:

-A LIBVIRT_FWI -o virbr0 -j REJECT --reject-with icmp-port-unreachable
-A LIBVIRT_FWO -i virbr0 -j REJECT --reject-with icmp-port-unreachable

如果轉發模式設置為“nat”,則它們由 libvirtd 根據 libvirt 中的網路配置添加。

一種可能的解決方案是將模式更改為“打開”,請小心,因為您必須通過這樣做手動添加轉發規則。

# virsh net-edit default

<network>
 <name>default</name>
 <uuid>......</uuid>
 <forward mode='open'/>
  .............

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