Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 134879  / 12 Months ago, tue, may 30, 2023, 10:13:58

I want Ubuntu to get the hostname and DNS name from a DHCP client. The default installation of Ubuntu 11.10 (Oneiric Ocelot) does not do that.



The same question was asked and is unsolved on Ubuntu Forums.


More From » networking

 Answers
4

There is a way to do it with a little script for a dhcp hook as described here.



Create a new file:



sudoedit /etc/dhcp/dhclient-exit-hooks.d/hostname


and paste the following code:



#!/bin/sh
# Filename: /etc/dhcp/dhclient-exit-hooks.d/hostname
# Purpose: Used by dhclient-script to set the hostname of the system
# to match the DNS information for the host as provided by
# DHCP.
#


# Do not update hostname for virtual machine IP assignments
if [ "$interface" != "eth0" ] && [ "$interface" != "wlan0" ]
then
return
fi


if [ "$reason" != BOUND ] && [ "$reason" != RENEW ]
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
then
return
fi

echo dhclient-exit-hooks.d/hostname: Dynamic IP address = $new_ip_address
hostname=$(host $new_ip_address | cut -d ' ' -f 5 | sed -r 's/((.*)[^.]).?/1/g' )
echo $hostname > /etc/hostname
hostname $hostname
echo dhclient-exit-hooks.d/hostname: Dynamic Hostname = $hostname


Replace eth0 and wlan0 with the names of the interfaces from which you want to obtain the hostname. In most cases eth0 and wlan0 should stay the same.



Make sure it is readable...



chmod a+r /etc/dhcp/dhclient-exit-hooks.d/hostname


That's all. On the next dhcp response your hostname will update automatically.


[#40336] Thursday, June 1, 2023, 12 Months  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
armis

Total Points: 38
Total Questions: 106
Total Answers: 118

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;