1. 程式人生 > >VMware搭建內網並通過iptables實現端口轉發實現聯網

VMware搭建內網並通過iptables實現端口轉發實現聯網

配置 enca conf data 一個 info 靜態ip fast accep

整體流程圖

技術分享圖片

配置Server1 新建兩塊網卡 一塊網卡設置為橋接模式,另外一塊設置為僅主機模式

技術分享圖片

技術分享圖片

技術分享圖片

查看兩塊網卡配置

root@ubuntu:~# ifconfig
ens33     Link encap:Ethernet  HWaddr 00:0c:29:42:81:1c  
          inet addr:192.168.31.159  Bcast:192.168.31.255  Mask:255.255.255.0
          inet6 addr: ffff::fff:29ff:fe42:811c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:163 errors:0 dropped:0 overruns:0 frame:0
          TX packets:421 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:20177 (20.1 KB)  TX bytes:35945 (35.9 KB)

打開interfaces查看ens33配置 DHCP 方式

auto ens33
iface ens33 inet dhcp

查看網卡名稱

root@ubuntu:/etc/network# ip link show
root@ubuntu:/etc/network# ip link show
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether ff:ff:ff:ff:81:1c brd ff:ff:ff:ff:ff:ff
3: ens38: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:42:81:26 brd ff:ff:ff:ff:ff:ff

設置內網為靜態ip

auto ens38
iface ens38 inet static
address 192.168.232.101
netmask 255.255.255.0

配置Server3 ip為靜態ip且和上一步配置的內網ip在一個網段

auto ens33
iface ens33 inet static
address 192.168.232.103
netmask 255.255.255.0

測試內網

root@ubuntu:/etc/network# ping 192.168.232.101
PING 192.168.232.101 (192.168.232.101) 56(84) bytes of data.
64 bytes from 192.168.232.101: icmp_seq=1 ttl=64 time=12.0 ms
64 bytes from 192.168.232.101: icmp_seq=2 ttl=64 time=1.61 ms

設置Server3 默認網關為Server1的內網ip

route add default gw 192.168.110.134

修改/etc/sysctl.conf文件 打開ip限制

net.ipv4.ip_forward = 1
sysctl -p /etc/sysctl.conf

設置iptables轉發功能

iptables -P FORWARD DROP
iptables -t nat -A POSTROUTING -s 192.168.232.0/24 -j SNAT --to 192.168.31.159
iptables -A FORWARD -s 192.168.232.103 -j ACCEPT
具體解釋 請參考這篇博客
http://xstarcd.github.io/wiki/Linux/iptables_forward_internetshare.html

查看轉發iptables轉發規則

iptables -t nat -nvL

root@ubuntu:/home/guolin# iptables -t nat -nvL 
Chain PREROUTING (policy ACCEPT 108 packets, 7306 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4 packets, 288 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 4 packets, 288 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  103  7035 SNAT       all  --  *      *       192.168.232.0/24     0.0.0.0/0            to:192.168.31.159
    0     0 SNAT       all  --  *      *       192.168.232.0/24     0.0.0.0/0            to:192.168.31.159
    0     0 SNAT       all  --  *      *       192.168.232.0/24     0.0.0.0/0            to:192.168.31.159
    0     0 SNAT       all  --  *      *       192.168.232.0/24     0.0.0.0/0            to:192.168.31.159

ping 公共DNS域名解析系統

root@ubuntu:/etc/network# ping 114.114.114.114
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=58 time=5.07 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=58 time=47.3 ms

如果有問題 可以traceroute 公司運維大神教我另外一個指令

mtr 114.114.114.114

設置域名解析系統

nameserver 114.114.114.114

大功告成 測試一下

root@ubuntu:/etc/network# ping qq.com
PING qq.com (61.135.157.156) 56(84) bytes of data.
64 bytes from 61.135.157.156: icmp_seq=1 ttl=43 time=73.7 ms
64 bytes from 61.135.157.156: icmp_seq=2 ttl=43 time=63.9 ms

VMware搭建內網並通過iptables實現端口轉發實現聯網