2016年8月2日 星期二

如何建立一個小型的私有網路, 然後透過你建立的 Router 當作它的 gateway 連到外面去

如何建立一個小型的私有網路, 然後透過你建立的 Router 當作它的 gateway 連到外面去

Googledoc

(Edit)

Points

  • 希望內部私有網路上的電腦, 可以透過一台 router, 當作 gateway 連到外面 Internet
  • 建立 Linux Router 當作內部網路的 Gateway, 進行 兩個網路的封包交換
  • 這個 Router 是一個 run 在你機器上的簡單 VM

Procedure

Router 的設定

Step 1:  Router setup for enabling the IP forward function
Ubuntu
Configuration File
[Linux Router]# /etc/sysctl.conf
    net.ipv4.ip_forward=1

Command (setup will lost when system reboot)
[Linux Router]# sudo sysctl -w net.ipv4.ip_forward=1
[Linux Router]# /etc/init.d/networking restart



Step 2: NAT setup (for Lab computer to access Internet)
# Route all packages from 192.168.6.0/24 to the nic of the other site (the other network) (eth0)
# Here, we go to Internet via eth0.

# Setup NAT Rule
. ./nat.sh
nat.sh
sudo iptables -t nat -A POSTROUTING -s 192.168.6.0/24 -o eth0 -j MASQUERADE


Verification


Lab PC in private network
Key Idea:
Gateway => 192.168.6.254   ( Point to your new VM router running on your host)

Linux (reboot will loss the setting)
Step 1: boot up another vm
Step 2: setup the ip to 102.168.6.x
Step 3: setup default gateway to 192.168.6.254
Command:
   sudo route add default gw 192.168.6.254

Step 4: check (Check if the VM can reach google DNS server)
   ping 8.8.8.8



Appendix