-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-configure-hostname.sh
executable file
·111 lines (98 loc) · 3.73 KB
/
08-configure-hostname.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
#-------------------------------------------------------------------------------
# Set the hostname, by:
#
# 1. checking the current hostname;
# 2. asking if the user wants to change it or creat one if unset; and
# 3. setting a hostname
#
# Based on this RedHat guide:
#
# - https://www.redhat.com/sysadmin/configure-hostname-linux
#
# N.B.
# This script needs to be run as "sudo".
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imported shared variables.
#-------------------------------------------------------------------------------
. ./linshafun/linshafun.var
#-------------------------------------------------------------------------------
# Imported project specific variables.
#-------------------------------------------------------------------------------
. ./linux-setup.var
#-------------------------------------------------------------------------------
# Imported shared functions.
#-------------------------------------------------------------------------------
. ./linshafun/comments.sh
# . ./linshafun/docker-env-variables.sh
# . ./linshafun/docker-images.sh
# . ./linshafun/docker-services.sh
# . ./linshafun/files-directories.sh
# . ./linshafun/firewall.sh
# . ./linshafun/host-env-variables.sh
# . ./linshafun/host-information.sh
# . ./linshafun/network.sh
. ./linshafun/ownership-permissions.sh
# . ./linshafun/packages.sh
# . ./linshafun/services.sh
. ./linshafun/setup-config.sh
. ./linshafun/setup.sh
# . ./linshafun/ssh-keys.sh
# . ./linshafun/text.sh
. ./linshafun/user-input.sh
#-------------------------------------------------------------------------------
# Config key variable.
#-------------------------------------------------------------------------------
CONFIG_KEY='configuredHostname'
#-------------------------------------------------------------------------------
# Asks the user to change the hostname. Runs "setNewHostname" if yes, makes no
# changes if no.
#-------------------------------------------------------------------------------
changeHostname () {
promptForUserInput 'Would you like to change the hostname (y/n)?'
HOSTNAME_CHANGE_YN="$(getUserInputYN)"
if [ "$HOSTNAME_CHANGE_YN" = true ]; then
setNewHostname
else
echoComment 'No changes made to hostname.'
fi
}
#-------------------------------------------------------------------------------
# Checks the existing hostname.
#-------------------------------------------------------------------------------
checkHostname () {
local CURRENT_HOSTNAME="$(hostname)"
if [ -z "$CURRENT_HOSTNAME" ]; then
echoComment 'No hostname set.'
else
echoComment 'The current hostname is:'
echoComment "$CURRENT_HOSTNAME"
fi
}
#-------------------------------------------------------------------------------
# Sets a new user inputed hostname.
#-------------------------------------------------------------------------------
setNewHostname () {
promptForUserInput 'What is your new hostname (my.hostname.com)?'
HOSTNAME="$(getUserInput)"
echoComment 'Setting hostname to:'
echoComment "$HOSTNAME"
echoComment 'You may be asked to authenticate.'
hostnamectl set-hostname "$HOSTNAME"
echoComment 'Hostname is now set to:'
hostname
}
#-------------------------------------------------------------------------------
# Executes the main functions of the script.
#-------------------------------------------------------------------------------
mainScript () {
checkHostname
changeHostname
}
#-------------------------------------------------------------------------------
# Run the script.
#-------------------------------------------------------------------------------
initialiseScript "$CONFIG_KEY"
mainScript
finaliseScript "$CONFIG_KEY"