Easy Ethernet access for a headless Raspberry Pi

This neat trick is implemented on the Raspberry Pi-based Twilio Developer Kit for Broadband IoT, with which I’ve been working a lot of late (as a Twilio staffer). It lets you connect a headless Pi to your main machine via Ethernet just be plugging in a cable. It also maintains the Pi’s own wireless connection to the Internet.

I wanted to figure out how it was done so that I could implement it on my own Pi. Using Ethernet makes SSH-ing into the Pi so much faster than doing over wireless. I did some research and got 99 per cent of it; a colleague — thanks, Randy! — showed me the missing one per cent that made it work.

Here’s how it’s done. The following steps are all performed on the Pi.

  1. Install udhcpd to serve up IP addresses. Run this command:
    sudo apt install udchpd
  2. udhcpd needs a file that’s not created by the install, so run:
    touch /var/lib/misc/udhcpd.leases
  3. Now we set the three udhcpd config files. First run:
    sudo nano /etc/init.d/udhcpd
  4. Add $network to the end of the header line beginning # Required-Start
  5. Save the file with ctrl-x then y.
  6. Edit the second udhcpd config file. Run:
    sudo nano /etc/default/udhcpd
  7. Comment out the line DHCPD_ENABLED="no"
  8. At the end of the file, add:
    DHCPD_OPTS="-I 192.168.253.100 -S"
    This IP address will be the one you use to access the Pi.
  9. Save the file with ctrl-x then y.
  10. Edit the third udhcpd config file. Run:
    sudo nano /etc/udhcpd.conf
  11. This is trickier as it involves making some changes throughout the file. Look for the items listed in the code block below and set them in the .conf file accordingly, uncommenting as you go. Or you can just replace the whole file with the lines below:
    start      192.168.253.101
    end        192.168.253.111
    interface  eth0
    max_leases 10
    option     subnet 255.255.255.0
    option     domain local
    option     lease  864000
  12. Save the file with ctrl-x then y.
  13. Edit the dhcpcd config file. Run:
    sudo nano /etc/dhcpcd.conf
  14. Look for the commented line #interface eth0 and uncomment it.
  15. Look for the commented line #static ip_address=192.168.0.10/24. Uncomment it and change the IP address to 192.168.253.100. Make sure you don’t delete the /24and that you match the address set in step 8.
  16. Save the file with ctrl-x then y.
  17. Enter the following command to enable udhcpd at boot:
    systemctl enable udhcpd
  18. Restart the Pi:
    sudo shutdown -r now

While the Pi is rebooting, connect it to your Mac using a standard Ethernet patch cable. If you open System Preferences > Network, you’ll see the Ethernet port go from Cable Unplugged to Not Connected then to Connected, the latter with an IP address somewhere between 192.168.253.101 and 192.168.253.111, the range you specified in /etc/udhcpd.conf.

ethernet_01
ethernet_02
ethernet_03

SSH to the Pi in the usual way, but this time use the static IP address you specified in /etc/default/udhcpd and /etc/dhcpcd.conf:

ssh pi@192.168.253.100

Of course, if you’re happy with your WiFi speed, either computer’s or the Pi’s Ethernet port isn’t free, or the two machines are too far apart to make a wired connect feasible, then this approach won’t help you, but it is very handy for desk-bound Pis.

Connecting by Ethernet is effectively as quick as logging into the Pi directly. That’s also very much the case with virtual desktop access using VNC.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s