Friday, August 14, 2020

Using a Raspberry Pi for basic network services, Part 3: Static IP addresses


Photo credit: Michael Henzler
Wikimedia Commons, CC BY-SA 4.0


The Raspberry Pi for basic Networking services series:

In part 2 of this series, I described how to set up a DHCP server on a Raspberry Pi so it can allocate and serve dynamic IP addresses. But sometimes you need a static address, where a given device always gets the same well-known address.

One obvious solution is to just disable the DHCP client on the device and manually configure its IP address and other networking credentials. This works well for many devices, especially desktop and laptop computers, but it is not without problems. These problems include:

  • Some devices (especially IoT devices) don't support manual configuration, or the mechanism is very inconvenient.
  • If you want to change some part of your network's configuration (not just IP addresses but any network parameter), you must manually reconfigure every device not using DHCP.
  • If some of your devices move around, you will need to re-enable DHCP when you're away and go back to manual configuration when you return home. Some operating systems (like iPhone OS) do this automatically, associating configurations with Wi-Fi networks. Some operating systems (like macOS) provide a "Location" menu where you can create different configurations, but you still need to manually change locations to change the configuration. Some operating systems (like Windows) make you manually change every parameter.
  • Some people using your network may not have the technical skills to manually configure a device's network settings without expert assistance.

Fortunately, there is a solution. You can configure the devices to use DHCP, but configure the DHCP server such that they always get the address you want them to have.

To configure this, you need to know the device's hardware address (also known as a MAC address). There are several different ways to determine a device's MAC address, including:

  • Look at the device. Many devices (including printers and routers) print the MAC address on a label, often near the serial number. It may also be printed on the device's packaging.
  • Look at a status screen on the device. Computers, phones and other devices with displays usually have a way to display the MAC address on the screen, usually on one of the device's network configuration screens. Appliances and other IoT devices may make the MAC address available via the mobile app or cloud service used to configure it.
  • Print it. Printers (especially laser printers) can usually print a network configuration/status page (via some sequence of buttons on the front panel). This page will contain the MAC address. Consult your manual if you need assistance printing this page.
  • Look at the DHCP server's status. If you can't find the address any other way, look at your DHCP server's log of active leases. If you are running the Raspberry Pi DHCP server described in part 2, you can type "cat /var/lib/dhcp/dhcpd.leases" to view this log. Locate your device (look for a client-hostname that looks like your device's name or a vendor-class-identifier that looks appropriate) and copy its MAC address (the hardware ethernet field).

That last option may be a bit tricky to understand, so here's an example:

$ cat /var/lib/dhcp/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.4.1

# authoring-byte-order entry is generated, DO NOT DELETE
authoring-byte-order little-endian;

lease 192.168.1.178 {
  starts 1 2020/08/10 01:00:58;
  ends 2 2020/08/11 01:00:58;
  tstp 2 2020/08/11 01:00:58;
  cltt 1 2020/08/10 01:00:58;
  binding state free;
  hardware ethernet xx:xx:xx:xx:xx:xx;
  uid "...";
  set vendor-class-identifier = "MSFT 5.0 XBOX";
}
lease 192.168.1.111 {
  starts 5 2020/08/14 03:44:58;
  ends 6 2020/08/15 03:44:58;
  cltt 5 2020/08/14 03:44:58;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet xx:xx:xx:xx:xx:xx;
  client-hostname "Living room tv";
}
lease 192.168.1.194 {
  starts 5 2020/08/14 13:51:10;
  ends 6 2020/08/15 05:53:46;
  cltt 5 2020/08/14 13:51:10;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet xx:xx:xx:xx:xx:xx;
  uid "...";
  set vendor-class-identifier = "EchoStar 0.0.1";
  client-hostname "Hopper2-br0";
}
lease 192.168.1.180 {
  starts 5 2020/08/14 12:37:18;
  ends 6 2020/08/15 12:37:18;
  cltt 5 2020/08/14 14:04:17;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet xx:xx:xx:xx:xx:xx;
  uid "..";
  set vendor-class-identifier = "android-dhcp-8.0.0";
  client-hostname "My-Phone";
}
lease 192.168.1.175 {
  starts 0 2020/08/09 07:52:54;
  ends 2 2020/09/08 07:52:54;
  cltt 3 2020/08/12 23:29:10;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet xx:xx:xx:xx:xx:xx;
  uid "...";
  client-hostname "Apple-TV";
}

The above fragment shows the current DHCP lease status for five devices. You can probably figure out that these are, respectively, an XBox game console, a smart TV, a Dish Network Hopper DVR, an Android phone and an Apple TV. Not all devices advertise their vendor class and name, but enough do that this may be a good way to determine a device's MAC address.

To assign a static IP address to a device, add a host command to your DHCP configuration file. Edit /etc/dhcp/dhcpd.conf and add a host command for each static address you want to configure:

host myphone
{
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 192.168.1.57;
}
host myprinter
{
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 192.168.1.58;
}
host mydoorbell
{
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 192.168.1.59;
}

When requesting an address, any device providing one of the specified MAC addresses (the hardware ethernet line) will be assigned the corresponding IP address (the fixed-address line).

When choosing an IP address, be sure to select an address that will not be assigned dynamically. That is, it should not be part of any range parameters on the configured subnet.

No comments: