ubuntu – 向libvirt / KVM中的访客转发端口

前端之家收集整理的这篇文章主要介绍了ubuntu – 向libvirt / KVM中的访客转发端口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在使用NAT时,如何将运行libvirt / KVM的服务器上的端口转发到VM上的指定端口?

例如,主机的公共IP为1.2.3.4.我想将端口80转发到10.0.0.1,将端口22转发到10.0.0.2.

我假设我需要添加iptables规则,但我不确定哪里合适以及究竟应该指定什么.

输出iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere            udp dpt:domain 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:domain 
ACCEPT     udp  --  anywhere             anywhere            udp dpt:bootps 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:bootps 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             10.0.0.0/24         state RELATED,ESTABLISHED 
ACCEPT     all  --  10.0.0.0/24          anywhere            
ACCEPT     all  --  anywhere             anywhere            
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable 
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

输出ifconfig

eth0      Link encap:Ethernet  HWaddr 00:1b:fc:46:73:b9  
          inet addr:192.168.1.14  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::21b:fcff:fe46:73b9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:201 errors:0 dropped:0 overruns:0 frame:0
          TX packets:85 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:31161 (31.1 KB)  TX bytes:12090 (12.0 KB)
          Interrupt:17 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

virbr1    Link encap:Ethernet  HWaddr ca:70:d1:77:b2:48  
          inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::c870:d1ff:fe77:b248/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:468 (468.0 B)

我正在使用Ubuntu 10.04.

用于Ubuntu的libvirt的最新稳定版本是0.7.5版本,它没有一些更新的功能(即脚本挂钩和网络过滤器),这使得自动网络配置更容易.也就是说,这是如何在Ubuntu 10.04 Lucid Lynx上为libvirt 0.7.5启用端口转发.

这些iptables规则应该可以解决问题:

iptables -t nat -I PREROUTING -p tcp -d 1.2.3.4 --dport 80 -j DNAT --to-destination 10.0.0.1:80
iptables -t nat -I PREROUTING -p tcp -d 1.2.3.4 --dport 22 -j DNAT --to-destination 10.0.0.2:22
iptables -I FORWARD -m state -d 10.0.0.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT

默认的KVM NAT配置提供了类似于上面给出的第3个规则,但是它省略了NEW状态,这对于接受传入连接是必不可少的.

如果你编写一个启动脚本来添加这些规则并且你不小心,libvirt 0.7.5会通过插入它们来覆盖它们.因此,为了确保在启动时正确应用这些规则,您需要确保在插入规则之前初始化了libvirt.

在行退出0之前,将以下行添加到/etc/rc.local:

(
# Make sure the libvirt has started and has initialized its network.
while [ `ps -e | grep -c libvirtd` -lt 1 ]; do
        sleep 1
done
sleep 10
# Set up custom iptables rules.
iptables -t nat -I PREROUTING -p tcp -d 1.2.3.4 --dport 80 -j DNAT --to-destination 10.0.0.1:80
iptables -t nat -I PREROUTING -p tcp -d 1.2.3.4 --dport 22 -j DNAT --to-destination 10.0.0.2:22
iptables -I FORWARD -m state -d 10.0.0.0/24 --state NEW,ESTABLISHED -j ACCEPT
) &

上面的睡眠10是一个黑客,以确保libvirt守护进程在我们添加自己的iptables规则之前有机会初始化其iptables规则.我不能等到他们为Ubuntu发布libvirt版本0.8.3.

原文链接:https://www.f2er.com/ubuntu/349046.html

猜你在找的Ubuntu相关文章