sonic-buildimage/files/dhcp/sethostname
Oleksandr Ivantsiv 793b842d60 Add DHCP client hook to update hostname. (#235)
* Add DHCP client hook to update hostname.

* Remove interface checking

* Update hostname in /etc/hosts file
2017-02-02 11:19:48 -08:00

21 lines
561 B
Bash

#!/bin/bash
# This script is to update hostname of the system.
if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
then
exit 0
fi
current_host_name=`hostname -s`
if [ "$current_host_name" != "$new_host_name" ]
then
echo $new_host_name > /etc/hostname
line_to_replace=`grep 127.0.0.1.*$current_host_name /etc/hosts`
new_line=`echo $line_to_replace | sed "s/$current_host_name/$new_host_name/"`
sed -i "s/$line_to_replace/$new_line/" /etc/hosts
hostname -F /etc/hostname
fi