How to set up a linux server as a router

Motivation:

I want to use my linux server instead of the average wireless router for several reasons

  1. I want to learn how to set up a more complete server on linux
  2. I don't want to have a modem, connected to a router, connected to a network switch
  3. I am sick and tired of having to unplug my router every 10 days because it just hangs
  4. I am sick and tired of buying routers only to realize they are missing something crucial, like port forwarding or static ip addressing (dhcp)

Set up:

Ultimately, the connection will come into a modem, and straight into my server through eth0, then eth1 will output to a network switch which all other client computers will connect to via ethernet cables (forget wireless for the moment). Currently, however, I am in an office building, and I have the connection coming into a modem, which goes into a router, which goes into a network switch, which then goes into eth0 as specified above.

Current Tutorials:

I looked at some tutorials (Ubuntu tutorial is the best one), and I have looked at some of the router questions here (ie. this one), but they all gloss over several key concepts, like:

  • What is eth1's relation to eth0? In /etc/network/interfaces do I have to tell eth1 to use eth0 as the network (generally it is the actual physical router address)?
  • Do I have to do anything to instruct eth1 to take the internet that comes into eth0 and pass it onto whoever wants it in the network switch?

Current Approach:

Here is my /etc/network/interfaces file on the server:

iface lo inet loopback

auto eth0
   iface eth0 inet static
   address 192.168.1.70
   netmask 255.255.255.0
   broadcast 192.168.1.255
   network 192.168.1.0
   gateway 192.168.1.1
   dns-nameservers 8.8.8.8

auto eth1
   # iface eth1 inet dhcp
   iface eth1 inet static
   address 192.168.7.0
   netmask 255.255.255.0
   broadcast 192.168.1.255
   network 192.168.1.0

And ifconfig tells me that both NICs are working fine:

eth0      Link encap:Ethernet  HWaddr 20:cf:30:55:a0:5f  
          inet addr:192.168.1.70  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::22cf:30ff:fe55:a05f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11297 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16639 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:948633 (948.6 KB)  TX bytes:1274685 (1.2 MB)

eth1      Link encap:Ethernet  HWaddr 00:11:95:f7:f4:6d  
          inet addr:192.168.7.0  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::211:95ff:fef7:f46d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:243 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3231 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:29934 (29.9 KB)  TX bytes:213055 (213.0 KB)
          Interrupt:21 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:5348 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5348 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:470737 (470.7 KB)  TX bytes:470737 (470.7 KB)

wlan0     Link encap:Ethernet  HWaddr bc:f6:85:f8:70:5c  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

And here is what route -n returns on the server:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1

Then on the client I have

auto lo
iface lo inet loopback
iface eth0 inet dhcp

But it is not being assigned an ip address.

EDIT: Here is the isc-dhcp-server configuration file located at /etc/dhcp3/dhcpd.con which I copied mostly from this site.

# Sample /etc/dhcpd.conf                                                                                                                                                  
# (add your comments here) 
default-lease-time 600;
max-lease-time 7200;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option routers 192.168.1.254;
option domain-name-servers 192.168.1.1, 192.168.1.2;
option domain-name "mydomain.example";

subnet 192.168.7.0 netmask 255.255.255.0 {
   range 192.168.7.10 192.168.7.25;
}  

EDIT: Output of sudo iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
LOG        all  --  anywhere             anywhere             LOG level warning

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Question:

What crucial steps/components am I missing in this setup?


I have to go like Jack the ripper due several missing things that you have:

  1. If you client will use DHCP to get the IP's you need a DHCP server.

    iface eth0 inet dhcp

    In the clients this indicates that they will get their IP's from a DHCP server, if you didn't setup a DHCP server, either you should use fixed IP's or install a DHCP server.

  2. You lack of DNS servers configured in the clients. Either due the lack of DHCP server, or you may want to use a local DNS server for all your network.

  3. You didn't offered the iptables rules (the output of sudo iptables -L) but I could guess that you didn't activated the Masquerade rules, nor IP forwarding as described.

  4. The IP address of eth1 is not recommended. Any IP ended in 0 are typically the network itself, and most routers/firewall just get confused when these are used. Change it to 192.168.7.1 and you will mostly fine.

  5. Your broadcast value in the eth1 interface is not correct. Is sending packages to nowhere. The correct value (taking into account other values of the interface) is 192.168.7.255.

  6. Your options in the DHCP server are vicious. The ARP packages to your router will never reach. This is what you should have:

    default-lease-time 600;
    max-lease-time 7200;
    option subnet-mask 255.255.255.0;
    option broadcast-address 192.168.7.255;
    option routers 192.168.7.1; ## This should be the same value of the step 4
    option domain-name-servers 8.8.8.8;
    
    subnet 192.168.7.0 netmask 255.255.255.0 {
        range 192.168.7.10 192.168.7.25;
    }

Follow these and most likely you will have your router working.


Braiam answered my question, but I thought it would be helpful to put a thorough walkthrough here. Please update this if I have made any mistakes.

First make sure you have two ethernet cards (NICs) and update the /etc/network/interfaces file as such (do not mistake this for the /etc/networks file).

iface lo inet loopback                                                                                                                    

auto eth0
   iface eth0 inet static
   address 192.168.1.70
   netmask 255.255.255.0
   broadcast 192.168.1.255
   network 192.168.1.0
   gateway 192.168.1.1
   dns-nameservers 8.8.8.8

auto eth1
   iface eth1 inet static
   address 192.168.7.1
   netmask 255.255.255.0
   broadcast 192.168.7.255
   network 192.168.1.0

To find your gateway, broadcast and network, follow these instructions.

Next, go into the client and edit the /etc/network/interface (again, not /etc/networks) file for static ip first, to make sure that at least the NIC card is working.

iface eth0 inet static
address 192.168.7.75
netmask 255.255.255.0
network 192.168.7.0
broadcast 192.168.7.255
gateway 192.168.7.1

Change the values to match up with the above values. If it works, great, then use the instructions here but follow them exactly, as there are several dhcp files so don't mistake the folder /etc/dhcp with /etc/dhcp3 and so on.