2 # Sets hostname and optionally FQDN.
4 # Calls hostname, writes to /etc/hostname and /etc/hosts. For /etc/hosts
5 # writing follows recommendations from Debian manual at
6 # <https://www.debian.org/doc/manuals/debian-reference/ch05.en.html>
7 # (section "The hostname resolution") on how to map hostname and possibly
8 # FQDN to a permanent IP if present (we assume here any non-private IP
9 # and non-loopback IP returned by hostname -I to fulfill that criterion
10 # on our systems) or to 127.0.1.1 if not. On the reasoning for separating
11 # localhost and hostname mapping to different IPs, see
12 # <https://unix.stackexchange.com/a/13087>.
19 if [ "${hostname}" = "" ]; then
20 echo "Need hostname as argument."
23 echo "${hostname}" > /etc/hostname
24 hostname "${hostname}"
27 for ip in $(hostname -I); do
28 if [ $(echo "${ip}" | grep ':' | wc -l) -eq 1 ]; then
31 range_1=$(echo "${ip}" | cut -d "." -f 1)
32 range_2=$(echo "${ip}" | cut -d "." -f 2)
33 if [ "${range_1}" -eq 127 ]; then
35 elif [ "${range_1}" -eq 10 ]; then
37 elif [ "${range_1}" -eq 172 ]; then
38 if [ "${range_2}" -ge 16 ] && [ "${range_2}" -le 31 ]; then
41 elif [ "${range_1}" -eq 192 ]; then
42 if [ "${range_2}" -eq 168 ]; then
49 echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
50 echo "${final_ip} ${fqdn} ${hostname}" >> /etc/hosts