IP Addressing Options

On newer versions of Raspbian, the IP addressing for all network cards is done on the Pi via the program called dhcpcd.  If you just want to set a static IP address, you can edit the /etc/dhcpcd.conf file, but we're going to take a different approach.

This page in the guide will walk you through:

  • Disabling dhcpcd
  • Setting your IP address on usb0 manually
  • Setting up the l0 and wlan0 interfaces to act normally
  • Run your own DHCP server on the usb0 port, so your Pi can provide an address to your Linux or Windows PC or Mac without any additional software on your desktop or laptop.

Disabling dhcpcd

First, let's disable dhcpcd. This is non-destructive, but when we run this command dhcpcd won't be able to assign addresses anymore, so you should be logged in locally for this with a monitor attached. 

sudo systemctl disable dhcpcd

Setting up the interfaces

Now let's setup your interfaces manually since dhcpcd won't be doing it anymore. Go ahead and run:

sudo nano /etc/network/interfaces

In there you will probably see something in the file that says:

source-directory /etc/network/interfaces.d

Go ahead and leave that in. Below it, add all of this text, paying attention to the indents:

auto lo
iface lo inet loopback

auto usb0
allow-hotplug usb0
iface usb0 inet static
address 10.77.77.77
netmask 255.255.255.0

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

The interfaces we've assigned are l0 (loopback, which is needed, just not in scope of this guide), usb0 (which we assigned a static IP address of 10.77.77.77), and wlan0, which will still connect to WiFi normally. Go ahead and save the file and close the editor.

Next, let’s make sure your phone’s hotspot connection is in /etc/wpa_supplicant/wpa_supplicant.conf.

Run:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

In there, each network should be listed like this:

network={
ssid="Your-Home-SSID"
psk="yourpassphrase"
}

Modify it to match your home network's wireless info - if it's already there, you don't need to add it. You can add this section for each wireless network you'll want the Pi to connect to.

Run your own DHCP Server

Next we'll install dnsmasq, which will let us use DHCP to assign IP addresses to PCs or Macs that connect to the USB port on the Pi. Simply run:

sudo apt-get install -y dnsmasq

Configuration is easy - just run:

sudo nano /etc/dnsmasq.conf

Add the following lines at the bottom:

dhcp-range=10.77.77.78,10.77.77.99,12h
dhcp-option=3
dhcp-option=6

The DHCP range will need to match the interface IP address we assign to the usb0 interface, and this option will assign addresses between 10.77.77.78 and .99, with a 12 hour lease. That should be more than enough. If you need to change the IP range for some reason, make sure to match the configuration of usb0 with these items. We also us DHCP options 3 and 6 - they are annotated in the config file, but they prevent dnsmaq from advertising a default route or DNS - we don’t need this Pi to be a DNS server or a router for this tutorial.

Go ahead and save and exit from the file, we won’t start dnsmasq just yet though. When rebooting, please give your Pi time to start all the services, get a WiFi address, and assign one to your PC. The Pi Zero W is a little slower.

Go ahead and safely shut the Pi down with the following command:

sudo halt

Checking it out

Once the Pi is halted, you should be able to see nothing going on with the display and safely unplug it from power. Next you can simply plug a USB data cable (make sure it's not a charging cable) to the micro USB port closest to the center of the Pi, and the other end to your PC. You'll hear a sound and see drivers installing on Windows 10 and newer, and on all systems you'll see a new network card. You should be able to simply connect with:

ssh -l pi 10.77.77.77

From your PC and get the login prompt on the Pi. The Pi will also independently connect to WiFi, which can be handy if you're testing a different wireless network, connecting to a WiFi hotspot, etc.

This guide was first published on Dec 24, 2015. It was last updated on Dec 24, 2015.
This page (IP Addressing Options) was last updated on Aug 10, 2020.