Thursday, March 9, 2017

Using dhclient to automatically change IP addresses of Raspberry Pi on start-up

So, in my previous post I dealt with changing IP addresses on my Pi cluster by scp'ing the new IP addresses after the Pi had started up and established a network, and creating an ssh/config file from that info.

This is fine for most occasions. But I have one instance set-up to run zookeeper. I'd have to go around and change the config files on all my other Pi's. Ansible could cure this, and I'll get there one of these days. But I'm not there today. And actually - I think I'll use the script I wrote previously to update ansibles' hosts file. I just now thought of that.

Anyway - I want the zookeeper Pi to always have the same address.



I combined this question on superuser.com with what I learned about creating systemd start-up jobs to request the same ip address from my hotspot.

The gist of the superuser.com answer is add send dhcp-request-address 192.168.0.XXX to /etc/dhcp/dhclient.conf.

I then created a short executable script in /usr/bin called request_ip.sh.
$cat /usr/bin/request_ip.sh
#! /bin/bash

dhclient -r -v && dhclient -4 -d -v -cf /etc/dhcp/dhclient.conf wlan0
Then I created the service file:
# cat /etc/systemd/system/request_ip.service
[Unit]
Description=Change ip to 131 for myProject
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/request_ip.sh

[Install]
WantedBy=multi-user.target
WantedBy=graphical.target
I installed request_ip.service in /etc/systemd/system, and enabled it with systemctl enable request_ip.service.  Checking syslog, I know it ran. I'll update this post if I run into problems.

No comments:

Post a Comment