-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosx_change_hostname.sh
executable file
·60 lines (43 loc) · 1.03 KB
/
osx_change_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
#!/usr/bin/env bash
# vim: filetype=sh
##### modeline end #####
set -e
set -u
set -o pipefail
IFS=$'\n\t'
readonly PROGNAME=$(basename $0)
readonly PROGDIR=$(readlink $(pwd)) # OSX
readonly ARGS=("$@")
function change_hostname() {
# Useful for debug
#echo $FUNCNAME $@
local newname="$1"
sudo scutil --set ComputerName "$newname"
sudo scutil --set LocalHostName "$newname"
sudo scutil --set HostName "$newname"
dscacheutil -flushcache
sudo shutdown -r +2 'Computer will be rebooted for new hostname to take effect'
}
function usage() {
# Useful for debug
#echo $FUNCNAME $@
echo 'Usage: ./'${PROGNAME}' newhostname'
exit 1
}
function main() {
echo 'You will prompted for your sudo password'
echo 'The computer will be rebooted once the change is complete.'
read -p 'Do you wish to continue (y/n):' prompt
if [ "${prompt}" == "y" ]; then
if [ ${#ARGS[@]} -eq 1 ]; then
change_hostname ${ARGS[0]}
else
usage
fi
else
echo 'Exiting...'
exit 0
fi
}
# Run script
main