-
Notifications
You must be signed in to change notification settings - Fork 0
/
changehostname
147 lines (121 loc) · 4.84 KB
/
changehostname
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /bin/bash
#HELP00: Script Name: changehostname
#HELP01: Purpose: simple one step to change hostname
#HELP02: Owner: David Eisenhardt
#HELP03: License: GPL v3
#HELP04: Created: 2018 August
#HELP05: Category: system utilities
#HELP06: Input File: stdin
#HELP07: Output File: stdout
#HELP08: Log path: na
#HELP09: Syntax: sudo changehostname [hostname]
#HELP10: Example: sudo changehostname mynewhostname
#HELP11: Commands: sed, cat
#HELP12: Production Files: /etc/hosts /etc/hostname
#HELP13: Ancillary Programs: na
#HELP14: Ancillary Scripts: na
#HELP15: Local Scripts: no dependencies
#HELP16: crontask/systemd srvc: na
#HELP17: task/service schedule: na
#HELP18: Kernel Version: any
#HELP19: DB Version: na
#HELP20: Audit Date:
#HELP21: Auditor:
#HELP22: Approve Date(s):
#HELP23: Approver(s):
#HELP24: Date Checked Into CVS:
#HELP25: Checked Into CVS By:
#HELP26: Version: 1.1
#HELP27: Changes: add/update output messages
#HELP28: Github url: https://github.com/deisenhardt/changehostname/
#HELP29:
#HELP30:
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Exit codes !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# 3 not sudo/root
# 4 bad input string
# 5 no input string
##############################################################
# flow of script / notes / todo ##############################
##############################################################
# some distros have instant / non-reboot-needed options to update the hostname
# outside the scope for this script
#{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
# Define functions {}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
#{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
usage()
{
printf "\n\n Usage: sudo changehostname <new hostname> \
\n\n updates /etc/hostname & /etc/hosts with user provided hostname \
\n\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \
\n !! hostname can only contain specific characters !! \
\n !! valid characters are ASCII a-z, 0-9, & - (hyphen) !! \
\n !! !! \
\n !! changehostname must be run as root or sudo !! \
\n !! !! \
\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n\n"
}
sudo_check(){
if [ "$(id -u)" -ne 0 ]; then
usage && exit 3
else
sudook=1
fi
}
input_check()
{
# ^ negates the string like != and + checks for more than one occurrence
# this bash regex check will not work in ksh
if [[ "$1" =~ [^[:alnum:]]+ ]]; then
usage && exit 4
else #no special characters
newhostname=$1
fi
}
get_hostname()
{ # get the current hostname
host1=$(cat /etc/hostname)
host2=$(cat /etc/hosts | grep "127\.0" | grep -v "localhost" | head -1 | awk '{print $2}')
# this should cover the broadest majority of systems as
# typically hosts will have only two entries in the 127 range
# 127.0.0.1 localhost
# 127.0.1.1 hostname
}
change_hostname()
{ # we are applying changes as sudo
printf "\n hostname in /etc/hostname \n"
cat /etc/hostname
echo "setting hostname to $newhostname in /etc/hostname"
sed -i "s/$host1/$newhostname/g" /etc/hostname
printf "\n hostname in /etc/hostname \n"
cat /etc/hostname
printf "\n hostname in /etc/hosts \n"
cat /etc/hosts | grep "127\.0" | grep -v "localhost"
echo "setting hostname to $newhostname in /etc/hosts"
sed -i "s/$host2/$newhostname/g" /etc/hosts
printf "\n hostname in /etc/hosts \n"
cat /etc/hosts | grep "127\.0" | grep -v "localhost"
# the change will affect all matches of the current hostname string
# in /etc/hosts so even if there is also an ipv6 equivalent of
# 127.0.1.1 where we checked to get the hostname within /etc/hosts,
# the /g behaviour in sed will still update all matches
# /g(lobally) in the file
printf "\n The hostname change will not be complete until the system is restarted \n\n"
}
##############################################################
# main program ###############################################
##############################################################
if [ $# -lt 1 ]; then
usage && exit 5
else
sudo_check
input_check $@
get_hostname
if [[ "$sudook" -eq 1 && -n "$newhostname" ]]; then
change_hostname $@
fi
fi
##############################################################
# End ########################################################
##############################################################