Domain-Name-System

QEMU/KVM 來賓 VM 無法解析 DNS 主機名或來自主機的 SSH/ping

  • January 6, 2022

我有一個 CentosOS 6.4 的基於 QEMU/KVM 的虛擬機,無法完全連接到 Internet。我可以 ping IP 地址(例如ping 8.8.8.8成功),但域名未解析(例如ping google.com返回ping: unknown host google.com)。

來賓分配了一個靜態 IP,我使用的是預設(基於 NAT)網路:

<network>
 <name>default</name>
 <uuid>8c257186-7af4-4e19-a086-27f50f692af6</uuid>
 <forward mode='nat'/>
 <bridge name='virbr0' stp='on' delay='0'/>
 <mac address='52:54:00:d6:62:2d'/>
 <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>

/run/resolvconf/resolv.conf虛擬機上不存在;, resolvconf, nslookup, systemctl, host,hostnamectlnmcli程序均未安裝,因此執行網路設置診斷的能力受到限制。

我嘗試了以下方法:

  • /etc/sysconfig/network-scripts/ifcfg-eth0通過添加DNS1=8.8.8.8DNS2=8.8.4.4進行編輯PEERDNS=yes。重新啟動後,這創建了/etc/resolv.conf最初不存在的 。
  • 將“hosts: files dns”替換為“hosts: files dns nisplus nis” /etc/nsswitch.conf,然後重新啟動。
  • 按照本指南創建僅主機網路。
  • 按照本指南通過 qemu-bridge-helper 創建虛擬網橋。
  • (順便說一句,我試圖通過本指南在來賓和主機之間創建文件共享,但這需要 9p,CentOS 6 顯然不兼容的文件系統。)

但是每次嘗試,主機仍然無法通過 SSH 連接/ping 來賓,也無法解析域名。對於完成這兩個步驟中的任何一個的任何幫助(主機可以通過 SSH 連接到來賓來賓完全連接到 Internet),我們將不勝感激。

以下是一些可能有用的文件內容/命令行輸出。很高興提供更多資訊。

/etc/resolv.conf(嘉賓)的內容:

options edns0 trust-ad
; generated by /sbin/dhclient-script
nameserver 8.8.8.8
nameserver 8.8.4.4

ip a(客人)的輸出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
   inet6 ::1/128 scope host 
      valid_lft forever preferred_lft forever


2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
   link/ether 52:54:00:7b:6c:27 brd ff:ff:ff:ff:ff:ff
   inet 192.168.122.92/22 brd 192.168.123.255 scope global eth0
   inet6 fe80::5054fffe7b:6c27/64 scope link
      valid_lft forever preferred_lft forever

ssh 192.168.122.92(主機)的輸出:

ssh: connect to host 192.168.122.92 port 22: Connection refused

ping 192.168.122.92(主機)的輸出:

PING 192.168.122.92 (192.168.122.92) 56(84) bytes of data.
From 192.168.122.1 icmp_seq=1 Destination Port Unreachable
ping: sendmsg: Operation not permitted
From 192.168.122.1 icmp_seq=2 Destination Port Unreachable
ping: sendmsg: Operation not permitted
From 192.168.122.1 icmp_seq=3 Destination Port Unreachable
ping: sendmsg: Operation not permitted
From 192.168.122.1 icmp_seq=4 Destination Port Unreachable
ping: sendmsg: Operation not permitted
^C
--- 192.168.122.92 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3065ms

編輯 1

/etc/sysconfig/network-scripts/ifcfg-eth0(嘉賓)的內容:

DEVICE=eth0
HWADDR=52:54:00:7B:6C:27
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPRONTO=none
IPADDR=192.168.122.92
NETMASK=255.255.252.0
GATEWAY=192.168.122.1
DNS1=8.8.8.8
DNS2=8.8.4.4
PEERDNS=yes

編輯 2

iptables-save(主機)的輸出:

# Generated by iptables-save v1.8.4 on Mon Jan  3 22:03:26 2022
*mangle
:PREROUTING ACCEPT [86972:77359835]
:INPUT ACCEPT [86966:77359331]
:FORWARD ACCEPT [6:504]
:OUTPUT ACCEPT [87805:9060728]
:POSTROUTING ACCEPT [69226:7583136]
: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 Mon Jan  3 22:03:26 2022
# Generated by iptables-save v1.8.4 on Mon Jan  3 22:03:26 2022
*nat
:PREROUTING ACCEPT [4:1038]
:INPUT ACCEPT [3:954]
:OUTPUT ACCEPT [19614:1550200]
:POSTROUTING ACCEPT [1032:73142]
:LIBVIRT_PRT - [0:0]
-A POSTROUTING -j LIBVIRT_PRT
-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 Mon Jan  3 22:03:26 2022
# Generated by iptables-save v1.8.4 on Mon Jan  3 22:03:26 2022
*filter
:INPUT ACCEPT [86966:77359331]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [69220:7582632]
: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 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 Mon Jan  3 22:03:26 2022

編輯 3

sudo iptables-save(客人)的輸出:

# Generated by iptables-save v1.4.7 on Thu Jan 6 05:53:35 2022
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT
COMMIT
# Completed on Thu Jan 6 05:53:35 2022

以下錯誤消息ping表明通信被防火牆阻止。

Destination Port Unreachable
ping: sendmsg: Operation not permitted

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