Connecting a LAN to internet through a gateway

Feed

date: 2023-11-01 21:21:44

categories: linux

firstPublishDate: 2023-11-01 21:21:44

I have a mobile phone, a laptop, a network hub and a desktop computer. I configure the mobile phone as a hot spot and connect the laptop to the hot spot with wifi, the laptop is connected to internet with the mobile phone. With a network cable the laptop is connected to the hub and the desktop computer is also connected to the hub. The laptop has 2 network interfaces and is connected to both internet and the desktop computer.

I want to connect the desktop computer to the internet using the laptop as a gateway.

I created the figure with patate:

Patate ASCII art editor

Computer A Laptop (gateway) has 2 network interfaces:

Computer B Desktop has 1 wired network interfaces:

I choose the network 192.168.0.0/24 for the LAN.

There are 3 steps to configure the laptop to be used as a gateway:

Set static ip for the enp6s0 interface

In the laptop, I choose to configure network 192.168.0.0/24 and ip 192.168.0.1, devices connected to the hub will have ips in this network. The ips are distributed automatically by the dhcp server.

Reconfigure the interface:

Setup iptables to forward packets to internet

Configure the linux kernel to allow forwarding packets and make it persistent:

Setup iptables:

This says: on the network address translation table, after we have figured out the routing of a packet on output enp6s0, replace the return address information with our own so the return packets come to us. Also, remember that we did this (like a lookup table that remembers this connection). Connections coming from the LAN (192.168.0.0/24) to the internet are recorded in the gateway (laptop) NAT table.

Allow packets that want to come from enp6s0 (the LAN) to go out wlp7s0b1 (the wifi interface connected to internet).

Use that lookup table we had from before to see if the packet arriving on the external interface actually belongs to a connection that was already initiated from the internal.

Setup a dhcp server

I setup a dhcp server in the gateway to automatically configure the network for the devices connected to the LAN. The dhcp server sets up a default nameserver, the default nameserver in the laptop is:

The devices in the LAN use the nameservers 192.168.223.198 and 8.8.8.8 (google, if the first one doesn't work). Install and configure the ISC dhcp server (I used Debian Bullseye for this setup):

Setup the network interface for the dhcp server, I disable ipv6 since it is not used in this setup:

Start the dhcp server:

Run these commands to restart the dhcp server:

Now the devices on the LAN have their network interface automatically configured by the dhcp server like a regular router.

Another article describing the same setup:

Building My Own Firewall/Router, Part 1 11-07-2023

Hashtag #networking

Feed

Guestbook

Proxied content from gemini://gmi.noulin.net/2023-11-01-connecting-a-lan-to-internet-through-a-gateway.gmi (external content)

Gemini request details:

Original URL
gemini://gmi.noulin.net/2023-11-01-connecting-a-lan-to-internet-through-a-gateway.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.