Difference between revisions of "Router: Basic"

From ArchWiki
Jump to: navigation, search
(DHCP Server: updated directly from dnsmasq archwiki article)
m (Ethernet Device Settings)
Line 41: Line 41:
=Ethernet Device Settings=
=Ethernet Device Settings=
Each ethernet device's IP configuration needs to be set in the system-wide configuration file.
Each ethernet device's IP configuration needs to be set in {{filename|/etc/rc.conf}}:
=== /etc/rc.conf ===
  lan="lan netmask broadcast"
  lan="lan netmask broadcast"

Revision as of 22:10, 28 July 2010


If you'd like to build a router to forward connections to LAN client(s), you'll need the details of creating a basic router. A router is required in order to connect multiple machines to the Internet using the a single global IP address. This is almost always the case for residential Internet access. The Arch Linux machine will fulfill several roles required to connect machines in a local network to the Internet:

  • Firewall - Block unauthorized packets but allow authorized traffic
  • IP Masquerading - Manipulate IP addresses as packets cross between internal network and Internet
  • DHCP Server - Manage IP addresses of machines in the internal network
  • DNS Server - Accept DNS lookups from local machines and forward them to the Internet
  • Gateway - Translate between protocols of the internal network and your Internet Service Provide (optional)


You'll need to have at least two Network Card Interfaces (NIC's) on the computer you plan to use as a router. Once installed see that they are recognized by the kernel:

ifconfig -a

If the NIC(s) don't show up, then either 1) the kernel module (driver) will need be loaded, 2) the kernel will need to be rebuilt with support for the hardware, or 3) the kernel may not have support for the driver yet.

If there is a kernel module for you NIC, the generic Arch Linux kernel will likely have support for it. You can add it by:

modprobe <device-module>

If there is support in the kernel, but not in the Arch kernel take a look at Kernel Compilation with ABS.

Ethernet Device Names

Udev is the device manager for Arch Linux and can be used to manually choose names for each ethernet device. This should be done to make sure that each physical network connection always has the same name, and also for convenience during later configuration steps. Create a Udev rule:


You can easily find the address of an existing device:

udevadm info -a -p /sys/class/net/<device> | grep address

Just use the output as the second field in the rules file. Next time Udev assigns device names it will use these. This article assumes "wan" connects to the Internet and that "lan" connects to the local network.

Ethernet Device Settings

Each ethernet device's IP configuration needs to be set in Template:Filename:

lan="lan netmask broadcast"
INTERFACES=(wan lan)

The device wan will request a dynamic IP address from the ISP. The device lan will use a static IP address. Later on dnsmasq will be configured used to grant dhcp leases to other local machines in the same subnet, i.e. with address in the range (but not because the router has that local address).

LAN Setup

For connecting to/from your LAN client(s), you can have to either add to the router a DHCP server (which will build the LAN client's routes for you) or define a static-route(s) manually.

DHCP Server Setup

Install dnsmasq

# pacman -S dnsmasq

Edit the dnsmasq configuration file Template:Filename:

# Only listen to routers' LAN NIC.  Doing so opens up tcp/udp port 53 to
# localhost and udp port 67 to world:

# dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with
# dynamic interfaces (assigning dynamic ips). Dnsmasq will discard world
# requests to them, but the paranoid might like to close them and let the 
# kernel handle them:

# Dynamic range of IPs to make available to LAN pc

# If you’d like to have dnsmasq assign static IPs, bind the LAN computer's
# NIC MAC address:

If you choose not to bind the interfaces, the domain port will need to be allowed in Template:Filename:

domain ALL : ALLOW


To assign a static-route (for example on a Arch Linux LAN client):

eth0="eth0 netmask broadcast"
gateway="default gw"

Forward Requests

The kernel will need to be told it's allowed to forward packets to/from the LAN clients:

echo 1 > /proc/sys/net/ipv4/ip_forward

To permanently set this, enable ip forwarding in /etc/sysctl.conf:


Redirection of packets to/from the LAN client(s) can be done with iptables.

pacman -S iptables

And add the rule:

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

The rule can be added permanently in /etc/rc.local though you'll probably want to create a bash script for it to build a firewall later. More information about firewalls can be found on Simple stateful firewall HOWTO.