From e5c6639a4a700d1dd26829a7e62b3f1bb4825058 Mon Sep 17 00:00:00 2001 From: Paul Edwards Date: Fri, 22 Sep 2023 16:31:45 +0100 Subject: [PATCH 1/2] Do not send localhost as the name for dhclient This causes localhost to be added to Azure DNS and a call to `nslookup localhost` from somewhere on the VNET can get another VMs private IP address. --- azurelinuxagent/common/osutil/default.py | 3 +++ azurelinuxagent/common/osutil/redhat.py | 3 +++ azurelinuxagent/common/osutil/suse.py | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index 9fb97f157f..dfe1b6b06a 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -1154,6 +1154,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 = "" 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: 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 From a42b4b8b607faf7a11a17cbdfdbb9c5dc807103c Mon Sep 17 00:00:00 2001 From: Paul Edwards Date: Tue, 26 Sep 2023 13:56:49 +0100 Subject: [PATCH 2/2] Don't use dhcp autosend when the hostname is localhost --- azurelinuxagent/common/osutil/default.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index dfe1b6b06a..4087b7cc8d 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -1162,7 +1162,7 @@ def set_dhcp_hostname(self, hostname): 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,