sonic-buildimage/files/image_config/hostname/hostname-config.sh
Devesh Pathak d45fe19576 Fix to improve hostname handling (#12064)
* Fix to improve hostname handling
If config_db.json is missing hostname entry, hostname-config.sh ends
up deleting existing entry too and hostname changes to default 'localhost'

* default hostname to 'sonic` if missing in config file
2022-10-26 05:48:16 +00:00

28 lines
846 B
Bash
Executable File

#!/bin/bash -e
CURRENT_HOSTNAME=`hostname`
HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']`
if [ -z "$HOSTNAME" ] ; then
echo "Missing hostname in the config file, setting to default 'sonic'"
HOSTNAME='sonic'
fi
echo $HOSTNAME > /etc/hostname
hostname -F /etc/hostname
# Remove the old hostname entry from hosts file.
# But, 'localhost' entry is used by multiple applications. Don't remove it altogether.
# Edit contents of /etc/hosts and put in /etc/hosts.new
if [ $CURRENT_HOSTNAME != "localhost" ] || [ $CURRENT_HOSTNAME == $HOSTNAME ] ; then
sed "/\s$CURRENT_HOSTNAME$/d" /etc/hosts > /etc/hosts.new
else
cp -f /etc/hosts /etc/hosts.new
fi
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts.new
# Swap file: hosts.new and hosts
mv -f /etc/hosts /etc/hosts.old
mv -f /etc/hosts.new /etc/hosts