A frequent problem I have when creating a new Raspberry Pi-based project is determining the IP address of the device. I am often working with Pi projects in a "headless" (no monitor) configuration using SSH to log in from a PC on the same LAN network. But to log in this way, I need the Pi's IP address, which sets up a chicken-and-egg problem that goes like this:
I want to connect (SSH) to a headless Raspberry Pi, but before I can do so, I first need to attach a monitor and keyboard to it to login locally and get its IP address (with 'ifconfig').
This is the very thing I would like to avoid since the system will ultimately be "headless". In truth, while it is only a minor inconvenience, it is one I'd like to do without. The more new RPi systems that you set up, the more tedious it is to pull out a monitor and keyboard to find the IP address of each one before you can begin remotely connecting to it over the LAN using SSH.
I would like to be able to find the IP address of a headless RPi without having to connect a monitor and keyboard to it. Ideally, I would be able to connect my new RPi to my network, and know the IP address that is it assigned. Then, I could simply connect directly to it using SSH from my PC on the same network.
As is the case for many others, my minor annoyance at "the problem" turned into a full-blown project to solve it. I created a program I called Hello Pi that can be run on a Linux or Windows computer to identify the IP address of a Pi (or other device) connected to the same network segment.
Hello Pi running on a PC passively listens for DHCP messages that are multicast by other devices on the network. When an RPi (or any device that uses DHCP) first powers up when connected to a network, it sends a multicast DHCP message to identify itself (by MAC address) and request an IP address from the network's DHCP server. The DHCP server responds with an IP address that it assigns to the MAC address specified in the request.
Hello Pi listens for these DHCP messages and displays the IP addresses that are assigned. It can be used to display only RPis that are connecting on the network by automatically filtering on MAC address ranges used by those devices, or it can display ALL devices connecting that use DHCP.
DHCP message are not only sent when a device is first connected to a network (to request an IP address), but periodically over longer time intervals to notify the DHCP server that it is still present and would like to continue using the same IP address. If Hello Pi is left running, you will see these messages over time as devices connect and/or renew their DHCP "lease".
Hello Pi is run from a computer (the 'Host') connected to the same DHCP-configured LAN as the RPi (the 'Target') or other device to be discovered.
- Using the command-prompt, start Hello Pi on the Host.
- Note that the -h option can be used to see a list of available command options.
- The initial state of the Target (RPi or device) is powered-down/unpowered.
- Connect Target to the LAN.
- If using Ethernet, physically connect the cable.
- If using WiFi, configure the wpa_supplicant.conf file in the boot folder of the OS image.
- Power-up the Target and wait while it boots.
- Observe the output of Hello Pi on the Host to learn the ip address assigned to the Target.
- To repeat the process, power-down the Target and return to step 2.