|
23 | 23 | SED_I="sed -i''" |
24 | 24 | fi |
25 | 25 |
|
| 26 | +# Detects suitable running syslog service and restarts it |
| 27 | +# to propagate the changed hostname into the logs |
| 28 | +# (otherwise old or default value remains until reboot) |
| 29 | +function restart_syslog() { |
| 30 | + for _name in syslog syslogd rsyslog; do |
| 31 | + if LANG=C systemctl is-active "${_name}" >/dev/null 2>&1; then |
| 32 | + systemctl restart "${_name}" && return |
| 33 | + elif service "${_name}" status >/dev/null 2>&1; then |
| 34 | + service "${_name}" restart && return |
| 35 | + fi |
| 36 | + done |
| 37 | +} |
| 38 | + |
26 | 39 | function set_hostname() { |
27 | | - local hostname=$1 |
| 40 | + local _hostname=$1 |
| 41 | + |
| 42 | + # remember currently set hostname to detect a change |
| 43 | + local _old_hostname |
| 44 | + _old_hostname=$(hostname) |
28 | 45 |
|
29 | 46 | if [ -d /run/systemd/system/ ] && hostnamectl status >/dev/null 2>/dev/null; then |
30 | | - hostnamectl set-hostname --static "${hostname}" |
| 47 | + hostnamectl set-hostname --static "${_hostname}" |
31 | 48 | else |
32 | 49 | if [ -f /etc/sysconfig/network ]; then |
33 | 50 | eval "${SED_I} '/^HOSTNAME=.*$/d' /etc/sysconfig/network" |
34 | | - echo "HOSTNAME=${hostname}" >>/etc/sysconfig/network |
| 51 | + echo "HOSTNAME=${_hostname}" >>/etc/sysconfig/network |
35 | 52 | elif [ "${_kernel}" = 'FreeBSD' ]; then |
36 | | - sysrc hostname="${hostname}" |
| 53 | + sysrc hostname="${_hostname}" |
37 | 54 | else |
38 | | - echo "${hostname}" >/etc/hostname |
| 55 | + echo "${_hostname}" >/etc/hostname |
39 | 56 | fi |
40 | 57 |
|
41 | | - hostname "${hostname}" |
| 58 | + hostname "${_hostname}" |
| 59 | + fi |
| 60 | + |
| 61 | + # restart syslog if actual hostname changed |
| 62 | + if [ "${_old_hostname}" != "$(hostname)" ]; then |
| 63 | + restart_syslog |
42 | 64 | fi |
43 | 65 | } |
44 | 66 |
|
45 | 67 | function set_domainname() { |
46 | | - domain=$1 |
| 68 | + local _domain=$1 |
| 69 | + |
| 70 | + touch /etc/resolv.conf |
47 | 71 | eval "${SED_I} -e '/^domain .*/d' /etc/resolv.conf" |
48 | | - echo "domain ${domain}" >>/etc/resolv.conf |
| 72 | + echo "domain ${_domain}" >>/etc/resolv.conf |
49 | 73 | } |
50 | 74 |
|
51 | 75 | function get_first_ip() { |
@@ -131,17 +155,17 @@ if [ -n "${name}" ]; then |
131 | 155 | domain='' |
132 | 156 | fi |
133 | 157 |
|
| 158 | + if [ -n "${domain}" ]; then |
| 159 | + set_domainname "${domain}" |
| 160 | + fi |
| 161 | + |
134 | 162 | # FreeBSD |
135 | 163 | if [ "${_kernel}" = 'FreeBSD' ]; then |
136 | 164 | set_hostname "${name}" |
137 | 165 | else |
138 | 166 | set_hostname "${hostname}" |
139 | 167 | fi |
140 | 168 |
|
141 | | - if [ -n "${domain}" ]; then |
142 | | - set_domainname "${domain}" |
143 | | - fi |
144 | | - |
145 | 169 | if [ -n "${DNS_HOSTNAME}" ]; then |
146 | 170 | host_ip=$first_ip |
147 | 171 | else |
|
0 commit comments