本文最后更新于:2025年6月24日 晚上

有一天,突然从外部无法访问 KVM 虚拟机了,本文记录解决方案。

问题描述

宿主机上以桥接模式安装kvm虚拟机,宿主机网络正常,但是虚拟机内部ping除宿主机以外的机器都ping不通,也只有宿主机能ssh到该虚拟机。

解决方案

排查宿主机网络

查看宿主机网桥状态

1
2
3
4
5
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.1213d36366e1 no eth0
virbr0 8000.5254003ad74c yes
[root@localhost ~]#

宿主机上网桥br0,可以看到主机网卡eth0接口已经添加到br0上了。

如果br0上的interfas中没有kvm虚拟机对应的vnet(vnet0,vnet1,vnet2等),则需要执行命令添加到br0上:

vnet相关接口可以通过 ip a 命令查看有哪些

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bridge0 state UP group default qlen 1000
link/ether 70:85:c2:80:45:d8 brd ff:ff:ff:ff:ff:ff
3: bridge0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default

5: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bridge0 state UNKNOWN group default qlen 1000
link/ether fe:54:00:d3:66:7d brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fed3:667d/64 scope link
valid_lft forever preferred_lft forever
6: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bridge0 state UNKNOWN group default qlen 1000
link/ether fe:54:00:5d:f7:bb brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fe5d:f7bb/64 scope link
valid_lft forever preferred_lft forever

把未添加到br1的vnet添加上来:

1
2
[root@localhost ~]# brctl addif br1 vnet0
[root@localhost ~]# brctl addif br1 vnet1

之后再查看网桥状态是否添加成功:

1
2
3
4
5
6
7
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.1213d36366e1 no eth0
vnet0
vnet1
virbr0 8000.5254003ad74c yes
[root@localhost ~]#

查看kvm虚拟机内部路由

1
2
3
4
5
6
7
8
9
[root@kvm-test ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.10.254 0.0.0.0 UG 0 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 enp0s3
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.61.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@kvm-test ~]#

如果路由没配置的话,需要配置下,/etc/sysconfig/network-scripts/ifcfg-enp0s3中添加GATEWAY=192.168.10.254,之后执行service network restart重启网络。

或者使用route命令添加路由:

1
route add default gw 192.168.10.254

查看宿主机防火墙

查看防火墙状态:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# systemctl status iptables.service 
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@localhost ~]#
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@localhost ~]#

我这里直接防火墙都关了,当然最简单的方法就是关闭防火墙:

1
2
3
[root@localhost ~]# systemctl stop iptables.service 
Failed to stop iptables.service: Unit iptables.service not loaded.
[root@localhost ~]# systemctl stop firewalld.service

修改内核参数

最后上面都设置了,但是虚拟机网络跟外部还是不通,就是说虚拟机只跟宿主机能通信(ping ssh),跟其它机器都访问不了,这时需要查看下宿主机网桥配置相关内核参数:

  1. ip_forward参数,若为0,则需改为1
1
2
3
4
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
1
[root@localhost ~]# sysctl -b net.ipv4.ip_forward
1
  1. 对应网桥(注意:我这里是br1)的nf_call_iptables参数,若为1,则需改为0,使宿主机iptables等防火墙不对bridge的数据进行过滤处理。

    我的虚拟机就是靠这个修好的。

1
2
# cat /proc/sys/net/bridge/bridge-nf-call-iptables 
1
1
2
# cat /sys/devices/virtual/net/br1/bridge/nf_call_iptables
1

su root 登陆后使用echo修改即可:

最好把该网桥下的bridge-nf-call-arptables、bridge-nf-call-ip6tables参数也设置为0。

1
2
3
echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 0 > /proc/sys/net/bridge/bridge-nf-call-arptables
echo 0 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

参考资料



文章链接:
https://www.zywvvd.com/notes/system/linux/kvm/kvm-net-error-fix/kvm-net-error-fix/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Linux kvm 虚拟机网络不通的问题解决方法
https://www.zywvvd.com/notes/system/linux/kvm/kvm-net-error-fix/kvm-net-error-fix/
作者
Yiwei Zhang
发布于
2025年6月24日
许可协议