Skip to content

Commit

Permalink
[baseimage]: Avoid removing localhost entry from /etc/hosts file (#2452)
Browse files Browse the repository at this point in the history
- What I did
This fix removes the possibility of 'localhost' entry getting removed from /etc/hosts file by hostname-config service.

Without this change, whenever we change the hostname from 'localhost' to any other name on the config_db.json and reload the config, /etc/hosts file will only have the new hostname on it. But there are multiple sonic utilities (eg: swssconfig) which relies on the hard coded 'localhost' name and they tend to stop working.

- How I did it
Added a new check on hostname-config.sh script to avid blindly deleting the line containing the old hostname from /etc/hosts file. Now it will delete the old hostname only if its not localhost or when the hostname is not changing.

- How to verify it

Bring up SONiC on a device with hostname as localhost
Edit /etc/sonic/config_db.json to update the 'hostname' filed under DEVICE_METADATA from "hostname" : "localhost" --> "hostname" : "sonic"
run config reload -y to reflect the hostname change done on config_db.json file.
cat /etc/hosts and check whether both 127.0.0.1 localhost and 127.0.0.1 sonic entry are present on the file.
ping localhost should work fine.
- Description for the changelog
Make hostname-config service more robust in handling SONiC hostname change from localhost to anything else.
  • Loading branch information
PrabhuSreenivasan authored and lguohan committed Jan 18, 2019
1 parent 20dfb03 commit f28a670
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion files/image_config/hostname/hostname-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']`
echo $HOSTNAME > /etc/hostname
hostname -F /etc/hostname

sed -i "/\s$CURRENT_HOSTNAME$/d" /etc/hosts
# Remove the old hostname entry from hosts file.
# But, 'localhost' entry is used by multiple applications. Don't remove it altogether.
if [ $CURRENT_HOSTNAME != "localhost" ] || [ $CURRENT_HOSTNAME == $HOSTNAME ] ; then
sed -i "/\s$CURRENT_HOSTNAME$/d" /etc/hosts
fi

echo "127.0.0.1 $HOSTNAME" >> /etc/hosts

0 comments on commit f28a670

Please sign in to comment.