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>.
17 if [ "${hostname}" = "" ]; then
18 echo "Need hostname as argument."
21 echo "${hostname}" > /etc/hostname
22 hostname "${hostname}"
25 for ip in $(hostname -I); do
26 range_1=$(echo "${ip}" | cut -d "." -f 1)
27 range_2=$(echo "${ip}" | cut -d "." -f 2)
28 if [ "${range_1}" -eq 127 ]; then
30 elif [ "${range_1}" -eq 10 ]; then
32 elif [ "${range_1}" -eq 172 ]; then
33 if [ "${range_2}" -ge 16 ] && [ "${range_2}" -le 31 ]; then
36 elif [ "${range_1}" -eq 192 ]; then
37 if [ "${range_2}" -eq 168 ]; then
44 echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
45 echo "${final_ip} ${fqdn} ${hostname}" >> /etc/hosts