1# Set the hostname from DHCP data if required 2 3# A hostname can either be a short hostname or a FQDN. 4# hostname_fqdn=true 5# hostname_fqdn=false 6# hostname_fqdn=server 7 8# A value of server means just what the server says, don't manipulate it. 9# This could lead to an inconsistent hostname on a DHCPv4 and DHCPv6 network 10# where the DHCPv4 hostname is short and the DHCPv6 has an FQDN. 11# DHCPv6 has no hostname option. 12# RFC4702 section 3.1 says FQDN should be prefered over hostname. 13# 14# As such, the default is hostname_fqdn=true so that a consistent hostname 15# is always assigned. 16: ${hostname_fqdn:=true} 17 18# If we used to set the hostname, but relinquish control of it, we should 19# reset to the default value. 20: ${hostname_default=localhost} 21 22# Some systems don't have hostname(1) 23_hostname() 24{ 25 if [ -z "${1+x}" ]; then 26 if type hostname >/dev/null 2>&1; then 27 hostname 28 elif [ -r /proc/sys/kernel/hostname ]; then 29 read name </proc/sys/kernel/hostname && echo "$name" 30 elif sysctl kern.hostname >/dev/null 2>&1; then 31 sysctl -n kern.hostname 32 elif sysctl kernel.hostname >/dev/null 2>&1; then 33 sysctl -n kernel.hostname 34 else 35 return 1 36 fi 37 return $? 38 fi 39 40 # Always prefer hostname(1) if we have it 41 if type hostname >/dev/null 2>&1; then 42 hostname "$1" 43 elif [ -w /proc/sys/kernel/hostname ]; then 44 echo "$1" >/proc/sys/kernel/hostname 45 elif sysctl kern.hostname >/dev/null 2>&1; then 46 sysctl -w "kern.hostname=$1" 47 elif sysctl kernel.hostname >/dev/null 2>&1; then 48 sysctl -w "kernel.hostname=$1" 49 else 50 # We know this will fail, but it will now fail 51 # with an error to stdout 52 hostname "$1" 53 fi 54} 55 56set_hostname_vars() 57{ 58 hfqdn=false 59 hshort=false 60 case "$hostname_fqdn" in 61 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) hfqdn=true;; 62 ""|[Ss][Ee][Rr][Vv][Ee][Rr]) ;; 63 *) hshort=true;; 64 esac 65} 66 67need_hostname() 68{ 69 # Always load the hostname variable for future use 70 hostname="$(_hostname)" 71 case "$hostname" in 72 ""|"(none)"|localhost|localhost.localdomain|"$hostname_default") 73 return 0;; 74 esac 75 76 case "$force_hostname" in 77 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;; 78 esac 79 80 set_hostname_vars 81 82 if [ -n "$old_fqdn" ]; then 83 if ${hfqdn} || ! ${hshort}; then 84 [ "$hostname" = "$old_fqdn" ] 85 else 86 [ "$hostname" = "${old_fqdn%%.*}" ] 87 fi 88 elif [ -n "$old_host_name" ]; then 89 if ${hfqdn}; then 90 if [ -n "$old_domain_name" ] && 91 [ "$old_host_name" = "${old_host_name#*.}" ] 92 then 93 [ "$hostname" = \ 94 "$old_host_name.$old_domain_name" ] 95 else 96 [ "$hostname" = "$old_host_name" ] 97 fi 98 elif ${hshort}; then 99 [ "$hostname" = "${old_host_name%%.*}" ] 100 else 101 [ "$hostname" = "$old_host_name" ] 102 fi 103 else 104 # No old hostname 105 false 106 fi 107} 108 109try_hostname() 110{ 111 [ "$hostname" = "$1" ] && return 0 112 if valid_domainname "$1"; then 113 syslog info "Setting hostname: $1" 114 _hostname "$1" 115 else 116 syslog err "Invalid hostname: $1" 117 fi 118} 119 120set_hostname() 121{ 122 need_hostname || return 123 124 set_hostname_vars 125 126 if [ -n "$new_fqdn" ]; then 127 if ${hfqdn} || ! ${hshort}; then 128 try_hostname "$new_fqdn" 129 else 130 try_hostname "${new_fqdn%%.*}" 131 fi 132 elif [ -n "$new_host_name" ]; then 133 if ${hfqdn}; then 134 if [ -n "$new_domain_name" ] && 135 [ "$new_host_name" = "${new_host_name#*.}" ] 136 then 137 try_hostname "$new_host_name.$new_domain_name" 138 else 139 try_hostname "$new_host_name" 140 fi 141 elif ${hshort}; then 142 try_hostname "${new_host_name%%.*}" 143 else 144 try_hostname "$new_host_name" 145 fi 146 elif [ -n "${hostname_default+x}" ]; then 147 try_hostname "$hostname_default" 148 fi 149} 150 151# For ease of use, map DHCP6 names onto our DHCP4 names 152case "$reason" in 153BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6) 154 new_fqdn="$new_dhcp6_fqdn" 155 old_fqdn="$old_dhcp6_fqdn" 156 ;; 157esac 158 159if $if_up; then 160 set_hostname 161fi 162