Skip to content

Commit 793b842

Browse files
oleksandrivantsivlguohan
authored andcommitted
Add DHCP client hook to update hostname. (#235)
* Add DHCP client hook to update hostname. * Remove interface checking * Update hostname in /etc/hosts file
1 parent 4cd3d31 commit 793b842

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

build_debian.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ iface eth0 inet dhcp
240240
EOF
241241

242242
sudo cp files/dhcp/rfc3442-classless-routes $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d
243+
sudo cp files/dhcp/sethostname $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
243244

244245
if [ -f sonic_debian_extension.sh ]; then
245246
./sonic_debian_extension.sh $FILESYSTEM_ROOT

files/dhcp/sethostname

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
# This script is to update hostname of the system.
3+
4+
if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
5+
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
6+
then
7+
exit 0
8+
fi
9+
10+
current_host_name=`hostname -s`
11+
12+
if [ "$current_host_name" != "$new_host_name" ]
13+
then
14+
echo $new_host_name > /etc/hostname
15+
line_to_replace=`grep 127.0.0.1.*$current_host_name /etc/hosts`
16+
new_line=`echo $line_to_replace | sed "s/$current_host_name/$new_host_name/"`
17+
sed -i "s/$line_to_replace/$new_line/" /etc/hosts
18+
19+
hostname -F /etc/hostname
20+
fi

0 commit comments

Comments
 (0)