diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index 9fb97f157f..4087b7cc8d 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -1154,12 +1154,15 @@ def set_hostname(self, hostname): self._run_command_without_raising(["hostname", hostname], log_error=False) def set_dhcp_hostname(self, hostname): + # ensure localhost is not sent to the dhcp server + if hostname.lower() == "localhost" or hostname.lower() == "localhost.localdomain": + hostname = "" autosend = r'^[^#]*?send\s*host-name.*?(|gethostname[(,)])' dhclient_files = ['/etc/dhcp/dhclient.conf', '/etc/dhcp3/dhclient.conf', '/etc/dhclient.conf'] for conf_file in dhclient_files: if not os.path.isfile(conf_file): continue - if fileutil.findre_in_file(conf_file, autosend): + if hostname != "" and fileutil.findre_in_file(conf_file, autosend): # Return if auto send host-name is configured return fileutil.update_conf_file(conf_file, diff --git a/azurelinuxagent/common/osutil/redhat.py b/azurelinuxagent/common/osutil/redhat.py index 312dd16084..7a595ebeb4 100644 --- a/azurelinuxagent/common/osutil/redhat.py +++ b/azurelinuxagent/common/osutil/redhat.py @@ -84,6 +84,9 @@ def set_hostname(self, hostname): self._run_command_without_raising(["hostname", hostname], log_error=False) def set_dhcp_hostname(self, hostname): + # ensure localhost is not sent to the dhcp server + if hostname.lower() == "localhost" or hostname.lower() == "localhost.localdomain": + hostname = "" ifname = self.get_if_name() filepath = "/etc/sysconfig/network-scripts/ifcfg-{0}".format(ifname) fileutil.update_conf_file(filepath, diff --git a/azurelinuxagent/common/osutil/suse.py b/azurelinuxagent/common/osutil/suse.py index 52fd3ce565..886e9f9de4 100644 --- a/azurelinuxagent/common/osutil/suse.py +++ b/azurelinuxagent/common/osutil/suse.py @@ -104,6 +104,12 @@ def set_hostname(self, hostname): ) def set_dhcp_hostname(self, hostname): + # ensure localhost is not sent to the dhcp server + if hostname.lower() == "localhost" or hostname.lower() == "localhost.localdomain": + # if the DHCLIENT_HOSTNAME_OPTION is empty it will not send a hostname to the dhcp server + # reference: https://manpages.opensuse.org/Tumbleweed/wicked/ifcfg-dhcp.5.en.html + hostname = "" + dhcp_config_file_path = '/etc/sysconfig/network/dhcp' hostname_send_setting = fileutil.get_line_startingwith( 'DHCLIENT_HOSTNAME_OPTION', dhcp_config_file_path