K
K
ksvdon2016-08-26 13:41:44
linux
ksvdon, 2016-08-26 13:41:44

How to generate hostname from IP?

I need the machines that I raise on the hypervisor to receive the hostname as well as the IP when they start up (so that the hostname is unique for each VM). How to do it without shamanism, who knows?
ps instead of localhost I want to receive 192.168.1.*...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chupasaurus, 2016-08-26
@chupasaurus

Push a simple script into autoload:

IP=$(ifconfig eth0 | awk '/inet addr/{print substr($2,6)}')
ID=$(echo $IP | sed -e 's/\.//g' | cut -c 7-)
HOSTNAME=$ID.local
echo $HOSTNAME > /etc/hostname && service hostname restart

The output ID is equal to the last two octets (i.e. 192.168.1.52 → 152).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question