Ubuntu - Create Gatway/Router PC with Single NIC

Previously, I have shown how one can turn your computer with two NICS into a gateway for a subnet. It is possible to do this without the use of a second network card by first creating a virtual network interface as shown below:

    Edit your interfaces file to add your virtual interface. This is done by appending a :0 to the end of whichever NIC you want to put the virtual interface on. If you want to add more than one virtual interface, just increment the number after the colon, such as :1 :2 etc.

    Add the following lines:
    auto eth0:0
    iface eth0:0 inet static
        address 10.0.0.1
        netmask 255.0.0.0
        gateway 10.0.0.1
    
    Restart your networking service:
    sudo service networking restart
    Now that your virtual network device is set up with a static ip, you need to allow packets recieved on this interface, to be forwarded through your normal interface as shown below (these commands need to run each time the computer/server is restarted):
    sudo iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
    sudo iptables --append FORWARD --in-interface eth0:0 -j ACCEPT
    edit the /etc/sysctl.conf file and uncomment the line shown here (remove the # at the front)
    #net.ipv4.ip_forward=1
    Run this command:
    echo 1 > /proc/sys/net/ipv4/ip_forward
    To allow those changes to the sysctl file to take effect, run the following command:
    sudo sysctl -p
    Thats it! Any server/computer set with its gateway to 192.168.1.1 (what we set the virtual interface to) will now route its traffic through this computer in order to access the internet (wlan).

No comments:

Post a Comment