0

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?

2
  • the RPi has to request the hostname from the DHCP server at the time of the DHCP negotiation. ... check the DHCP client configuration. ... there should be an option similar to request host-name
    – jsotola
    Commented Feb 26, 2018 at 18:52
  • The hostname is set on the Raspberry Pi. The IP address is given by the DHCP server. The DNS server connects the IP to the Hostname. Example. If you open CMD and type ping www.google.com it will use Domain Name Resolution to find the IP address associated with google.com. Commented Feb 26, 2018 at 21:41

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.