diff options
| author | Roy Marples <roy@marples.name> | 2020-03-31 18:23:05 +0100 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2020-03-31 18:23:05 +0100 |
| commit | 645d14292178c4f6a6f7d359ff8eeb87998f7a35 (patch) | |
| tree | d360f93e826f2b66eead1bce9cf42cf1fb25345d /hooks | |
| parent | 5f275b7bd1ed4d1f830b7a60ba253a98f7ef6127 (diff) | |
| download | dhcpcd-645d14292178c4f6a6f7d359ff8eeb87998f7a35.tar.xz | |
hostname: Default to blank instead of localhost
No kernel sets a default value of localhost.
Diffstat (limited to 'hooks')
| -rw-r--r-- | hooks/30-hostname | 53 |
1 files 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 </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 } |
