From b78f0c7c085db22868caf3f4dd241ce1abb0f0fb Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Tue, 31 Mar 2020 18:23:05 +0100 Subject: [PATCH] hostname: Default to blank instead of localhost No kernel sets a default value of localhost. --- hooks/30-hostname | 53 ++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/hooks/30-hostname b/hooks/30-hostname index 53ae6b11..3ac73ea4 100644 --- a/hooks/30-hostname +++ b/hooks/30-hostname @@ -17,16 +17,16 @@ # If we used to set the hostname, but relinquish control of it, we should # reset to the default value. -: ${hostname_default=localhost} +: ${hostname_default=} # Some systems don't have hostname(1) _hostname() { if [ -z "${1+x}" ]; then - if type hostname >/dev/null 2>&1; then - hostname - elif [ -r /proc/sys/kernel/hostname ]; then + if [ -r /proc/sys/kernel/hostname ]; then read name /dev/null 2>/dev/null; then + hostname elif sysctl kern.hostname >/dev/null 2>&1; then sysctl -n kern.hostname elif sysctl kernel.hostname >/dev/null 2>&1; then @@ -37,48 +37,39 @@ _hostname() return $? fi - # Always prefer hostname(1) if we have it - if type hostname >/dev/null 2>&1; then - hostname "$1" - elif [ -w /proc/sys/kernel/hostname ]; then + if [ -w /proc/sys/kernel/hostname ]; then echo "$1" >/proc/sys/kernel/hostname + elif [ -n "$1" ] && type hostname >/dev/null 2>&1; then + hostname "$1" elif sysctl kern.hostname >/dev/null 2>&1; then - sysctl -w "kern.hostname=$1" + sysctl -w "kern.hostname=$1" >/dev/null elif sysctl kernel.hostname >/dev/null 2>&1; then - sysctl -w "kernel.hostname=$1" + sysctl -w "kernel.hostname=$1" >/dev/null else - # We know this will fail, but it will now fail - # with an error to stdout + # May fail to set a blank hostname hostname "$1" fi } -set_hostname_vars() +is_default_hostname() { - hfqdn=false - hshort=false - case "$hostname_fqdn" in - [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) hfqdn=true;; - ""|[Ss][Ee][Rr][Vv][Ee][Rr]) ;; - *) hshort=true;; + case "$1" in + ""|"(none)"|localhost|localhost.localdomain|"$hostname_default") + return 0;; esac + return 1 } need_hostname() { # Always load the hostname variable for future use hostname="$(_hostname)" - case "$hostname" in - ""|"(none)"|localhost|localhost.localdomain|"$hostname_default") - return 0;; - esac + is_default_hostname "$hostname" && return 0 case "$force_hostname" in [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;; esac - set_hostname_vars - if [ -n "$old_fqdn" ]; then if ${hfqdn} || ! ${hshort}; then [ "$hostname" = "$old_fqdn" ] @@ -119,9 +110,15 @@ try_hostname() set_hostname() { - need_hostname || return + hfqdn=false + hshort=false + case "$hostname_fqdn" in + [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) hfqdn=true;; + ""|[Ss][Ee][Rr][Vv][Ee][Rr]) ;; + *) hshort=true;; + esac - set_hostname_vars + need_hostname || return if [ -n "$new_fqdn" ]; then if ${hfqdn} || ! ${hshort}; then @@ -143,7 +140,7 @@ set_hostname() else try_hostname "$new_host_name" fi - elif [ -n "${hostname_default+x}" ]; then + elif ! is_default_hostname "$hostname"; then try_hostname "$hostname_default" fi } -- 2.47.3