Article Published on 17 Octorber 2017
We've all faced situations where we have a Raspberry Pi (without Wi-Fi), a laptop, and an Ethernet cable, but no external USB Wi-Fi stick. You need to get your Pi online. This guide will walk you through using dnsmasq
and some configuration magic to share your laptop's internet connection with your Pi.
Requirements
A Linux-based laptop with Wi-Fi and an Ethernet port.
A Raspberry Pi (or any device) with an Ethernet port.
An Ethernet cable.
Step-by-Step Guide
1. Choose an IP Address Range
Select a range for all future clients. I recommend the private IP address range 10.0.0.0/24
:
10.0.0.0 - Network address
10.0.0.1 - Ethernet interface address
10.0.0.x - Client addresses (2 < x < 255)
2. Configure the Ethernet Interface on the Host
Activate the interface and assign an IP:
$ sudo ip link set dev eth0 up
$ sudo ip addr add 10.0.0.1/24 dev eth0
3. Install and Set Up dnsmasq
dnsmasq
is a lightweight tool that provides network infrastructure for small networks. Here, it will offer DHCP services for Ethernet-connected clients.
# Install dnsmasq
$ sudo pacman -Syu dnsmasq
# For Ubuntu or Debian, use apt-get
# Configure dnsmasq
$ sudo vim /etc/dnsmasq.conf
# Add the following to /etc/dnsmasq.conf
interface=eth0
bind-interfaces
dhcp-range=10.0.0.2,10.0.0.254
# Start dnsmasq
$ sudo systemctl start dnsmasq
# Check DHCP lease info
$ sudo journalctl -u dnsmasq
# or
$ cat /var/lib/misc/dnsmasq.leases
4. Enable IP Forwarding and NAT
To provide the Pi with internet access via the laptop's Wi-Fi, you'll need to forward packets and enable Network Address Translation (NAT). This makes the laptop's public IP address also serve as the Pi's public IP.
# Enable IP forwarding temporarily
$ sudo sysctl net.ipv4.ip_forward=1
$ sudo sysctl net.ipv4.conf.eth0.forwarding=1
# Enable IP forwarding permanently (requires restart)
$ sudo vim /etc/sysctl.d/30-ipforward.conf
# Add the following to /etc/sysctl.d/30-ipforward.conf
net.ipv4.ip_forward=1
net.ipv6.conf.default.forwarding=1
net.ipv6.conf.all.forwarding=1
# Enable NAT for your wireless interface (assuming it's named wlan0)
$ sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
$ sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
With these steps completed, you can check for leased IP addresses with sudo journalctl -u dnsmasq
or cat /var/lib/misc/dnsmasq.leases
. Once you identify your device's IP address, SSH into it, run sudo apt-get update
, and watch as it accesses the internet through a single cable!