Last Updated: September 09, 2019
·
2.975K
· benwah

Connect Raspberry PI on a wifi network via Ethernet, through your computer

I just spent a few hours figuring this out, so I thought it might be useful for other people, and serve as a reminder next time I need to do this.

Problem: You have a computer or laptop running Ubuntu that's connected to a Wifi network, and your Raspberry PI doesn't have a Wifi adapter to connect to the Wireless network, so you want to use your computer to act as a router for the Raspberry PI.

  • Set Raspberry PI's network interface to be configured by DHCP, on the raspberry PI, edit /etc/network/interfaces, mine looks like this:


# /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp

</pre>

  • Edit /etc/network/interfaces on the computer running Ubuntu. The last line assumes that your wifi adapter connected to the wifi router is wlan0. Mine looks like this:


# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet static
        address 10.0.0.1
        netmask 255.255.255.0
        network 10.0.0.0
        broadcast 10.0.0.255
        dns-nameservers 8.8.8.8
        dns-search lan
        post-up /sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

</pre>

  • Install dns-masq on the computer running Ubuntu:


$ sudo apt-get install dnsmasq

</pre>

  • If ubuntu firewall is enabled (ufw), allow port 53 for dhcp:


$ sudo ufw allow 53

</pre>

  • Edit /etc/dnsmasq.conf, here are the lines I uncommented and modified, but honestly there might be some things that are wrong, it worked the first time so whatever:


server=10.0.0.1@wlan0
interface=eth0
dhcp-range=10.0.0.2,10.0.0.254,255.255.255.0,24h

</pre>

Now connect your raspberry PI via Ethernet to your computer, ensure that computer is connected via Wifi, reboot your computer and your raspberry PI and voila! You should be online.

IMPORTANT NOTE! You don't know what IP address was assigned to your raspberry PI? You can check the DHCP lease:


$ cat /var/lib/misc/dnsmasq.leases

</pre>

And you should see what IP address has been assigned to the Raspberry PI, you can also fuck around with DHCP settings to give it a static lease, or just disable DHCP alltogether and manually configure /etc/network/interfaces.

2 Responses
Add your response

This is a cool tip, but in Ubuntu you can achieve the same from the NetworkManager icon. Edit the Ethernet interface and set it to be Shared to other computers. instead of Automcatic (DHCP) or Manual, et voila!

over 1 year ago ·

I saw this was possible but (as far as i could see) it required to manually configure the raspberry PI's network interface and set its gateway to the IP address network manager decides Ubuntu should have. So next (Ubuntu) reboot your PI might lose connectivity and you'll have to plug in a keyboard and monitor to update its network configuration.

over 1 year ago ·