How to Share an Internet Connection via Ethernet in Debian 12 Bookworm

How to Share an Internet Connection via Ethernet in Debian 12 Bookworm

Sharing an internet connection via Ethernet in Debian 12 (Bookworm) can be useful in various scenarios, such as providing internet access to another computer, setting up a small local network, or testing network configurations. This guide will walk you through the process step by step, ensuring a stable and secure connection.

Prerequisites

Before we begin, ensure that you have the following:

  • A computer running Debian 12 Bookworm.
  • A functional internet connection (either Wi-Fi or another Ethernet connection).
  • An additional Ethernet cable to connect the target device.
  • Root or sudo privileges to modify network settings.

Step 1: Identify Network Interfaces

The first step is to identify your network interfaces. Open a terminal and run:

ip a

This will list all network interfaces. You should see at least two interfaces:

  • One connected to the internet (e.g., wlan0 for Wi-Fi or eth0 for Ethernet).
  • One for sharing the connection (e.g., eth1).

Step 2: Enable IP Forwarding

To allow your Debian system to act as a router and forward network traffic, enable IP forwarding:

echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

To make this setting permanent, edit the system configuration file:

sudo nano /etc/sysctl.conf

Find the line:

#net.ipv4.ip_forward=1

Uncomment it by removing the #, so it reads:

net.ipv4.ip_forward=1

Save the file (Ctrl + X, then Y, and Enter), then apply the changes:

sudo sysctl -p

Step 3: Configure Network Address Translation (NAT) with iptables

Now, set up NAT using iptables so that the internet connection can be shared:

sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -o wlan0 -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT

Replace wlan0 with your internet-connected interface and eth1 with the target Ethernet interface.

To make these rules persistent across reboots, install the iptables-persistent package:

sudo apt install iptables-persistent

During installation, you will be prompted to save the current rules. If not, save them manually with:

sudo netfilter-persistent save

Step 4: Assign a Static IP Address to the Ethernet Interface

The Ethernet interface (eth1) needs a static IP address. Edit the network configuration file:

sudo nano /etc/network/interfaces

Add the following lines:

auto eth1
iface eth1 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255

Save the file and restart networking services:

sudo systemctl restart networking

Step 5: Set Up a DHCP Server (Optional)

To automatically assign IP addresses to connected devices, install and configure a DHCP server:

sudo apt install isc-dhcp-server

Edit the DHCP configuration file:

sudo nano /etc/dhcp/dhcpd.conf

Add the following configuration at the end:

subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.100 192.168.1.200;
    option routers 192.168.1.1;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Edit the default interface configuration file:

sudo nano /etc/default/isc-dhcp-server

Find the line:

INTERFACESv4=""

Modify it to specify the Ethernet interface:

INTERFACESv4="eth1"

Save the file and restart the DHCP service:

sudo systemctl restart isc-dhcp-server

Ensure the service starts on boot:

sudo systemctl enable isc-dhcp-server

Step 6: Configure the Target Device

On the device receiving the shared internet connection, configure the network settings:

  • If using DHCP, it should automatically receive an IP address.
  • If setting manually, use:
    • IP Address: 192.168.1.2 (or any within 192.168.1.100-200)
    • Subnet Mask: 255.255.255.0
    • Gateway: 192.168.1.1
    • DNS: 8.8.8.8, 8.8.4.4

Step 7: Test the Connection

On the target device, test connectivity by running:

ping 8.8.8.8

If the ping succeeds, try browsing the internet.

Conclusion

By following these steps, you can successfully share an internet connection via Ethernet on a Debian 12 Bookworm system. Whether you’re setting up a home network or troubleshooting connectivity issues, this method provides a reliable way to extend your internet connection.