Following up on my previous post with the same topic, for Hyper-V, this is how to get DNS names for your VirtualBox guest VM’s into your hosts file on a Windows host.
First things first, you need the VirtualBox Guest Additions installed for the host to tell OS level information about the guest, just as with Hyper-V. Here’s a guide on how to install those on Debian (archive here). PowerShell on the host machine will have access to the IP addresses of the client machine. Well, not technically PowerShell in this case, but it can be used to run VBoxManage.exe and process its output.
Without the Guest Additions, VBoxManage.exe will not show any IP addresses for the client machine.
This is the script from the Hyper-V post, modified to do the same for VirtualBox, with the additional support for autostarting virtual machines in headless mode, as I found this lacking in VirtualBox by default. One of my VM’s, Sarge is used for example purposes. Replace this with your own VM names.
$ErrorActionPreference = "Stop" $hosts = "$($env:windir)\System32\drivers\etc\hosts" Start-Transcript -Path "C:\custom\vm_hosts.log" -Append $autostart = @( 'Sarge' ) Write-Host -ForegroundColor Yellow "Autostarting machines" $autostart | ForEach-Object ` { $name = $_ try { & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm "$name" --type headless } catch {} } while ($true) { Write-Host -ForegroundColor Yellow "Scanning for Virtual Machines" $content = Get-Content $hosts [array] $vms = & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' list runningvms $vmListRegex = '^\s*"([^"]+)"' $vms = $vms | Where-Object { $_ -match $vmListRegex } if ($vms) { $vms | ForEach-Object ` { $vm = $_ $name = [regex]::Match($vm, '^\s*"([^"]+)"').Captures.Groups[1].Value Write-Host -ForegroundColor Magenta "VM: $($name)" [array] $ips = & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' guestproperty get "$name" "/VirtualBox/GuestInfo/Net/0/V4/IP" $ip = [regex]::Match(($ips | Select -First 1), ': (\S+)').Captures.Groups[1].Value if ($ip) { Write-Host -ForegroundColor Green "IP: $($ip)" $hostsRegex = '^\s*\S+\s+' + $name.ToLower() + '$' $matches = $content | Select-String -Pattern $hostsRegex | ForEach-Object { $_.Line } | Out-String $wanted = $ip | ForEach-Object { $_ + ' ' + $name.ToLower() } | Out-String if ($matches -ne $wanted) { $content = ($content | Select-String -Pattern $hostsRegex -NotMatch | ForEach-Object { $_.Line } | Out-String).Trim() $content += "`n" + $wanted } } $content | Set-Content "${hosts}.tmp" if ((Get-FileHash "$hosts").Hash -ne (Get-FileHash "${hosts}.tmp").Hash) { Move-Item -Path "${hosts}.tmp" -Destination "$hosts" -Force Write-Host -ForegroundColor Yellow "Hosts file updated!" } } } Write-Host -ForegroundColor DarkGray "Sleeping..." Start-Sleep -Seconds 60 Write-Host }
I run it as a Scheduled Task, same as in the Hyper-V post.
Now I can easily connect to my virtual machines whenever I need to, without knowing their IP address.