Nov 12, 2011

setting linux as a gateway

Since my school project need to set up an ethernet environment. I need to create an internal network under virtualbox. The first step is to setup the gateway inside a virtualbox. After google for a while, I finally setting up my linux as a gateway.
Environment :
linux distribution: alpine linux 2.3
kernel version:    3.0
virtualbox network adapter setting:
     1. host-only network=> eth0 for internal network.
     2. bridged network => eth1 for internet.
the connection state is like the following figure:
<<internet>>----------<<alpine linux>>------------<<internal>>
            eth1 pppoe                 eth0 NAT

The reason why I use alpine linux instead of other distribution is that it is tiny but contains the utilities that to set up my environment.
alpine linux download link:
alpine linux installation guide:

I'll write down all the steps that I configure the internet.
1. package requirement
2. configure the network interface
3. setting up the iptables
4. enable packet forwarding
 1.package requirement:

rp-pppoe => pppoe client side program
iptables => firewall
ppp  => ppp deamon
that's all. 

2.configure the network interface
   2.1. the internal network:
      configure the /etc/network/interface
      add the following line to the file.
auto eth0
iface eth0 inet static
      the address, netmask, network and broadcast can be changed according to you network setting. 

   2.2.pppoe configuration:
      pppoe-setting => to start pppoe configuration. 
      After setting up type 
      pppoe-connect => connect to the internet via pppoe. 
      You can check the result by typing:
      ifconfig | less

3.setting up the iptables:
   type the following command to set the iptables rules:      
   iptables -A FORWARD -o eth1 -i eth0 -s -m conntrack --ctstate NEW -j ACCEPT
   iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT 
   iptables -A POSTROUTING -t nat -j MASQUERADE

4.enable packet forwarding:
    echo 1 > /proc/sys/net/ipv4/ip_forward
    cat /proc/sys/net/ipv4/ip_forward => check the result.

reference website:

No comments:

Post a Comment