Skip to content

Commit a4a861a

Browse files
authored
Update v-change-sys-hostname (hestiacp#4030)
Update v-change-sys-hostname to replace current hostname in /etc/hosts instead of just add it
1 parent d088949 commit a4a861a

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

bin/v-change-sys-hostname

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,60 @@ check_hestia_demo_mode
3535
# Action #
3636
#----------------------------------------------------------#
3737

38-
hostname "$domain"
38+
current_hostname="$(hostname)"
39+
if [[ "$current_hostname" == "$domain" ]]; then
40+
echo "Current hostname \"$current_hostname\" is the same as the new one you want to use"
41+
echo "I'm not going to change it"
42+
exit
43+
fi
3944

45+
hostname "$domain"
4046
if [ -d "/etc/sysconfig" ]; then
41-
# RHEL/CentOS/Amazon
42-
touch /etc/sysconfig/network
43-
if [ -z "$(grep HOSTNAME /etc/sysconfig/network)" ]; then
44-
echo "HOSTNAME='$domain'" >> /etc/sysconfig/network
45-
else
46-
sed -i "s/HOSTNAME=.*/HOSTNAME='$domain'/" /etc/sysconfig/network
47-
fi
47+
# RHEL/CentOS/Amazon
48+
touch /etc/sysconfig/network
49+
if [ -z "$(grep HOSTNAME /etc/sysconfig/network)" ]; then
50+
echo "HOSTNAME='$domain'" >>/etc/sysconfig/network
51+
else
52+
sed -i "s/HOSTNAME=.*/HOSTNAME='$domain'/" /etc/sysconfig/network
53+
fi
4854
else
49-
# Debian/Ubuntu
50-
hostnamectl set-hostname "$domain"
51-
echo "$domain" > /etc/hostname
55+
# Debian/Ubuntu
56+
hostnamectl set-hostname "$domain"
57+
echo "$domain" >/etc/hostname
5258
fi
5359

54-
# Update Roundcube password plugin configuration
60+
# Update webmail's password plugin configuration
5561
if [ -d /etc/roundcube/ ]; then
56-
sed -i "/password_hestia_host/c\$rcmail_config['password_hestia_host'] = '$domain';" /etc/roundcube/plugins/password/config.inc.php
62+
sed -i "/password_hestia_host/c\$rcmail_config['password_hestia_host'] = '$domain';" /etc/roundcube/plugins/password/config.inc.php
5763
fi
5864
if [ -d /etc/rainloop/ ]; then
59-
sed -i "/hestia_host/c\hestia_host = \"$domain\"" /etc/rainloop/data/_data_/_default_/configs/plugin-hestia-change-password.ini
65+
sed -i "/hestia_host/c\hestia_host = \"$domain\"" /etc/rainloop/data/_data_/_default_/configs/plugin-hestia-change-password.ini
6066
fi
6167
if [ -d /etc/snappymail/ ]; then
62-
sed -i "/\"hestia_host\":/c\\\"hestia_host\": \"$domain\"," /etc/snappymail/data/_data_/_default_/configs/plugin-change-password.json
68+
sed -i "/\"hestia_host\":/c\\\"hestia_host\": \"$domain\"," /etc/snappymail/data/_data_/_default_/configs/plugin-change-password.json
6369
fi
6470

71+
# Update /etc/hosts
6572
if [ -f /etc/hosts ]; then
66-
if ! cat /etc/hosts | grep $domain > /dev/null; then
67-
echo "127.0.0.1 $domain" >> /etc/hosts
68-
fi
73+
if grep -q -E "^127\.0\.0\.1\s{1,}${current_hostname}$" /etc/hosts; then
74+
sed -i -E "s/127\.0\.0\.1\s{1,}${current_hostname}/127\.0\.0\.1 ${domain}/" /etc/hosts
75+
else
76+
echo "127.0.0.1 $domain" >>/etc/hosts
77+
fi
78+
# Check whether hostname entries are duplicated and remove all but the last one
79+
ndup_hosts="$(grep -c -E "^127\.0\.0\.1\s{1,}${domain}$" /etc/hosts)"
80+
if [[ "${ndup_hosts}" -gt "1" ]]; then
81+
nlines_to_del="$((ndup_hosts - 1))"
82+
lines_to_del="$(grep -n -E "^127\.0\.0\.1\s{1,}${domain}$" /etc/hosts | head -n${nlines_to_del} | awk -F ':' '{print $1}')"
83+
for i in $lines_to_del; do
84+
if [[ -z $list_lines ]]; then
85+
list_lines="${i}d"
86+
else
87+
list_lines+=";${i}d"
88+
fi
89+
done
90+
sed -i "${list_lines}" /etc/hosts
91+
fi
6992
fi
7093

7194
#----------------------------------------------------------#

0 commit comments

Comments
 (0)