]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
hostname: Default to blank instead of localhost
authorRoy Marples <roy@marples.name>
Tue, 31 Mar 2020 17:23:05 +0000 (18:23 +0100)
committerRoy Marples <roy@marples.name>
Tue, 31 Mar 2020 17:23:05 +0000 (18:23 +0100)
No kernel sets a default value of localhost.

hooks/30-hostname

index 53ae6b1147ee66383311b28220e539e03e4283cb..3ac73ea443cfd50d96af2c28b8bd590e5dfcbc28 100644 (file)
 
 # 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 </proc/sys/kernel/hostname && echo "$name"
+               elif type hostname >/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
 }