From 42a2d6cceef620fc31200b9c4c3fcdde34941306 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 15 Nov 2018 01:49:13 +0100 Subject: [PATCH] Add /etc/hosts and hostname and FQDN setting. --- all_new_2018/set_hostname_and_fqdn.sh | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 all_new_2018/set_hostname_and_fqdn.sh diff --git a/all_new_2018/set_hostname_and_fqdn.sh b/all_new_2018/set_hostname_and_fqdn.sh new file mode 100755 index 0000000..f39a359 --- /dev/null +++ b/all_new_2018/set_hostname_and_fqdn.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# Sets hostname and optionally FQDN. +# +# Calls hostname, writes to /etc/hostname and /etc/hosts. For /etc/hosts +# writing follows recommendations from Debian manual at +# +# (section "The hostname resolution") on how to map hostname and possibly +# FQDN to a permanent IP if present (we assume here any non-private IP +# and non-loopback IP returned by hostname -I to fulfill that criterion +# on our systems) or to 127.0.1.1 if not. On the reasoning for separating +# localhost and hostname mapping to different IPs, see +# . +set -e + +hostname="$1" +fqdn="$2" +if [ "${hostname}" = "" ]; then + echo "Need hostname as argument." + false +fi + +echo "${hostname}" > /etc/hostname +hostname "${hostname}" + +final_ip="127.0.1.1" +for ip in $(hostname -I); do + range_1=$(echo "${ip}" | cut -d "." -f 1) + range_2=$(echo "${ip}" | cut -d "." -f 2) + if [ "${range_1}" -eq 127 ]; then + continue + elif [ "${range_1}" -eq 10 ]; then + continue + elif [ "${range_1}" -eq 172 ]; then + if [ "${range_2}" -ge 16 ] && [ "${range_2}" -le 31 ]; then + continue + fi + elif [ "${range_1}" -eq 192 ]; then + if [ "${range_2}" -eq 168 ]; then + continue + fi + fi + echo 'SETTING' $ip + final_ip="${ip}" +done +echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts +echo "${final_ip} ${fqdn} ${hostname}" >> /etc/hosts -- 2.30.2