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 image
ofDebian 12 bookworm
Installation
Initial setup
-
BOOT: we begin the installation booting with the
Debian 12 bookwork
iso image (it can be thenetinstall
one if you prefer).
As the
router
will only be a server, we will not need any graphical interface, and it will be the same for the installer.
-
INSTALL: choose the
Install
option instead of theGraphical install
. -
LANGUAGE: choose the
English
language. -
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
root
password (make sure to follow the conventions of your enterprise) and confirm it. -
USER: create a new user and name it
router
or 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
boot
partition, and then create the lvmvolume group
vg0, 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
France
if your server is located there (it should be thedeb.debian.org
). -
PROXY / PACKAGE SURVEY: leave the
proxy
option blank and do not accept the package survey. -
SOFTWARE SELECTION: as said beforehand, this server will not need any
graphical
environment, so only check theSSH Server
andstandard 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
sda
disk (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
router
account.
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.list
with the editor of your choice and comment the line beginning withdeb cdrom
. Rerun theupdate
command 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-persistent
installation should ask you if you want to save theipv4
andipv6
configurations. ChooseNo
.
sudo
configuration
- To set
sudo
up for yournon-root User
, run the following command:
visudo
- Then uncomment the line
#%sudo ALL=(ALL:ALL) ALL
removing the#
character at the beginning of the line, then pressCtrl + X
to save,Y
to confirm andEnter
to overwrite the config file. - Now all users that are in the
sudo
group will be able to act asroot
using the commandsudo
, but your user may not be part of it yet. - To add a specific user to the
sudo
group, run the following command (replacerouter
with 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 interfaces
using theip a
command (there should be at least four, including theloopback
). - Open the
/etc/network/interfaces
file with your favorite text editor (vim
,vi
,nvim
,nano
or any other). - Configure your three interfaces like the following (replace the
INTERFACES
andADDRESSES
with 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
WAN
interface will then be set todhcp
and the twoLAN
interfaces will bestatic
.
-
DNS Entries
: to add specificDNS
entries in yourrouter
, you can edit the/etc/hosts
file 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
-hostname
on a blank line, separated by spaces.
DHCP
-
Static DHCP leases
: to set staticDHCP
leases, 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 address
pairs, so that a specificMAC address
will take the specifiedIP address
fromdnsmasq
.
- Enable the
read-ethers
option in/etc/dnsmasq.conf
: open the file with your text editor and uncomment the#read-ethers
line removing the leading#
character:
read-ethers
- Set the
DHCP ranges
you want to use in the same/etc/dnsmasq.conf
file. Example with networks192.168.56.0/24
and172.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
dnsmasq
service with that command:
service dnsmasq restart
Routing
- Enable the
forwarding
fromsysctl
: open/etc/sysctl.conf
with your text editor and uncomment the following line (removing the leading#
character):
net.ipv4.ip_forward=1
Setting the iptables
rules
- To set the
iptables
rules, you need to have clear which connections you want to allow, depending on the services present in your networks. Here is an example using aWebServer
and aPostgreSQL Server
in two differentLAN
networks.
"$WAN"
,"$LAN1"
and"$LAN2"
correspond to the network interfaces ofrouter
.
"$WEB_SERVER"
and"$PSQL_SERVER"
correspond toIP
addresses belonging to aWebServer
and 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
rsyslog
tcp reception
in the/etc/rsyslog.conf
file. 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
rsyslog
output file to use a template in the same/etc/rsyslog.conf
file:
- 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.conf
to enable thedateext
option (to use the date in the names of the log files) and uncomment the following line:
dateext
- Create a new file
/etc/logrotate.d/remote
with 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,
reboot
therouter
using that command:
reboot
No Comments