Skip to content

2. Alpine Linux-based Router

This section proposes a way to configure a router and its networks. The router has the IP address 192.168.88.2/24, so that it does not interfere with the one from Practice 1 if it has already been done, and so both routers can be used on the same network.

Create a generic base template for Alpine Linux

To create the desktop, the Alpine ISO is needed; it is available on the official website: https://dl-cdn.alpinelinux.org/

How to install and create a virtual machine with Alpine?

The following guide explains how to create an Alpine desktop in IsardVDI: Alpine installation

Once Alpine is installed in IsardVDI, it can be configured as a router.

Since the desktop must work as a router, edit it with the following characteristics:

  • vCPUs: 1
  • Memory (GB): 0.5 GB
  • Boot: Hard Disk
  • Networks:
    • eth0: Default (Internet output)
    • eth1: Wireguard VPN
    • eth2: Personal1

The goal is to prepare a script to set the firewall rules, routes and actions to run when the router starts.

  1. Create a first script:

    touch /usr/local/bin/startup_script.sh
    chmod u+x /usr/local/bin/startup_script.sh
    

  2. The script content can be:

    #!/bin/bash
    ip address show >> /tmp/log_ip.txt
    

  3. Create the service file:

    touch /etc/init.d/router
    chmod u+x /etc/init.d/router
    

  4. The service file must contain:

    #!/sbin/openrc-run
    
    depend() {
        after sshd
    }
    
    start() {
        ebegin "router starting"
        /usr/local/bin/startup_script.sh
        eend $?
    }
    

  5. Enable the service:

    rc-update add router default
    

  6. Enable the forwarding bit:

    echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
    

  7. After a reboot (system restart), you can check whether it is active by looking at the following file:

    cat /proc/sys/net/ipv4/ip_forward
    

  8. Install the packages needed for the network functions:

    apk add iptables iproute2 dnsmasq bash
    

  9. With the packages installed and the router base done, edit the /usr/local/bin/startup_script.sh script and add the following content:

#!/bin/bash

# SET USER ISARD VPN IP
# Adding the route manually, since udhcpc does not configure it by default.
ip link set eth1 name vpnisard
ip link set vpnisard up
udhcpc -i vpnisard
ip route add 10.0.0.0/14 via 10.2.0.1

# SET LAN1 IP
ip link set eth2 name lan1
ip link set lan1 up
ip address add 192.168.88.2/24 dev lan1

# DHCP SERVER
# Create configuration file
cat <<'EOF' > /etc/dnsmasq_router.conf
interface=lan1
dhcp-range=192.168.88.20,192.168.88.99,255.255.255.0,24h
dhcp-option=3,192.168.88.2
dhcp-option=6,8.8.8.8,1.1.1.1
EOF
# Start the dnsmasq server
dnsmasq --conf-file=/etc/dnsmasq_router.conf

# NAT MASQUERADE
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  1. To check it, restart the desktop and verify that the boot messages show no errors and that what was programmed in the script has been applied correctly.

Once everything has been verified, check with another desktop acting as a client that it gets an IP, Internet access and DNS resolution. Then, create a template named router-Alpine.