使用proxmox创建的虚拟机,无法访问外网的解决办法:
1、配置/etc/network/interfaces文件
添加如下信息:
auto vmbr2 iface vmbr2 inet static address 10.0.0.254/24 bridge-ports none bridge-stp off bridge-fd 0 post-up echo 1 > /proc/sys/net/ipv4/ip_forward post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 8888 -j DNAT --to 10.0.0.102:8888 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 8888 -j DNAT --to 10.0.0.102:8888 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 3389 -j DNAT --to 10.0.0.102:3389 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 3389 -j DNAT --to 10.0.0.102:3389 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 10022 -j DNAT --to 10.0.0.102:22 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 10022 -j DNAT --to 10.0.0.102:22 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to 10.0.0.102:443 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to 10.0.0.102:443 以下为新增,增加回流,这样就可以在nat通过公网IP访问服务: post-up iptables -t nat -A PREROUTING -s 10.0.0.0/24 -d 公网/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.0.0.102 post-up iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 10.0.0.0/24 -j SNAT --to-source 10.0.0.254 post-down iptables -t nat -A PREROUTING -s 10.0.0.0/24 -d 公网/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.0.0.102 post-down iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 10.0.0.102 -j SNAT --to-source 10.0.0.254 说明: PREROUTING -s 私网网段 -d 公网IP -j DNAT –to-destination 私网服务器地址 POSTROUTING -s 私网网段 -d 私网服务器地址 -j SNAT –to-source 私网网关
配置后,文件内容大致如下:
auto lo iface lo inet loopback iface enp1s0 inet manual auto vmbr0 iface vmbr0 inet static address 公网IP地址/32 gateway 100.64.35.133 bridge-ports enp1s0 bridge-stp off bridge-fd 0 iface vmbr0 inet6 static address IPV6/64 gateway IPV6网关 auto vmbr2 iface vmbr2 inet static address 10.0.0.254/24 bridge-ports none bridge-stp off bridge-fd 0 post-up echo 1 > /proc/sys/net/ipv4/ip_forward post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 8888 -j DNAT --to 10.0.0.102:8888 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 8888 -j DNAT --to 10.0.0.102:8888 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 3389 -j DNAT --to 10.0.0.102:3389 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 3389 -j DNAT --to 10.0.0.102:3389 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 10022 -j DNAT --to 10.0.0.102:22 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 10022 -j DNAT --to 10.0.0.102:22 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to 10.0.0.102:443 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to 10.0.0.102:443 post-up iptables -t nat -A PREROUTING -s 10.0.0.0/24 -d 公网/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.0.0.102 post-up iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 10.0.0.0/24 -j SNAT --to-source 10.0.0.254 post-down iptables -t nat -A PREROUTING -s 10.0.0.0/24 -d 公网/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.0.0.102 post-down iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 10.0.0.102 -j SNAT --to-source 10.0.0.254
重启网络配置:
service networking restart
2、在proxmox管理界面配置虚拟机网络属性
选择vmb2
3、在虚拟机中配置网络属性:
测试: