1 # Set the hostname from DHCP data if required
3 # A hostname can either be a short hostname or a FQDN.
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.
14 # As such, the default is hostname_fqdn=true so that a consistent hostname
16 : ${hostname_fqdn:=true}
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}
22 # Some systems don't have hostname(1)
25 if [ -z "${1+x}" ]; then
26 if type hostname >/dev/null 2>&1; then
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
40 # Always prefer hostname(1) if we have it
41 if type hostname >/dev/null 2>&1; then
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"
50 # We know this will fail, but it will now fail
51 # with an error to stdout
60 case "$hostname_fqdn" in
61 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) hfqdn=true;;
62 ""|[Ss][Ee][Rr][Vv][Ee][Rr]) ;;
69 # Always load the hostname variable for future use
70 hostname="$(_hostname)"
72 ""|"(none)"|localhost|localhost.localdomain|"$hostname_default")
76 case "$force_hostname" in
77 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;;
82 if [ -n "$old_fqdn" ]; then
83 if ${hfqdn} || ! ${hshort}; then
84 [ "$hostname" = "$old_fqdn" ]
86 [ "$hostname" = "${old_fqdn%%.*}" ]
88 elif [ -n "$old_host_name" ]; then
90 if [ -n "$old_domain_name" ] &&
91 [ "$old_host_name" = "${old_host_name#*.}" ]
94 "$old_host_name.$old_domain_name" ]
96 [ "$hostname" = "$old_host_name" ]
99 [ "$hostname" = "${old_host_name%%.*}" ]
101 [ "$hostname" = "$old_host_name" ]
111 [ "$hostname" = "$1" ] && return 0
112 if valid_domainname "$1"; then
113 syslog info "Setting hostname: $1"
116 syslog err "Invalid hostname: $1"
122 need_hostname || return
126 if [ -n "$new_fqdn" ]; then
127 if ${hfqdn} || ! ${hshort}; then
128 try_hostname "$new_fqdn"
130 try_hostname "${new_fqdn%%.*}"
132 elif [ -n "$new_host_name" ]; then
134 if [ -n "$new_domain_name" ] &&
135 [ "$new_host_name" = "${new_host_name#*.}" ]
137 try_hostname "$new_host_name.$new_domain_name"
139 try_hostname "$new_host_name"
142 try_hostname "${new_host_name%%.*}"
144 try_hostname "$new_host_name"
146 elif [ -n "${hostname_default+x}" ]; then
147 try_hostname "$hostname_default"
151 # For ease of use, map DHCP6 names onto our DHCP4 names
153 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
154 new_fqdn="$new_dhcp6_fqdn"
155 old_fqdn="$old_dhcp6_fqdn"