HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //bin/google_set_hostname
#!/bin/bash
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Deal with a new hostname assignment.

# Legacy behavior is this script setting the hostname and fqdn directly.
# New behavior is to notify the agent when they should be updated.
#
# New behavior should be used (and this function returns 1) when the agent
# is new enough to include the ggactl utility and the agent configuration
# enables the SetFQDN or SetHostname options
retain_legacy_behavior() {
	ggactl=$(which ggactl 2> /dev/null)
	if [ -z "$ggactl" ]; then
		# Agent doesn't have ggactl for communication, legacy behavior.
		return 0
	fi
	# Don't retain legacy behavior if set_hostname or set_fqdn is enabled in
	# the agent.
	if ggactl coreplugin send '{"Command":"agent.config.getoption","Option":"Unstable.SetHostname"}' | grep '"Value":"true"' >/dev/null || ggactl coreplugin send '{"Command":"agent.config.getoption","Option":"Unstable.SetFQDN"}' | grep -i '"Value":"true"' >/dev/null; then
		return 1
	fi
	return 0
}

if ! retain_legacy_behavior; then
	exec ggactl coreplugin send '{"Command":"agent.hostname.reconfigurehostname"}'
fi

if [ -n "$new_host_name" ] && [ -n "$new_ip_address" ]; then
  # Delete entries with new_host_name or new_ip_address in /etc/hosts.
  sed -i"" '/Added by Google/d' /etc/hosts

  # Don't allow DHCP responses with the MDS as the hostname.
  # See: https://github.com/irsl/gcp-dhcp-takeover-code-exec
  if echo "$new_host_name" | grep -iq "metadata.google.internal"; then
    echo "not setting invalid hostname"
  else
    # Add an entry for our new_host_name/new_ip_address in /etc/hosts.
    echo "${new_ip_address} ${new_host_name} ${new_host_name%%.*}  # Added by Google" >> /etc/hosts
  fi

  # Add an entry for reaching the metadata server in /etc/hosts.
  echo "169.254.169.254 metadata.google.internal  # Added by Google" >> /etc/hosts
fi

# /sbin/dhclient-scripts in both ubuntu and centos have some problems for us:
#  1) BOUND doesn't always set hostname (e.g. if old_host_name is unset in
#     precise pangolin)
#  2) Using too long of a FQDN as a hostname causes some tools to break in
#     some distros (e.g. ssh-keygen) and hostname tool complains when given
#     a FQDN that is > 64 bytes.
#
# As a result, we set the host name whenever a new host name is set - ignoring when
# old_hostname is equal to new_host_name's host part, to the truncated unqualified
# domain name.

hostnamecli=$(which hostname 2> /dev/null)
if [ -x "$hostnamecli" ]; then
  old_hostname=$($hostnamecli)
fi

if [ -n "$new_host_name" ] && [ "${new_host_name%%.*}" != "$old_hostname" ] && ! echo "$new_host_name" | grep -iq "metadata.google.internal"; then
  hostname "${new_host_name%%.*}"

  # If NetworkManager is installed set the hostname with nmcli.
  # to resolve issues with NetworkManager resetting the hostname
  # to the FQDN on DHCP renew.
  nmcli=$(which nmcli 2> /dev/null)
  if [ -x "$nmcli" ]; then
    nmcli general hostname "${new_host_name%%.*}"
  fi

  hostnamectl=$(which hostnamectl 2> /dev/null)
  if [ -x "$hostnamectl" ]; then
    # based on the hostnamectl command, attempt to use hostname otherwise fall back to set-hostname
    hostnamectl hostname "${new_host_name%%.*}" || hostnamectl set-hostname "${new_host_name%%.*}"
  fi

  # Restart rsyslog to update the hostname.
  systemctl=$(which systemctl 2> /dev/null)
  if [ -x "$systemctl" ]; then
	# See man 5 systemd.unit for systemd unit naming rules
    hasrsyslog=$($systemctl | grep rsyslog | grep -oE '[a-zA-Z0-9:-_.\]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)')
    if [ ! -z "$hasrsyslog" ]; then
      $systemctl -q --no-block restart "$hasrsyslog"
    fi
  else
    pkill -HUP syslogd
  fi
fi