Vai al contenuto

Configuring a Router in VirtualBox

Pubblicato:

Table of contents

Step by step

Warning

VirtualBox and have downloaded some .iso file. We will use debian

The idea is to create an architecture like this one:

Loading diagram...

Note

Create the virtual machines

First of all create all the three virtual machines (VM) needed. To make the whole process faster, consider creating one and then cloning it two times, before having done any configuration.

Note

client, router and server.

To make the simulation more interesting, and avoid any accidental success, make sure to put each VM in a separate network, with the router being the one that is able to connect them. After clicking on a VM in the VirtualBox window, go to settings -> network, go to one of the adapter tabs and set the connection to internal network.
The router will have three interfaces active: the one it shares with the client, the one it shares with the server, and you can add a NATted one so it can connect to the internet.

Configure the interfaces

First of all, check which interface you are using with the command

ip a

Warning


Make sure to use the correct interface of your machine.

Client

The adapter 1 of the client should be set to the internal network client.

Edit the /etc/network/interfaces file

# Client /etc/network/interfaces
auto enp0s3
iface enp0s3 inet static
    address 10.0.1.2
    netmask 255.255.255.0
    gateway 10.0.1.1

Reboot the machine

reboot

Router

Each adapter of the router should be set to

  1. the nat configuration
  2. the internal network client
  3. the internal network server

Edit the /etc/network/interfaces file

# Router /etc/network/interfaces
# The first one should already be set
allow-hotplug enp0s3
iface enp0s3 inet dhcp

auto enp0s8
iface enp0s8 inet static
    address 10.0.1.1
    netmask 255.255.255.0

auto enp0s9
iface enp0s9 inet static
    address 10.0.5.1
    netmask 255.255.255.0

Enable the forwarding of ip packets through the router by removing the comment from the line

# Router /etc/sysctl.conf
net.ipv4.ip_forward=1

Reboot the machine

reboot

Server

The adapter 1 of the server should be set to the internal network server.

Edit the /etc/network/interfaces file

# Server /etc/network/interfaces
auto enp0s3
iface enp0s3 inet static
    address 10.0.5.2
    netmask 255.255.255.0
    gateway 10.0.5.1

Reboot the machine

reboot

[Optional] Configure the hostnames

Edit the /etc/hosts/ file and add the following lines at the end of the file.
This way, you can use the hostname instead of the ip address.

# Client /etc/hosts/
10.0.1.1 router
10.0.5.2 server
# Router /etc/hosts/
10.0.1.2 client
10.0.5.2 server
# Server /etc/hosts/
10.0.5.1 router
10.0.1.2 client

Results

Now you should be able to ping the server from the client and vice-versa.
Using traceroute it is possible to have a cleaner picture of the network.

# from the client
traceroute 10.0.5.2
# from the server
traceroute 10.0.1.2

Extra: Connect to the internet

If you try pinging a public server from any VM which isn’t the router, you won’t receive any response. This is because, for the forwarding to work, the router must allow NATting.
To do so, IPTABLES must be configured accordingly.

# Enable the masquerading of the ip address
iptables --table nat --append POSTROUTING --out-interface enp0s3 -j MASQUERADE
iptables --append FORWARD --in-interface enp0s3 -j ACCEPT
# Restart the service, if needed (on RHEL / Red Hat / CentOS / Fedora)
service iptables restart

Cheatsheet

Modify network configuration from terminal (volatile)

Add an ip address to an interface

# ip a add <ip>/<mask> dev <interface>
ip a add 10.0.1.5/24 dev enp0s8

Set an interface up or down

# ip link set dev <interface> (up|down)
ip link set dev enp0s8 up

Add, replace or remove a route for a network

# ip route (add|replace|del) <ip>/<mask> dev <interface>
ip route add 10.0.1.0/24 dev enp0s8
# ip route (add|replace|del) <ip>/<mask> via <ip>
ip route add 10.0.1.0/24 via 192.168.1.1

Add, replace or remove the default route for every ip which hasn’t a route specified

# ip route (add|replace|del) default dev <interface>
ip route add default dev enp0s8
# ip route (add|replace|del) default via <ip>
ip route add default via 192.168.1.1

Modify network configuration from file (persistent)

Modify the file /etc/network/interfaces. You can use whatever editor you like to achieve this, but you will probably find vi or nano already installed.

auto <interface>
iface <interface> inet static       # don't use dhcp to get the ip address of the interface
    address <ip>                    # ip of the interface
    netmask <mask>                  # mask of the ip (the /n part)
    gateway <router ip>             # [ONLY ONE INTERFACE] set the default interface
    network <network>               # [OPTIONAL] base network. ip/mask by default
    broadcast <broadcast address>   # [OPTIONAL] broadcast address. ip.255 by default
    post-up <route up>              # [OPTIONAL] put up a route on interface up. Does by default
    per-down <route down>           # [OPTIONAL] delete a route on interface down. Does by default

auto enp0s8
iface enp0s8 inet static
    address 10.0.1.2
    netmask 255.255.255.0
    gateway 10.0.1.1
    network 10.0.1.0
    broadcast 10.0.1.255
    post-up route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.1.1 dev enp0s8
    per-down route del -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.1.1 dev enp0s8

Set up hostnames

Edit the /etc/hosts file and add all the couples ip-hostname below the lines you’ll find

127.0.0.1       localhost
::1             localhost ip6-localhost

# <ip> <hostnames>
10.0.0.1 router
192.168.1.2 server