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=@DEFAULT_HOSTNAME@}
22 # Some systems don't have hostname(1)
25 if [ -z "${1+x}" ]; then
26 if [ -r /proc/sys/kernel/hostname ]; then
27 read name </proc/sys/kernel/hostname && echo "$name"
28 elif type hostname >/dev/null 2>/dev/null; then
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 if [ -w /proc/sys/kernel/hostname ]; then
41 echo "$1" >/proc/sys/kernel/hostname
42 elif [ -n "$1" ] && type hostname >/dev/null 2>&1; then
44 elif sysctl kern.hostname >/dev/null 2>&1; then
45 sysctl -w "kern.hostname=$1" >/dev/null
46 elif sysctl kernel.hostname >/dev/null 2>&1; then
47 sysctl -w "kernel.hostname=$1" >/dev/null
49 # May fail to set a blank hostname
57 ""|"$hostname_default"|localhost|localhost.localdomain)
65 # Always load the hostname variable for future use
66 hostname="$(_hostname)"
67 is_default_hostname "$hostname" && return 0
69 case "$force_hostname" in
70 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;;
73 if [ -n "$old_fqdn" ]; then
74 if ${hfqdn} || ! ${hshort}; then
75 [ "$hostname" = "$old_fqdn" ]
77 [ "$hostname" = "${old_fqdn%%.*}" ]
79 elif [ -n "$old_host_name" ]; then
81 if [ -n "$old_domain_name" ] &&
82 [ "$old_host_name" = "${old_host_name#*.}" ]
85 "$old_host_name.$old_domain_name" ]
87 [ "$hostname" = "$old_host_name" ]
90 [ "$hostname" = "${old_host_name%%.*}" ]
92 [ "$hostname" = "$old_host_name" ]
102 [ "$hostname" = "$1" ] && return 0
103 if valid_domainname "$1"; then
104 syslog info "Setting hostname: $1"
107 syslog err "Invalid hostname: $1"
115 case "$hostname_fqdn" in
116 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) hfqdn=true;;
117 ""|[Ss][Ee][Rr][Vv][Ee][Rr]) ;;
121 need_hostname || return
123 if [ -n "$new_fqdn" ]; then
124 if ${hfqdn} || ! ${hshort}; then
125 try_hostname "$new_fqdn"
127 try_hostname "${new_fqdn%%.*}"
129 elif [ -n "$new_host_name" ]; then
131 if [ -n "$new_domain_name" ] &&
132 [ "$new_host_name" = "${new_host_name#*.}" ]
134 try_hostname "$new_host_name.$new_domain_name"
136 try_hostname "$new_host_name"
139 try_hostname "${new_host_name%%.*}"
141 try_hostname "$new_host_name"
143 elif ! is_default_hostname "$hostname"; then
144 try_hostname "$hostname_default"
148 # For ease of use, map DHCP6 names onto our DHCP4 names
150 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
151 new_fqdn="$new_dhcp6_fqdn"
152 old_fqdn="$old_dhcp6_fqdn"
156 if $if_configured && $if_up && [ "$reason" != ROUTERADVERT ]; then