Debian As Router

In this tutorial , we will discuss how we can make our debian machine as a router, provided our machine has alteast two network interfaces. We will demonstrate this using virtual machine.

  1.  create a virtual machine
  2. setup two network interfaces on it.
    1. one as bridged adapter—for accessing working network
    2. one as internal network–for our local network
  3. power on the machine and connect the first interface
    1. apt-get install net-tools   (for ifconfig)
  4. set up second interface as
    1. edit /etc/network/interfaces as
    2. auto enp0s8
    3. iface enp0s8 inet static
      1. address 192.168.7.1
      2. netmask 255.255.255.0
      3. gateway 192.168.7.1
      4. network 192.168.7.0
    4. service networking restart—eventhough fail message try ifconfig
    5. The new interface is assigned an ip address on this network
  5. configure dnsmasq
    1. dnsmasq provides both dns and dhcp functionality
    2. apt-get install dnsmasq.
    3. /etc/dnsmasq.conf
    4. interface=enp0s8  —-interface on which dhcp server should listen
    5. dhcp-range  192.168.7.50, 192.168.7.150,12h—-beg,end,lease valid time
    6. service dnsmasq restart.
  6. setup client
    1. create another virtual machine with one interface
    2. set interface to local network
    3. power on the machine
    4. connect to network and check the ip you got.
    5. dns server is set as the router …which copies from 8.8.8.8
    6. client can now connect to any machine in local network , that we created.
    7. but not to external network.
  7. Enable ip forwarding in router from one interface to another
    1. echo 1 > /proc/sys/net/ipv4/ip_forward
    2. iptables –table nat –append POSTROUTING –out-interface enp0s3 -j MASQUERADE
    3. //specifies translate the address from which the request from local network comes to the router address–after routing to interface enp0s3—masquerading
    4. iptables –append FORWARD –in-interface enp0s8 -j ACCEPT
    5. //accept all request from interface enp0s8 and forward it.
    6. service networking restart to reflect the changes
  8. go to localmachin
    1. ping 192.168.7.1
    2. ping 192.168.1.1
    3. ping google.com
    4. access the network

Leave a comment