记一次KVM虚拟机和防火墙的冲突

站在巨人的肩膀上

part.0 背景

在一次停电之后,发现无法连接到数据库了,此台数据库服务器的是用KVM虚拟出来的。当关闭firewalld时,能连接,但是在开启firewalld时,即使开放端口也无法连接。

part.1 检查

1.1 查看服务器是否启动

服务器是正常启动的

$ virsh list
 Id    名称                         状态
----------------------------------------------------
 1    centos7-mysql                  running

1.2 查看firewalld开放端口

$ firewall-cmd --zone=public --list-ports
5900/tcp 5901/tcp 5902/tcp 5903/tcp 5904/tcp 10050/tcp 3306/tcp 12244/tcp

vnc的端口也正常开放。

1.3 查看iptables情况

$ iptables -nvL

部分
Chain IN_public_allow (0 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_deny (0 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_log (0 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT_direct (0 references)
 pkts bytes target     prot opt in     out     source               destination   

发现在IN_public_allow 中并没有开放端口。

part.2 解决办法

添加相应的端口号到IN_public_allow

$ iptables -A IN_public_allow -p tcp --dport 5900:5910 -j ACCEPT
$ iptables -nvL

Chain IN_public_allow (0 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpts:5900:5910

Chain IN_public_deny (0 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_log (0 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT_direct (0 references)
 pkts bytes target     prot opt in     out     source               destination         

然后再进行连接测试,连接成功。

你可能感兴趣的:(记一次KVM虚拟机和防火墙的冲突)