I have 2 Debian virtual machines in VirtualBox: debian-router (has NAT adapter and internal network INTNET1 adapter) and debian-client (has only INTNET1 adapter). Give settings for debian-router and debian-client to give debian-client ability surf the Internet with dhcp and nat (make debian-router works like real router)
Assistant
To set up debian-router as a router that allows debian-client to access the internet, you need to configure network address translation (NAT) and set up a DHCP server on debian-router. Here are the steps to achieve this:
Step 1: Configure Network Interfaces
debian-router
Ensure that the NAT adapter (enp0s3) is configured for internet access.
Ensure that the internal network adapter (enp0s8) is connected to INTNET1.
Edit the /etc/network/interfaces file to configure the interfaces (you may have different interface names):
sh
auto lo
iface lo inet loopback
# NAT interface
auto enp0s3
iface enp0s3 inet dhcp
# Internal network interface
auto enp0s8
iface enp0s8 inet static
address 192.168.10.1
netmask 255.255.255.0
debian-client
This VM should already be connected with its only adapter to INTNET1.
Edit the /etc/network/interfaces file to set it up for DHCP:
sh
auto lo
iface lo inet loopback
# Internal network interface
auto enp0s3
iface enp0s3 inet dhcp
Step 2: Enable IP Forwarding on debian-router
To allow routing between interfaces, enable IP forwarding:
Edit /etc/sysctl.conf to enable IP forwarding by uncommenting or adding the following line:
sh
net.ipv4.ip_forward=1
Activate this change immediately by running:
sh
sudo sysctl -p
Step 3: Set Up NAT on debian-router
You need to configure iptables to handle NAT:
sh
sudo iptables -t nat -A POSTROUTING -o enp0s3 -j MASQUERADE
To ensure the iptables rule persists across reboots, you can save the current iptables rules:
sh
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
Make sure the iptables-persistent package is installed to apply these settings at boot.
Step 4: Install and Configure DHCP Server on debian-router
Specify the interface for the DHCP server in /etc/default/isc-dhcp-server:
sh
INTERFACESv4="enp0s8"
Restart the DHCP service to apply these changes:
sh
sudo systemctl restart isc-dhcp-server
Step 5: Test the Configuration
Start both virtual machines.
On debian-client, check if the network settings are assigned via DHCP:
sh
ip address show enp0s3
Test the internet connection:
sh
ping -c 4 google.com
If everything is configured correctly, debian-client should receive an IP from the DHCP server on debian-router and be able to access the internet using NAT.