工作中经常需要在linux物理机上安装kvm虚拟机,kvm虚拟机的网络问题最让人头疼,本文介绍桥接模式下,kvm虚拟机网络问题解决。
问题是这样的,宿主机上以桥接模式安装kvm虚拟机,宿主机网络正常,但是虚拟机内部ping除宿主机以外的机器都ping不通,也只有宿主机能ssh到该虚拟机。
首先排查宿主机网络是否正常,对应网卡是否启用等。
1、查看宿主机网桥状态
[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命令查看有哪些
把未添加到br1的vnet添加上来:
[root@localhost ~]# brctl addif br1 vnet0
[root@localhost ~]# brctl addif br1 vnet1
之后再查看网桥状态是否添加成功:
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.1213d36366e1 no eth0
vnet0
vnet1
virbr0 8000.5254003ad74c yes
[root@localhost ~]#
2、查看kvm虚拟机内部路由
[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命令添加路由:
route add default gw 192.168.10.254
3、查看宿主机防火墙
查看防火墙状态:
[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 ~]#
我这里直接防火墙都关了,当然最简单的方法就是关闭防火墙:
[root@localhost ~]# systemctl stop iptables.service
Failed to stop iptables.service: Unit iptables.service not loaded.
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]#
4、修改内核参数
最后上面都设置了,但是虚拟机网络跟外部还是不通,就是说虚拟机只跟宿主机能通信(ping ssh),跟其它机器都访问不了,这时需要查看下宿主机网桥配置相关内核参数:
①ip_forward参数,若为0,则需改为1
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
1
[root@localhost ~]#
[root@localhost ~]# sysctl -b net.ipv4.ip_forward
1[root@localhost ~]#
②对应网桥(注意:我这里是br1)的nf_call_iptables参数,若为1,则需改为0,使宿主机iptables等防火墙不对bridge的数据进行过滤处理。
[root@localhost ~]# cat /proc/sys/net/bridge/bridge-nf-call-iptables
1
[root@localhost ~]#
或者
[root@localhost ~]# cat /sys/devices/virtual/net/br1/bridge/nf_call_iptables
1
[root@localhost ~]#
使用echo修改即可:
[root@localhost ~]# echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables
[root@localhost ~]#
[root@localhost ~]# cat /proc/sys/net/bridge/bridge-nf-call-iptables
0
[root@localhost ~]#
或者
[root@localhost ~]# echo 0 > /sys/devices/virtual/net/br1/bridge/nf_call_iptables
[root@localhost ~]#
[root@localhost ~]# cat /sys/devices/virtual/net/br1/bridge/nf_call_iptables
0
[root@localhost ~]#
这里最好把该网桥下的bridge-nf-call-arptables、bridge-nf-call-ip6tables参数也设置为0。
5、其它
如果以上都排查了,网络还是不通,则看下宿主机是否有其它防火墙软件在启用,或者有没有selinux、KYSEC等规则设置导致的虚拟机网络不通,最简单验证方法就是关闭这些功能。