2017-11-25 408 views
0

我對來賓網絡使用NAT模式。我需要我的機器可以從外面訪問。我已經設置了iptables來將主機上的特定端口轉發到guest虛擬機上的端口22,但這似乎不起作用。使用KVM/QEMU在NAT上進行端口轉發

我添加了這個規則:

# Port Forwardings 
-A PREROUTING -i eth0 -p tcp --dport 9867 -j DNAT --to-destination 192.168.122.136:22 

# Forward traffic through eth0 - Change to match you out-interface 
-A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE 

當我ssh 192.168.122.136從主機它完美的作品,但是當我嘗試ssh 192.168.122.136 -p 9867它顯示ssh: connect to host 192.168.122.1 port 9867: Connection refused

我已經啓用使用上/etc/ufw/sysctl.conf

端口轉發iptables -t nat -L顯示規則設置在iptable上

DNAT  tcp -- anywhere    anywhere    tcp dpt:9867 to:192.168.122.136:22 

回答

0

找到我的答案here。基本上我改變了上述

# connections from outside 
iptables -t nat -A PREROUTING -p tcp --dport 9867 -j DNAT --to 192.168.122.136:22 
# for local connection 
iptables -t nat -A OUTPUT -p tcp --dport 9867 -j DNAT --to 192.168.122.136:22 

# Masquerade local subnet 
iptables -t nat -A POSTROUTING -s 192.168.122.0/24 -j MASQUERADE 
iptables -A FORWARD -o virbr0 -m state --state RELATED,ESTABLISHED -j ACCEPT 
iptables -A FORWARD -i virbr0 -o eth0 -j ACCEPT 
iptables -A FORWARD -i virbr0 -o lo -j ACCEPT