Router installation
Introduction
In this documentation, we will explain how to set a router serving two different networks.
The router will not only serve as DHCP server and router, but also as a firewall and a logging facility for the two subordinated networks.
We will then have three networks: the WAN, the LAN1 and the LAN2.
We will install Debian 12 bookworm in it due to its reliability.
Requirements
- a computer with three network interfaces at least
- 20 GB of hard drive minimum
- 1 GB of RAM minimum
- an
iso imageofDebian 12 bookworm
Installation
Initial setup
-
BOOT: we begin the installation booting with the
Debian 12 bookworkiso image (it can be thenetinstallone if you prefer).
As the
routerwill only be a server, we will not need any graphical interface, and it will be the same for the installer.
-
INSTALL: choose the
Installoption instead of theGraphical install. -
LANGUAGE: choose the
Englishlanguage. -
TIMEZONE: choose
other, thenEurope, and finallyFrance. -
LOCALE: choose the locale
United States en_US.UTF-8 -
KEYBOARD: choose the keyboard you prefer (
French, or any other). -
HOSTNAME: type the hostname
router. -
DOMAIN: leave the domain name blank.
-
ROOT: enter a good
rootpassword (make sure to follow the conventions of your enterprise) and confirm it. -
USER: create a new user and name it
routeror the name of your choice (according to your conventions). -
Enter a good password respecting the conventions of your enterprise and confirm it.
-
DISK PARTITIONING: choose the
manual partitioning.
Partitions
- For the manual partitioning, first create the
bootpartition, and then create the lvmvolume groupvg0, in which you will create all thelvm partitions.
| Partition name | Type | Mount point | Allocated space |
|---|---|---|---|
| boot | ext4 | /boot | 1GB |
| root | lvm | / | 3GB |
| tmp | lvm | /tmp | 1GB |
| var | lvm | /var | 5GB |
| var_log | lvm | /var/log | 1GB |
| usr | lvm | /usr | 3GB |
| home | lvm | /home | 1GB |
| swap | lvm | swap type: no mount point | 1GB |
- Accept and apply the partitioning.
Last steps
-
MIRROR: to the
Continue without a network mirror?question, answerNo. - Choose the first mirror based in
Franceif your server is located there (it should be thedeb.debian.org). -
PROXY / PACKAGE SURVEY: leave the
proxyoption blank and do not accept the package survey. -
SOFTWARE SELECTION: as said beforehand, this server will not need any
graphicalenvironment, so only check theSSH Serverandstandard system utilities. -
GRUB: accept to install
grub-loader, it will let your computer boot without problem ondebian.
WARNING: if you don't acceptgrub, you will not be able to turn your system on, so it's a crucial step.
- Select the
sdadisk (it should be the one you used for the partitioning). - FINISH: click on continue when the installation is finished.
Configuration
In this section, we will be logged in with the
routeraccount.
Install packages
- Update your system and install the necessary packages:
apt-get -y update
If you have an issue with this command about the
CDROM, you need to edit the/etc/apt/sources.listwith the editor of your choice and comment the line beginning withdeb cdrom. Rerun theupdatecommand after modifying this file.
apt-get -y install curl sudo net-tools tcpdump rsync
You can also install other packages like
vim psmisc git.
apt-get -y install dnsmasq rsyslog logrotate iptables iptables-persistent
- The
iptables-persistentinstallation should ask you if you want to save theipv4andipv6configurations. ChooseNo.
sudo configuration
- To set
sudoup for yournon-root User, run the following command:
visudo
- Then uncomment the line
#%sudo ALL=(ALL:ALL) ALLremoving the#character at the beginning of the line, then pressCtrl + Xto save,Yto confirm andEnterto overwrite the config file. - Now all users that are in the
sudogroup will be able to act asrootusing the commandsudo, but your user may not be part of it yet. - To add a specific user to the
sudogroup, run the following command (replacerouterwith the user you want):
usermod -aG sudo router
Network
Here we will configure the network interfaces of the router.
- Get the names of the
network interfacesusing theip acommand (there should be at least four, including theloopback). - Open the
/etc/network/interfacesfile with your favorite text editor (vim,vi,nvim,nanoor any other). - Configure your three interfaces like the following (replace the
INTERFACESandADDRESSESwith the corresponding ones):
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# Other network interfaces
allow-hotplug <WAN-INTERFACE>
iface <WAN-INTERFACE> inet dhcp
auto <LAN1-INTERFACE>
iface <LAN1-INTERFACE> inet static
address <LAN1-ADDRESS>
auto <LAN2-INTERFACE>
iface <LAN2-INTERFACE> inet static
address <LAN2-ADDRESS>
The
WANinterface will then be set todhcpand the twoLANinterfaces will bestatic.
-
DNS Entries: to add specificDNSentries in yourrouter, you can edit the/etc/hostsfile like in the example below:
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
# WWW network
192.168.56.10 router
192.168.56.80 www
# SQL network
172.16.13.10 router
172.16.13.54 sql
You just need to add a pair of
IP address-hostnameon a blank line, separated by spaces.
DHCP
-
Static DHCP leases: to set staticDHCPleases, you need to create a new file named/etc/ethers. Here is an example:
## Static DHCP leases
# SQL server
bc:24:11:cf:34:b2 172.16.13.54
# WWW server
bc:24:11:2c:a7:2d 192.168.56.80
This file takes
MAC address-IP addresspairs, so that a specificMAC addresswill take the specifiedIP addressfromdnsmasq.
- Enable the
read-ethersoption in/etc/dnsmasq.conf: open the file with your text editor and uncomment the#read-ethersline removing the leading#character:
read-ethers
- Set the
DHCP rangesyou want to use in the same/etc/dnsmasq.conffile. Example with networks192.168.56.0/24and172.16.13.0/24:
# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
dhcp-range=192.168.56.50,192.168.56.100,6h
dhcp-range=172.16.13.50,172.16.13.100,6h
- Restart the
dnsmasqservice with that command:
service dnsmasq restart
Routing
- Enable the
forwardingfromsysctl: open/etc/sysctl.confwith your text editor and uncomment the following line (removing the leading#character):
net.ipv4.ip_forward=1
Setting the iptables rules
- To set the
iptablesrules, you need to have clear which connections you want to allow, depending on the services present in your networks. Here is an example using aWebServerand aPostgreSQL Serverin two differentLANnetworks.
"$WAN","$LAN1"and"$LAN2"correspond to the network interfaces ofrouter.
"$WEB_SERVER"and"$PSQL_SERVER"correspond toIPaddresses belonging to aWebServerand aPostgreSQL Server.
# INPUT policy
# accept all related or already established connections
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# accept HTTP requests from the WAN interface
iptables -A INPUT -i "$WAN" -p tcp -m tcp --dport 80 -j ACCEPT
# accept SSH connections from any interface
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
# accept rsyslog tcp connections from LAN1 & LAN2 interfaces
iptables -A INPUT -i "$LAN1" -p tcp -m tpc --dport 514 -j ACCEPT
iptables -A INPUT -i "$LAN2" -p tcp -m tpc --dport 514 -j ACCEPT
# accept DNS requests from LAN1 & LAN2 interfaces
iptables -A INPUT -i "$LAN1" -p udp -m udp --dport 53 -j ACCEPT
iptables -A INPUT -i "$LAN2" -p udp -m udp --dport 53 -j ACCEPT
# accept DHCP requests from LAN1 & LAN2 interfaces
iptables -A INPUT -i "$LAN1" -p udp -m udp --dport 67 -j ACCEPT
iptables -A INPUT -i "$LAN2" -p udp -m udp --dport 67 -j ACCEPT
# FORWARD policy
# Forward policy set to DROP
iptables --policy FORWARD DROP
# allow related or already established connections
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
# allow forwarding from WAN to LAN1 (if LAN1 needs connections from outside, like a webserver)
iptables -A FORWARD -i "$WAN" -o "$LAN1" -j ACCEPT
# allow forwarding from LAN1 & LAN2 to WAN (to have access to internet from the LANs networks)
iptables -A FORWARD -i "$LAN1" -o "$WAN" -j ACCEPT
iptables -A FORWARD -i "$LAN2" -o "$WAN" -j ACCEPT
# allow all ICMP requests from any interface
iptables -A FORWARD -p icmp -j ACCEPT
# allow a specific connection to an IP address in a particular port (example with a PSQL Server)
iptables -A FORWARD -d "$PSQL_SERVER"/32 -p tcp -m tcp --dport 5432 -j ACCEPT
# NAT policy
# enable HTTP DNAT from WAN to a specific IP address (example with a WebServer)
iptables -t nat -A PREROUTING -i "$WAN" -p tcp -m tcp --dport 80 -j DNAT --to-destination "$WEB_SERVER"
# enable NAT from the LANs networks to the WAN network
# necessary to have internet connection working in the LANs networks
iptables -t nat -A POSTROUTING -o "$WAN" -j MASQUERADE
Making the rules persistent
- Save the configuration to make it persistent with the following command:
iptables-save > /etc/iptables/rules.v4
Logging
- Enable the
rsyslogtcp receptionin the/etc/rsyslog.conffile. Open the file in your text editor and uncomment the following lines removing the leading#character:
module(load="imtcp")
input(type="imtcp" port="514")
- Disable the default
rsyslogoutput file to use a template in the same/etc/rsyslog.conffile:
- Comment the last line of the following part
#
# Log anything besides private authentication messages to a single log file
#
#*.*;auth,authpriv.none -/var/log/syslog
- Add the following lines at the end of the file:
#
# Remote logs
#
template(name="RemoteLogs" type="string" string="/var/log/remote/%HOSTNAME%.log")
*.* ?RemoteLogs
- Configure
logrotate:
- Modify the
/etc/logrotate.confto enable thedateextoption (to use the date in the names of the log files) and uncomment the following line:
dateext
- Create a new file
/etc/logrotate.d/remotewith the following content:
/var/log/remote/*.log {
weekly
rotate 52
compress
delaycompress
missingok
dateext
dateformat .%Y-%m-%d
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
- Then restart the two services:
service rsyslog restart
service logrotate restart
End
- To be sure to have all configurations taken into account,
reboottherouterusing that command:
reboot
No Comments