2017-11-22 16:36:25 -06:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
|
|
|
CURRENT_HOSTNAME=`hostname`
|
|
|
|
HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']`
|
|
|
|
|
2022-10-25 16:51:02 -05:00
|
|
|
if [ -z "$HOSTNAME" ] ; then
|
|
|
|
echo "Missing hostname in the config file, setting to default 'sonic'"
|
|
|
|
HOSTNAME='sonic'
|
|
|
|
fi
|
|
|
|
|
2017-11-22 16:36:25 -06:00
|
|
|
echo $HOSTNAME > /etc/hostname
|
|
|
|
hostname -F /etc/hostname
|
|
|
|
|
2019-01-18 00:47:19 -06:00
|
|
|
# Remove the old hostname entry from hosts file.
|
|
|
|
# But, 'localhost' entry is used by multiple applications. Don't remove it altogether.
|
2019-10-29 10:30:27 -05:00
|
|
|
# Edit contents of /etc/hosts and put in /etc/hosts.new
|
2019-01-18 00:47:19 -06:00
|
|
|
if [ $CURRENT_HOSTNAME != "localhost" ] || [ $CURRENT_HOSTNAME == $HOSTNAME ] ; then
|
2019-10-29 10:30:27 -05:00
|
|
|
sed "/\s$CURRENT_HOSTNAME$/d" /etc/hosts > /etc/hosts.new
|
|
|
|
else
|
|
|
|
cp -f /etc/hosts /etc/hosts.new
|
2019-01-18 00:47:19 -06:00
|
|
|
fi
|
|
|
|
|
2019-10-29 10:30:27 -05:00
|
|
|
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts.new
|
2017-11-22 16:36:25 -06:00
|
|
|
|
2019-10-29 10:30:27 -05:00
|
|
|
# Swap file: hosts.new and hosts
|
|
|
|
mv -f /etc/hosts /etc/hosts.old
|
|
|
|
mv -f /etc/hosts.new /etc/hosts
|