# README
kvm-freebsd-hostname
kvm-freebsd-hostname
is a Go-based command-line tool designed to change the hostname of a FreeBSD virtual machine running under KVM (Kernel-based Virtual Machine). This tool operates remotely, using SSH to execute commands on the target VM.
This tool is specifically designed to work with FreeBSD virtual machine images created using the freebsdvirt-image-kit. It assumes the structure and configuration of VMs generated by this toolkit.
Background
This tool was developed to address a specific limitation when working with FreeBSD VMs on a Linux host. The Linux ufs kernel module only supports read-only operations, which prevents direct modification of the /etc/rc.conf
file in FreeBSD VM images. As a result, this tool implements a solution that uses SSH to change the hostname on a running FreeBSD VM, rather than attempting to modify the VM image directly.
Features
- Retrieves VM information from libvirt's dnsmasq status file
- Verifies the current hostname before making changes
- Generates and executes a shell script on the remote FreeBSD VM to change the hostname
- Provides detailed output for debugging purposes
- Uses SSH for remote execution
Prerequisites
- SSH access to the target FreeBSD VM
sudo
privileges on the target FreeBSD VM
Installation
Build the tool:
$ go build
Usage
$ ./kvm-freebsd-hostname [flags] <vm_name>
Important Notes on Flags
- The
-current
flag is mandatory. It is used to verify that the hostname on the FreeBSD VM matches the expected value before making any changes. This verification step ensures that the tool is operating on the correct VM and prevents unintended modifications. The tool will only proceed with the hostname change if the current hostname matches the value specified with the-current
flag. - The
-new
flag is also mandatory and specifies the new hostname to be set.
Example
To change the hostname of a VM named "freebsd" from "freebsd" to "freebsd-14-jail":
$ ./kvm-freebsd-hostname -user freebsd -current freebsd -new freebsd-14-jail freebsd
How It Works
- The tool reads the VM's IP address from the libvirt dnsmasq status file.
- It connects to the VM via SSH and verifies that the current hostname matches the value specified by the
-current
flag. - If the current hostname matches, it generates a shell script to change the hostname.
- The script is copied to the remote VM using SCP.
- The script is executed on the remote VM using SSH.
- The script updates the hostname in
/etc/rc.conf
and sets it using thehostname
command. - The temporary script is removed from the remote VM after execution.
Notes
- This tool requires the FreeBSD VM to be running, as it operates via SSH.
- Ensure that your SSH key is properly set up for passwordless authentication to the target VM.
- The tool requires sudo privileges on the target FreeBSD VM to modify
/etc/rc.conf
and run thehostname
command.
License
This project is licensed under the MIT License - see the LICENSE for details.