Difference between revisions of "Router"

From ArchWiki
Jump to navigation Jump to search
m (DNS and DHCP: Changed the comment on DHCP range to reflect the actual value.)
m (Post-Installation: Added reference to sudo configuration guide)
Line 37: Line 37:
  # pacman -S sudo
  # pacman -S sudo
Now, add a normal user. Be sure to add the user to the '''wheel''' group. This will allow the user to use '''sudo'''. Logging in as root is unsafe, it is much better to use sudo.
Now, add a normal user. Be sure to add the user to the '''wheel''' group and make the necessary changes to /etc/sudoers as described in [http://wiki.archlinux.org/index.php/Sudo#Configuration Sudo#Configuration]. This will allow the user to use '''sudo'''. Logging in as root is unsafe, it is much better to use sudo.
After sudo is installed and configured, it is possible to lock to root account and prevent login directly to root.
After sudo is installed and configured, it is possible to lock to root account and prevent login directly to root.

Revision as of 10:08, 20 September 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

This article is a tutorial for turning a computer into an internet gateway/router. It focuses on security, since the gateway is connected directly to the Internet. It should not run any services available to the outside world. Towards the LAN, it should only run gateway specific services. It should not run httpd, ftpd, samba, nfsd, etc. as those belong on a server in the LAN as they introduce security flaws.

This article does not attempt to show how to set up a shared connection between 2 PCs using cross-over cables. For a simple internet sharing solution, see Internet Share.

Hardware Requirements

  • At least 1 GB of hard drive space. The base install will take up around 500MB of space and if you want to use a caching web proxy, you will need to reserve space for the cache as well.
  • At least two physical network interfaces: a gateway connects two networks with each other. You will need to be able to connect those networks to the same physical computer. One interface must connect to the external network, while the other connects to the internal network.
  • A hub, switch or UTP cable: You need a way to connect the other computers to the gateway


Conventions in this guide will be to use non-realistic interface names, to avoid confusion about which interface is which.

  • intern0: the network card connected to the LAN. On an actual computer it will probably have the name eth0, eth1, etc.
  • extern1: the network card connected to the external network (or WAN). It will probably have the name eth0, eth1, etc.


Note: For a full installation guide, see the Official Arch Linux Install Guide.

A fresh install of Arch Linux is the easiest to start from, as no configuration changes have been made and there is a minimal amount of packages installed. This is helpful when attempting to reduce security risk.


For security purposes, /var, /tmp and /home should be separate from the / partition. This prevents disk space from being completely used up by log files, daemons or the unprivileged user. It also allows different mount options for those partitions. If you have already partitioned your drive, the gparted livecd can be used to resize, move, or create new partitions.

Your home and root partitions can be much smaller than a regular install since this is not a desktop machine. /var should be the largest partition - it is where databases, logs and long-term caches are stored. If you have a lot of RAM, mounting /tmp as tmpfs is a good idea, so making a disk partition for it during the initial install is unnecessary.


After installation boot Arch and upgrade all the packages to their latest version:

# pacman -Syu

Install sudo:

# pacman -S sudo

Now, add a normal user. Be sure to add the user to the wheel group and make the necessary changes to /etc/sudoers as described in Sudo#Configuration. This will allow the user to use sudo. Logging in as root is unsafe, it is much better to use sudo.

After sudo is installed and configured, it is possible to lock to root account and prevent login directly to root.

# passwd -l root

Network interface configuration

Persistent naming

When you let udev handle loading the modules, you will notice your NIC's switch names: one boot your LAN NIC is eth0, the other boot it is eth1.

To fix this problem, see here.

IP configuration

Now you will need to configure the network interfaces. The best way to so do is using netcfg profiles, instead of the regular network daemon. You will need to create two profiles.

  • /etc/network.d/extern0-profile
DESCRIPTION='Public Interface.'
  • /etc/network.d/intern0-profile
DESCRIPTION='Private Interface.'
Note: The example configuration above assumes a full subnet. If you are building the gateway for a small amount of people, you will want to change the netmask and broadcast to accommodate a smaller range.

Next up is to set up the interfaces in /etc/rc.conf.

  • Define the profiles
NETWORKS=(extern0-profile intern0-profile)
  • Replace the "network" daemon with the netcfg one: "net-profiles"
DAEMONS=( ... net-profiles ... )

ADSL connection

Using rp-pppoe, we can connect an ADSL modem to the extern1 of the firewall and have Arch manage the connection. Make sure you put the modem in bridged mode though, otherwise the modem will act as a router too.

# pacman -S rp-pppoe

Configuration: rp-pppoe


The questions are all documented. You can select "no firewall" because we will let Shorewall / iptables handle that part.


We will use dnsmasq, a DNS and DHCP daemon for the LAN. It was specifically designed for small sites.

First, install dnsmasq:

# pacman -S dnsmasq

Now, dnsmasq needs to be configured. To do this:

Edit /etc/dnsmasq.conf and add the following lines

interface=intern1 # make dnsmasq listen for requests only on intern1 (our LAN)
expand-hosts      # add a domain to simple hostnames in /etc/hosts
domain=foo.bar    # allow fully qualified domain names for DHCP hosts (needed when
                  # "expand-hosts" is used)
dhcp-range=,,,1h # defines a DHCP-range for the LAN: 
                  # from to .255 with a subnet mask of and a
                  # DHCP lease of 1 hour (change to your own preferences)

Somewhere below, you will notice you can also add "static" DHCP leases, i.e. assign an IP-address to the MAC-address of a computer on the LAN. This way, whenever the computer requests a new lease, it will get the same IP. That is very useful for network servers with a DNS record. You can also deny certain MAC's from getting an IP. Evil!! ^_^ Now start dnsmasq

# /etc/rc.d/dnsmasq start

and add the daemon to the DAEMONS list in /etc/rc.conf.

Connection sharing

Time to tie the two network interfaces to each other.


Simple stateful firewall documents the setup of an iptables firewall and NAT.


Shorewall, an iptables frontend, can be used as an easier alternative.

# pacman -S shorewall

Shorewall configuration

Time to configure Shorewall! Open its config file in /etc/shorewall/shorewall.conf and start editing. The file is very well documented.

IP_FORWARDING=On : it is a gateway, remember! ;)
STARTUP_ENABLED=Yes # when you are done editing

After installing shorewall, run

$ pacman -Ql shorewall | grep Sample

to see where the sample files are. cd into the directory "two-interfaces" and copy the contents to the /etc/shorewall/ directory. Now use Shorewall's guide to set up the files correctly.

Read the document carefully. Take special care to change eth0 and eth1 (or ppp0 in if you are using PPPoE where appropriate in your config files as the Shorewall guide uses different names for the interfaces. When you have followed it thoroughly, make the following changes:

  • /etc/shorewall/interfaces : add "dhcp" to the loc line to allow computers on the LAN to make use of our DHCP server
  • /etc/shorewall/rules : add
ACCEPT        loc        $FW        TCP      2367

but change 2367 into whatever port you have your SSH server listening on.

Finally, run

# /etc/rc.d/shorewall start

From here on, the Arch box is operational. Connect a hub or switch to intern0 and a computer to the LAN to test it.

Port forwarding (DNAT)
  • /etc/shorewall/rules : here is an example for a webserver on our LAN with IP You can reach it on port 5000 of our "external" IP.
DNAT        net        loc:        tcp        5000


Now that the installation has been performed, it is necessary to remove as many packages as possible. Since we are making a gateway, keeping unneeded packages only "bloats" the system, and increases the number of security risks.

First, check for obsolete/deprecated packages (likely after a fresh install and massive series of updates):

$ pacman -Qm

Review the list of explicitly installed packages that are not dependencies and remove any that are unneeded. Having only needed packages installed is an important security consideration.

$ pacman -Qet

Completely remove the packages you do not need along with their configuration files and dependencies:

# pacman -Rsn package1 package2 package3


You should review the logrotate configuration to make sure the box is not brought down by lack of diskspace due to logging.

Logrotate is installed by default, so you will not have to install it.

Optional additions


The above configuration of shorewall does not include UPnP support. Use of UPnP is discouraged as it may make the gateway vulnerable to attacks from within the LAN. However, some applications such as MSN require this to function correctly.

Read the Shorewall guide on UPnP for more information

Remote administration

OpenSSH can be used to administer your router remotely. This is useful for running it "headless" (no monitor or input devices).

Caching web proxy

See Squid or Polipo for the setup of a web proxy to speed up browsing and/or adding an extra layer of security.

Time server

To use the router as a time server, see Network Time Protocol.

Then, configure shorewall or iptables to allow NTP traffic in and out.

Content filtering

Install and configure DansGuardian if you need a content filtering solution.

Traffic shaping

Traffic shaping is very useful, especially when you are not the only one on the LAN. The idea is to assign a priority to different types of traffic. Interactive traffic (ssh, online gaming) probably needs the highest priority, while P2P traffic can do with the lowest. Then there is everything in between.

Traffic shaping with shorewall

Read Shorewall's Traffic Shaping/Control guide.

Here is my config as an example:

  • /etc/shorewall/tcdevices : here is where you define the interface you want to have shaped and its rates. I have got a ADSL connection with a 4MBit down/256KBit up profile.
ppp0        4mbit        256kbit 
  • /etc/shorewall/tcclasses : here you define the minimum (rate) and maximum (ceil) throughput per class. You will assign each one to a type of traffic to shape.
# interactive traffic (ssh)
ppp0            1       full    full    0
# online gaming
ppp0            2       full/2  full    5
# http
ppp0            3       full/4  full    10
# rest
ppp0            4       full/6  full    15              default
  • /etc/shorewall/tcrules : this file contains the types of traffic and the class it belongs to.
1       tcp     ssh
2       udp     27000:28000
3       tcp     http
3       tcp     https
4       all

I have split it up my traffic in 4 groups:

  1. interactive traffic or ssh: although it takes up almost no bandwidth, it is very annoying if it lags due to leechers on the LAN. This get the highest priority.
  2. online gaming: needless to say you ca not play when your ping sucks. ;)
  3. webtraffic: can be a bit slower
  4. everything else: every sort of download, they are the cause of the lag anyway.

Intrusion detection and prevention with snort

See Snort.

See also