I have a script that I wrote that relies on the DHCP hostname to configure itself. When I check $HOSTNAME
on the Pi I see the default "raspberrypi". How can I force it to take the DHCP hostname?
1 Answer
This assumes the Raspbian default of using dhcpcd as the DHCP client.
$ ps ax|grep dhcpcd
247 ? Ss 0:00 /sbin/dhcpcd -q -b
569 pts/0 S+ 0:00 grep dhcpcd
$ head -1 /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
Note the contents of your existing /etc/hostname (in case you need to restore it).
$ cat /etc/hostname
raspberrypi-1
Replace /etc/hostname with localhost from Terminal:
$ echo localhost | sudo tee /etc/hostname
$ sudo shutdown -r now
When finished booting, $HOSTNAME should be set to what your DHCP server returned for option host_name.
Warning: /etc/hosts will not be updated with the new host_name by dhcpcd's default script (/lib/dhcpd/dhcpd-hooks/30-hostname).
Ref: https://www.hiroom2.com/2018/05/30/ubuntu-1804-hostname-en/#sec-5
request host-name