2016年4月7日 星期四

Raspberry Pi - 建立一個網路路由器 Router

1. 網路架構圖
          ---------------------------------------------
         |        eth0          |         eth1        |
       LAN PC                 Router                WAN PC
  ip: 192.168.1.100    eth0: 192.168.1.1       ip: 10.1.1.100
  gw: 192.168.1.1      eth1: 10.1.1.1          gw: 10.1.1.1
2. 設定網路介面的IP address,eth0為LAN interface,eth1為WAN interface
$ ifconfig eth0 192.168.1.1
$ ifconfig eth1 up
3. 在WAN interface eth1啟動dhcp client去跟外面的dhcp server要IP
$ udhcpc -i eth1
4. 開啟IPv4 forwrding功能
$ sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
5. 開啟iptables NAT功能
$ iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
6. 設定default gateway
$ route add default gw 10.1.1.1 
7. 在LAN interface eth0啟動dhcp server,設定dhcp server的configuration file
example 1:
echo "start     192.168.1.20" > /etc/udhcpd.conf
echo "end       192.168.1.254" >> /etc/udhcpd.conf
echo "interface br0" >> /etc/udhcpd.conf

echo "opt       dns     192.168.1.1 192.168.1.1" >> /etc/udhcpd.conf
echo "option    subnet  255.255.255.0" >> /etc/udhcpd.conf
echo "opt       router  192.168.1.1" >> /etc/udhcpd.conf
echo "opt       wins    192.168.1.1" >> /etc/udhcpd.conf
echo "option    dns     192.168.1.1" >> /etc/udhcpd.conf
echo "option    domain  local" >> /etc/udhcpd.conf
echo "option    lease   864000" >> /etc/udhcpd.conf

echo "static_lease 00:24:BE:7E:AE:C1 192.168.1.100" >> /etc/udhcpd.conf
example 2:
echo "start     192.168.1.20" > /etc/udhcpd.conf
echo "end       192.168.1.254" >> /etc/udhcpd.conf
echo "interface eth0" >> /etc/udhcpd.conf

echo "opt       dns     192.168.1.1 192.168.1.1" >> /etc/udhcpd.conf
echo "option    subnet  255.255.255.0" >> /etc/udhcpd.conf
echo "opt       router  192.168.1.1" >> /etc/udhcpd.conf
echo "opt       wins    192.168.1.1" >> /etc/udhcpd.conf
echo "option    dns     192.168.1.1" >> /etc/udhcpd.conf
echo "option    domain  local" >> /etc/udhcpd.conf
echo "option    lease   864000" >> /etc/udhcpd.conf

echo "static_lease 00:24:BE:7E:AE:C1 192.168.1.100" >> /etc/udhcpd.conf
8. 在LAN interface eth0啟動dhcp server,以供LAN side的主機取得IP
$ mkdir /var/lib/misc/
$ touch /var/lib/misc/udhcpd.leases
$ udhcpd
9. 啟動DNS MASQUERATE
$ dnsmasq

沒有留言:

張貼留言