v-change-sys-hostname 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. # info: change hostname
  3. # options: HOSTNAME
  4. #
  5. # example: v-change-sys-hostname mydomain.tld
  6. #
  7. # This function for changing system hostname.
  8. #----------------------------------------------------------#
  9. # Variables & Functions #
  10. #----------------------------------------------------------#
  11. # Argument definition
  12. domain=$1
  13. # Includes
  14. # shellcheck source=/etc/hestiacp/hestia.conf
  15. source /etc/hestiacp/hestia.conf
  16. # shellcheck source=/usr/local/hestia/func/main.sh
  17. source $HESTIA/func/main.sh
  18. # load config file
  19. source_conf "$HESTIA/conf/hestia.conf"
  20. #----------------------------------------------------------#
  21. # Verifications #
  22. #----------------------------------------------------------#
  23. check_args '1' "$#" 'HOSTNAME'
  24. is_format_valid 'domain'
  25. # Perform verification if read-only mode is enabled
  26. check_hestia_demo_mode
  27. #----------------------------------------------------------#
  28. # Action #
  29. #----------------------------------------------------------#
  30. current_hostname="$(hostname)"
  31. if [[ "$current_hostname" == "$domain" ]]; then
  32. echo "Current hostname \"$current_hostname\" is the same as the new one you want to use"
  33. echo "I'm not going to change it"
  34. exit
  35. fi
  36. hostname "$domain"
  37. if [ -d "/etc/sysconfig" ]; then
  38. # RHEL/CentOS/Amazon
  39. touch /etc/sysconfig/network
  40. if [ -z "$(grep HOSTNAME /etc/sysconfig/network)" ]; then
  41. echo "HOSTNAME='$domain'" >>/etc/sysconfig/network
  42. else
  43. sed -i "s/HOSTNAME=.*/HOSTNAME='$domain'/" /etc/sysconfig/network
  44. fi
  45. else
  46. # Debian/Ubuntu
  47. hostnamectl set-hostname "$domain"
  48. echo "$domain" >/etc/hostname
  49. fi
  50. # Update webmail's password plugin configuration
  51. if [ -d /etc/roundcube/ ]; then
  52. sed -i "/password_hestia_host/c\$rcmail_config['password_hestia_host'] = '$domain';" /etc/roundcube/plugins/password/config.inc.php
  53. fi
  54. if [ -d /etc/rainloop/ ]; then
  55. sed -i "/hestia_host/c\hestia_host = \"$domain\"" /etc/rainloop/data/_data_/_default_/configs/plugin-hestia-change-password.ini
  56. fi
  57. if [ -d /etc/snappymail/ ]; then
  58. sed -i "/\"hestia_host\":/c\\\"hestia_host\": \"$domain\"," /etc/snappymail/data/_data_/_default_/configs/plugin-change-password.json
  59. fi
  60. # Update /etc/hosts
  61. if [ -f /etc/hosts ]; then
  62. if grep -q -E "^127\.0\.0\.1\s{1,}${current_hostname}$" /etc/hosts; then
  63. sed -i -E "s/127\.0\.0\.1\s{1,}${current_hostname}/127\.0\.0\.1 ${domain}/" /etc/hosts
  64. else
  65. echo "127.0.0.1 $domain" >>/etc/hosts
  66. fi
  67. # Check whether hostname entries are duplicated and remove all but the last one
  68. ndup_hosts="$(grep -c -E "^127\.0\.0\.1\s{1,}${domain}$" /etc/hosts)"
  69. if [[ "${ndup_hosts}" -gt "1" ]]; then
  70. nlines_to_del="$((ndup_hosts - 1))"
  71. lines_to_del="$(grep -n -E "^127\.0\.0\.1\s{1,}${domain}$" /etc/hosts | head -n${nlines_to_del} | awk -F ':' '{print $1}')"
  72. for i in $lines_to_del; do
  73. if [[ -z $list_lines ]]; then
  74. list_lines="${i}d"
  75. else
  76. list_lines+=";${i}d"
  77. fi
  78. done
  79. sed -i "${list_lines}" /etc/hosts
  80. fi
  81. fi
  82. #----------------------------------------------------------#
  83. # Hestia #
  84. #----------------------------------------------------------#
  85. # Logging
  86. $BIN/v-log-action "system" "Warning" "System" "System hostname changed (Host: $domain)."
  87. log_event "$OK" "$ARGUMENTS"
  88. exit