https://wiki.archlinux.org/api.php?action=feedcontributions&user=Roliver&feedformat=atomArchWiki - User contributions [en]2024-03-29T15:09:05ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Router&diff=151707Router2011-08-12T23:52:50Z<p>Roliver: Corrected structure of sentance.</p>
<hr />
<div>[[Category:Networking (English)]]<br />
[[Category:Security (English)]]<br />
{{i18n|Router}}<br />
<br />
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 shouldn't 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.<br />
<br />
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]].<br />
<br />
==Hardware Requirements==<br />
* 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.<br />
* 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.<br />
* A hub, switch or UTP cable: You need a way to connect the other computers to the gateway<br />
<br />
==Conventions==<br />
Conventions in this guide will be to use non-realistic interface names, to avoid confusion about which interface is which.<br />
<br />
* '''intern0''': the network card connected to the LAN. On an actual computer it will probably have the name eth0, eth1, etc.<br />
* '''extern1''': the network card connected to the external network (or WAN). It will probably have the name eth0, eth1, etc.<br />
<br />
==Installation==<br />
{{Note | For a full installation guide, see the [[Official Arch Linux Install Guide]].}}<br />
<br />
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.<br />
<br />
===Partitioning===<br />
<br />
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 [http://gparted.sourceforge.net/ gparted livecd] can be used to resize, move, or create new partitions.<br />
<br />
Your home and root partitions can be much smaller than a regular install since this isn't a desktop machine. /var should be the largest partition - it's 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.<br />
<br />
===Post-Installation===<br />
<br />
After installation boot Arch and upgrade all the packages to their latest version:<br />
# pacman -Syu<br />
<br />
Install [[sudo]]:<br />
# pacman -S sudo<br />
<br />
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.<br />
<br />
After sudo is installed and configured, it's possible to lock to root account and prevent login directly to root.<br />
<br />
# passwd -l root<br />
<br />
==Network interface configuration==<br />
<br />
===Persistent naming===<br />
When you let udev handle loading the modules, you'll notice your NIC's switch names: one boot your LAN NIC is eth0, the other boot it's eth1.<br />
<br />
To fix this problem, see [[Udev#Mixed_Up_Devices.2C_Sound.2FNetwork_Cards_Changing_Order_Each_Boot|here]].<br />
<br />
===IP configuration===<br />
Open /etc/rc.conf once more and scroll down to the network config section. Here's where you define how your network cards should obtain their IP. The LAN card will have a static IP, I'm going with 10.0.0.1 because it's easy to type. I'm building a gateway for a small student home with 4 rooms so I'm keeping the subnet fairly small: 4 bits allow 16 IP's. <br />
<br />
16 - 3 IP's: <br />
* one for the network address: 10.0.0.0<br />
* the gateway: 10.0.0.1 <br />
* and the broadcast address: 10.0.0.15 leaves 13 IP's for computers on the LAN. This translates into:<br />
lo="lo 127.0.0.1"<br />
intern0="intern0 10.0.0.1 netmask 255.255.255.240 broadcast 10.0.0.15"<br />
extern1="dhcp"<br />
<br />
{{Note|The new interface method does not work with this because it only supports single interfaces for the time being. However, the old method is still supported, and should still be used as shown.}}<br />
<br />
==ADSL connection==<br />
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.<br />
# pacman -S rp-pppoe<br />
<br />
===Configuration: rp-pppoe===<br />
/usr/sbin/pppoe-setup <br />
The questions are all documented. You can select "no firewall" because we'll let Shorewall / iptables handle that part.<br />
<br />
==DNS and DHCP==<br />
We'll use [[dnsmasq]], a DNS and DHCP daemon for the LAN. It was specifically designed for small sites.<br />
<br />
First, install '''dnsmasq''':<br />
# pacman -S dnsmasq<br />
<br />
Now, dnsmasq needs to be configured. To do this:<br />
<br />
Edit /etc/dnsmasq.conf and add the following lines<br />
interface=intern1 # make dnsmasq listen for requests only on intern1 (our LAN)<br />
expand-hosts # add a domain to simple hostnames in /etc/hosts<br />
domain=foo.bar # allow fully qualified domain names for DHCP hosts (needed when<br />
# "expand-hosts" is used)<br />
dhcp-range=10.0.0.2,10.0.0.14,255.255.255.240,1h # defines a DHCP-range for the LAN: <br />
# from 10.0.0.2 to .14 with a subnet mask of 255.255.255.240 and a<br />
# DHCP lease of 1 hour (change to your own preferences)<br />
<br />
Somewhere below, you'll 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'll get the same IP. That's very useful for network servers with a DNS record. You can also deny certain MAC's from getting an IP. Evil!! ^_^<br />
Now start dnsmasq<br />
# /etc/rc.d/dnsmasq start<br />
and add the daemon to the DAEMONS list in /etc/rc.conf.<br />
<br />
==Connection sharing==<br />
<br />
Time to tie the two network interfaces to each other.<br />
===iptables===<br />
[[Simple stateful firewall]] documents the setup of an [[iptables]] firewall and NAT.<br />
<br />
===Shorewall===<br />
Shorewall, an iptables frontend, can be used as an easier alternative.<br />
<br />
# pacman -S shorewall<br />
<br />
====Shorewall configuration====<br />
<br />
Time to configure Shorewall! Open its config file in /etc/shorewall/shorewall.conf and start editing. The file is very well documented.<br />
SUBSYSLOCK=/var/run<br />
IP_FORWARDING=On : it's a gateway, remember! ;)<br />
STARTUP_ENABLED=Yes # when you're done editing<br />
<br />
After installing shorewall, run<br />
$ pacman -Ql shorewall | grep Sample<br />
to see where the sample files are. cd into the directory "two-interfaces" and copy the contents to the /etc/shorewall/ directory.<br />
Now use [http://www.shorewall.net/two-interface.htm Shorewall's guide] to set up the files correctly.<br />
<br />
Read the document carefully. Take special care to '''change eth0 and eth1 (or ppp0 in if you're using PPPoE where appropriate''' in your config files as the Shorewall guide uses different names for the interfaces. When you've followed it thoroughly, make the following changes:<br />
* /etc/shorewall/interfaces : add "dhcp" to the ''loc'' line to allow computers on the LAN to make use of our DHCP server<br />
* /etc/shorewall/rules : add<br />
ACCEPT loc $FW TCP 2367<br />
but change 2367 into whatever port you have your SSH server listening on.<br />
<br />
Finally, run<br />
# /etc/rc.d/shorewall start<br />
<br />
From here on, the Arch box is operational. Connect a hub or switch to intern0 and a computer to the LAN to test it.<br />
<br />
=====Port forwarding (DNAT)=====<br />
* /etc/shorewall/rules : here's an example for a webserver on our LAN with IP 10.0.0.85. You can reach it on port 5000 of our "external" IP.<br />
DNAT net loc:10.0.0.85:80 tcp 5000<br />
<br />
==Cleanup==<br />
<br />
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.<br />
<br />
First, check for obsolete/deprecated packages (likely after a fresh install and massive series of updates):<br />
<br />
$ pacman -Qm<br />
<br />
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.<br />
<br />
$ pacman -Qet<br />
<br />
Completely remove the packages you don't need along with their configuration files and dependencies:<br />
<br />
# pacman -Rsn package1 package2 package3<br />
<br />
== Logrotate ==<br />
<br />
You should review the [[logrotate]] configuration to make sure the box isn't brought down by lack of diskspace due to logging.<br />
<br />
Logrotate is installed by default, so you won't have to install it.<br />
<br />
==Optional additions==<br />
<br />
===Remote administration===<br />
<br />
[[SSH|OpenSSH]] can be used to administer your router remotely. This is useful for running it "headless" (no monitor or input devices).<br />
<br />
=== Caching web proxy ===<br />
<br />
See [[Squid]] or [[Polipo]] for the setup of a web proxy to speed up browsing and/or adding an extra layer of security.<br />
<br />
=== Time server ===<br />
To use the router as a time server, see [[Network Time Protocol]].<br />
<br />
Then, configure shorewall or iptables to allow NTP traffic in and out.<br />
<br />
=== Content filtering ===<br />
<br />
Install and configure [[DansGuardian]] if you need a content filtering solution.<br />
<br />
=== Traffic shaping ===<br />
<br />
Traffic shaping is very useful, especially when you're 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's everything in between.<br />
<br />
==== Traffic shaping with shorewall ====<br />
<br />
Read [http://www.shorewall.net/traffic_shaping.htm Shorewall's Traffic Shaping/Control] guide.<br />
<br />
Here's my config as an example:<br />
* /etc/shorewall/tcdevices : here's where you define the interface you want to have shaped and its rates. I've got a ADSL connection with a 4MBit down/256KBit up profile.<br />
ppp0 4mbit 256kbit <br />
* /etc/shorewall/tcclasses : here you define the minimum (rate) and maximum (ceil) throughput per class. You'll assign each one to a type of traffic to shape.<br />
# interactive traffic (ssh)<br />
ppp0 1 full full 0<br />
# online gaming<br />
ppp0 2 full/2 full 5<br />
# http<br />
ppp0 3 full/4 full 10<br />
# rest<br />
ppp0 4 full/6 full 15 default<br />
* /etc/shorewall/tcrules : this file contains the types of traffic and the class it belongs to.<br />
1 0.0.0.0/0 0.0.0.0/0 tcp ssh<br />
2 0.0.0.0/0 0.0.0.0/0 udp 27000:28000<br />
3 0.0.0.0/0 0.0.0.0/0 tcp http<br />
3 0.0.0.0/0 0.0.0.0/0 tcp https<br />
4 0.0.0.0/0 0.0.0.0/0 all<br />
I've split it up my traffic in 4 groups: <br />
# interactive traffic or ssh: although it takes up almost no bandwidth, it's very annoying if it lags due to leechers on the LAN. This get the highest priority.<br />
# online gaming: needless to say you can't play when your ping sucks. ;)<br />
# webtraffic: can be a bit slower<br />
# everything else: every sort of download, they're the cause of the lag anyway.<br />
<br />
===Intrusion detection and prevention with snort===<br />
<br />
See [[Snort]].<br />
<br />
==See also==<br />
*[[Simple stateful firewall]]<br />
*[[Internet Share]]</div>Roliver