第一种方式:Nat转发
VPS端:
iptables -t nat -F iptables -t mangle -F iptables -F iptables -X iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark iptables -t mangle -A PREROUTING -m mark ! --mark 0 -j ACCEPT iptables -t mangle -A PREROUTING -d 107.173.11.201 -j MARK --set-mark 1 iptables -t mangle -A PREROUTING -d 107.173.11.202 -j MARK --set-mark 2 iptables -t mangle -A PREROUTING -d 107.173.11.203 -j MARK --set-mark 3 iptables -t mangle -A PREROUTING -d 107.173.11.204 -j MARK --set-mark 4 iptables -t mangle -A PREROUTING -j CONNMARK --save-mark iptables -t nat -A PREROUTING -d 107.173.11.201 -j DNAT --to-destination 192.99.1.103 iptables -t nat -A POSTROUTING -d 192.99.1.103 -m mark --mark 1 -j SNAT --to-source 107.173.11.201 iptables -t nat -A PREROUTING -d 107.173.11.202 -j DNAT --to-destination 192.99.1.103 iptables -t nat -A POSTROUTING -d 192.99.1.103 -m mark --mark 2 -j SNAT --to-source 107.173.11.202 iptables -t nat -A PREROUTING -d 107.173.11.203 -j DNAT --to-destination 192.99.1.103 iptables -t nat -A POSTROUTING -d 192.99.1.103 -m mark --mark 3 -j SNAT --to-source 107.173.11.203 iptables -t nat -A PREROUTING -d 107.173.11.204 -j DNAT --to-destination 192.99.1.103 iptables -t nat -A POSTROUTING -d 192.99.1.103 -m mark --mark 4 -j SNAT --to-source 107.173.11.204 service iptables save service iptables restart
以上代码作用分别为:
1、清除所有防火墙规则
2、将访问107.173.11.201的请求,标记为“mark 1”,同理标记为“mark 2”“mark 3”
3、保存标记
4、将访问107.173.11.201的请求,转发到192.99.1.103,离开防火墙之前,带上标记 “mark 1”
4、保存防火墙配置
KS3C端:
auto vmbr2 iface vmbr2 inet static address 10.0.2.1 netmask 255.255.255.0 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.2.0/24' -o vmbr0 -j MASQUERADE post-down iptables -t nat -D POSTROUTING -s '10.0.2.0/24' -o vmbr0 -j MASQUERADE post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.2.2 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.2.2 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to 10.0.2.2 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to 10.0.2.2 post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 3839 -j DNAT --to 10.0.2.254 post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 3839 -j DNAT --to 10.0.2.254 post-up iptables -t nat -A PREROUTING -i vmbr0 -s 107.173.11.201 -j DNAT --to 10.0.2.2 post-down iptables -t nat -D PREROUTING -i vmbr0 -s 107.173.11.201 -j DNAT --to 10.0.2.2 post-up iptables -t nat -A PREROUTING -i vmbr0 -s 107.173.11.202 -j DNAT --to 10.0.2.3 post-down iptables -t nat -D PREROUTING -i vmbr0 -s 107.173.11.202 -j DNAT --to 10.0.2.3 post-up iptables -t nat -A PREROUTING -i vmbr0 -s 107.173.11.203 -j DNAT --to 10.0.2.4 post-down iptables -t nat -D PREROUTING -i vmbr0 -s 107.173.11.203 -j DNAT --to 10.0.2.4 post-up iptables -t nat -A PREROUTING -i vmbr0 -s 107.173.11.204 -j DNAT --to 10.0.2.5 post-down iptables -t nat -D PREROUTING -i vmbr0 -s 107.173.11.204 -j DNAT --to 10.0.2.5
以上代码的作用分别为:
1、vmbr2地址为10.0.2.1,整个网段均通过网卡“vmbr0”Nat上网
2、映射80、443端口至内网IP(10.0.2.2),映射3839至内网IP(10.0.2.254)
3、将来自107.173.11.201的数据转发至内网IP(10.0.2.2)同理得出其他IP转发至不同内网IP
第二种方式:Gre隧道,将IP路由至KS3C小鸡
VPS端:
ip link add gre1 type gretap remote 142.4.212.175 local 144.217.240.161 key 66268765 ip addr add 10.10.10.1 peer 10.10.10.2 dev gre1 ip link set gre1 mtu 1458 (不设置,使用默认即可) ip link set gre1 up
以上代码的作用为:
1、建立密码为:66268765的Gre隧道远程链接
2、设置gre1虚拟网卡的MTU值
3、启用gre1虚拟网卡
KS3C端:
ip link add gre1 type gretap remote 144.217.240.161 local 142.4.212.175 key 66268765 ip addr add 10.10.10.2 peer 10.10.10.1 dev gre1 ip link set gre1 mtu 1458 ip link set gre1 up
以上代码的作用同上
/sbin/ip route add 144.217.179.3/32 dev vmbr2 /sbin/ip route add 144.217.179.4/32 dev vmbr2 /sbin/ip route add 144.217.179.5/32 dev vmbr2 /sbin/ip route add 144.217.179.6/32 dev vmbr2 /sbin/ip route add 144.217.179.7/32 dev vmbr2 /sbin/ip route add 144.217.179.8/32 dev vmbr2 /sbin/ip route add 144.217.179.9/32 dev vmbr2 /sbin/ip route add 144.217.179.10/32 dev vmbr2 /sbin/ip route add 144.217.179.11/32 dev vmbr2 /sbin/ip route add 144.217.179.12/32 dev vmbr2 /sbin/ip route add 144.217.179.13/32 dev vmbr2 /sbin/ip route add 144.217.179.14/32 dev vmbr2 /sbin/ip route add 144.217.179.15/32 dev vmbr2 /sbin/ip route add 144.217.179.16/32 dev vmbr2 /sbin/ip route add 144.217.179.17/32 dev vmbr2 /sbin/ip route add 144.217.179.18/32 dev vmbr2
以上代码的作用,将VPS的附加IP,在KS3C端,路由到正确的虚拟网桥
VPS端:
/sbin/ip route add 144.217.179.4/32 dev gre1 /sbin/ip route add 144.217.179.5/32 dev gre1 /sbin/ip route add 144.217.179.6/32 dev gre1 /sbin/ip route add 144.217.179.7/32 dev gre1 /sbin/ip route add 144.217.179.8/32 dev gre1 /sbin/ip route add 144.217.179.9/32 dev gre1 /sbin/ip route add 144.217.179.10/32 dev gre1 /sbin/ip route add 144.217.179.11/32 dev gre1 /sbin/ip route add 144.217.179.12/32 dev gre1 /sbin/ip route add 144.217.179.13/32 dev gre1 /sbin/ip route add 144.217.179.14/32 dev gre1 /sbin/ip route add 144.217.179.15/32 dev gre1 /sbin/ip route add 144.217.179.16/32 dev gre1 /sbin/ip route add 144.217.179.17/32 dev gre1 /sbin/ip route add 144.217.179.18/32 dev gre1 /sbin/arp -i eth0 -s 144.217.179.4 6E:B8:2C:EF:C3:82 pub
以上代码的作用:
1、将需要路由到K3C的IP,路由到gre1虚拟网卡
2、注意,如果VPS的主IP和附加IP不在一个C段,则需要留一个附加IP作为网关,所以“.3”作为网关,不路由到gre1虚拟网卡
3、做ARP绑定,告诉VPS网关,“.4”的MAC地址(KS3C端的小鸡)
4、参考interfaces文件
5、无论哪种方式,切记一定要开启系统转发功能:
vi /etc/sysctl.conf net.ipv4.ip_forward = 1
我的微信
我的微信
微信扫一扫
评论