From 85e3a81f47bfc67d0be7d0c5218194682f856420 Mon Sep 17 00:00:00 2001 From: Devesh Pathak <54966909+devpatha@users.noreply.github.com> Date: Tue, 25 Oct 2022 14:51:02 -0700 Subject: [PATCH] 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 --- files/image_config/hostname/hostname-config.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/files/image_config/hostname/hostname-config.sh b/files/image_config/hostname/hostname-config.sh index e9f7fc1227..c2a4f1e546 100755 --- a/files/image_config/hostname/hostname-config.sh +++ b/files/image_config/hostname/hostname-config.sh @@ -3,6 +3,11 @@ 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