# 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
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" ]
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
else
try_hostname "$new_host_name"
fi
- elif [ -n "${hostname_default+x}" ]; then
+ elif ! is_default_hostname "$hostname"; then
try_hostname "$hostname_default"
fi
}