Setting Hostname as IP on Linux for Hadoop VM

I added

hostname `hostname -I`

to '/etc/rc.local` and that set the hostname correctly to the currently-assigned IP address.


Using a fixed IP would be the simplest solution. If you can do that, just change /etc/hostname on the guest so that it looks like this (using the right IP of course):

192.168.1.10

If you can't/don't want to use that, you can set up a cronjob that reads the system's IP and updates /etc/hostname accordingly. This command will give you your IP:

ifconfig | grep Bcast | awk '{print $2}' | sed 's/addr://'

Now add a new crontab for root:

sudo crontab -e

This will bring up whicever editor you have defined. Add this line to the crontab file:

@reboot ifconfig | grep Bcast | awk '{print $2}' | sed 's/addr://' > /etc/hostname

This will run the command once each time the machine boots and save its output (the IP) to /etc/hostname. A possible problem is that the cron daemon is started before an IP is assigned. In that case, you could set it to be run every five minutes or so:

*/5 * * * * ifconfig | grep Bcast | awk '{print $2}' | sed 's/addr://' > /etc/hostname