Set Hostname

The hostname of a virtual machine can be changed on startup. The name of the virtual domain is passed in a smbios string and can be used to rename.

Requirements

This feature is available from release 0.3.4 and requires libvirt-4.6.

Packages

  • Ravada: 0.3.4
  • libvirt: 4.6

Distributions

This feature has been reported to work with these Linux distributions. Any other distribution with libvirt 4.6 or bigger will work too. Please report if you successfully tested it.

Supported distributions:

  • Ubuntu 18.10

Linux

The virtual machine name can be read with dmidecode

dmidecode | grep hostname | awk -F: '{ print $3}'

To set the hostname you must create a script that runs on startup, this one line should be enough for most cases:

hostname `dmidecode | grep hostname | awk -F: '{ print $3}'`

Some tools may read the hostname from the config file, set it like this:

dmidecode | grep hostname | awk -F: '{ print $3}' | sed -e 's/^ //' > /etc/hostname

systemd

If your system supports systemd this script will set the virtual machine name as the hostname on startup. Put the service file in /lib/systemd/system/sethostname.service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[Install]
WantedBy=multi-user.target

[Unit]
Description=Set Hostname 
After=systemd-hostnamed.target syslog.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/set_hostname.sh

This is the script that is launched by the service, it should be in /usr/local/bin/set_hostname.sh as specified in the previous file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/sh
date >> /var/log/set_hostname.log
hostname=`/usr/sbin/dmidecode | grep hostname | awk '{ print $4}'`
if [ ! -z "$hostname" ]; then
	/bin/hostname $hostname
	/bin/hostname > /etc/hostname
	echo "Found hostname $hostname in dmidecode " >> /var/log/set_hostname.log
else
	echo "Not found hostname in dmidecode " >> /var/log/set_hostname.log
	/usr/sbin/dmidecode >> /var/log/set_hostname.log
fi

Type this so the script is executed on startup:

sudo systemctl enable sethostname

Reboot and check if the hostname is applied. You should find a log file at /var/log/set_hostname.log.

rc.local

If you Linux system supports rc.local just add this lines to it and the hostname will be updated on boot:

hostname `dmidecode | grep hostname | awk -F: '{ print $3}'`
hostname > /etc/hostname

Windows

SMBios information is available in Windows too. The data is stored in the registry and also can be shown with a tool called WMI.

Contributed information would be appreciated.