-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostname.sh
executable file
·42 lines (33 loc) · 1007 Bytes
/
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
#!/bin/bash
if [ "$1" = "" ]; then
echo "usage: $0 <new-hostname>"
exit 1
elif ! [[ $1 =~ ^[a-z0-9.-]+$ ]]; then
echo "error: parameter $1 not conforming hostname format"
exit 1
fi
HOST=$1
SHORT="${HOST%.*}"
hostname $HOST
if grep -q $SHORT /etc/hosts; then
sed -i -e "/$SHORT/d" /etc/hosts
fi
if [ -f /etc/sysconfig/network ]; then
sed -i -e '/HOSTNAME=/d' /etc/sysconfig/network
echo "HOSTNAME=$HOST" >> /etc/sysconfig/network
fi
if [ -f /etc/rc.conf ]; then
sed -e '/hostname=/d' /etc/rc.conf >/etc/rc.conf.$$
echo "hostname=$HOST" >>/etc/rc.conf.$$
cat /etc/rc.conf.$$ >/etc/rc.conf
fi
if [ -f /etc/ssmtp/ssmtp.conf ]; then
sed -i -e '/hostname=/d' /etc/ssmtp/ssmtp.conf
echo "hostname=$HOST" >> /etc/ssmtp/ssmtp.conf
fi
if [ -x /usr/bin/hostnamectl ]; then
/usr/bin/hostnamectl set-hostname $HOST
fi
if [ -f /etc/HOSTNAME ]; then echo $HOST >/etc/HOSTNAME; fi
if [ -f /etc/hostname ]; then echo $HOST >/etc/hostname; fi
if [ -f /etc/mailname ]; then echo $HOST >/etc/mailname; fi