SSH displays incorrect hostnames between Ubuntu VirtualBox VMs primarily due to misconfigured /etc/hostname files or stale DNS resolution caches. This issue arises when the system’s network identity does not match the expected label during connection establishment, causing terminal prompts and logs to reflect wrong identifiers.
Verifying the System Hostname Configuration
The most common cause for this discrepancy is a mismatch between the actual hostname set in the operating system and what SSH clients expect. You must first verify the current hostname on both the source and destination Ubuntu VMs. Open a terminal on each machine and run the following command:hostnamectl status
Look for the line labeled "Static hostname" or "Hostname". If these values do not match the names you intend to use for inter-VM communication, they need to be corrected. It is crucial that both VMs have unique hostnames within the same VirtualBox network segment to avoid conflicts.
To change the hostname permanently, use the hostnamectl utility with administrative privileges. For example, to set the hostname to ubuntu-server, execute:
sudo hostnamectl set-hostname ubuntu-server
After running this command, the change takes effect immediately for new sessions. However, existing terminals may still display the old name until they are refreshed or restarted. Verify the change by running hostname again; it should now return the new value.
Updating the Hosts File for Local Resolution
VirtualBox internal networks often rely on local DNS resolution rather than external DNS servers. The/etc/hosts file maps IP addresses to hostnames locally. If this file is missing entries for your VMs, SSH might resolve names incorrectly or fall back to reverse DNS lookups that yield unexpected results.
Open the hosts file on both VMs using a text editor like nano:
sudo nano /etc/hosts
Ensure that each VM lists its own IP address alongside its correct hostname. A typical entry looks like this:
192.168.56.101 ubuntu-server
192.168.56.102 ubuntu-client
Replace the IP addresses and hostnames with those specific to your environment. You can find your current IP address using the ip addr show command. After editing, save the file and exit the editor. This step ensures that when one VM resolves the other’s hostname, it gets the correct IP, preventing confusion in session identification.
For users managing multiple virtual machines, maintaining consistent naming conventions across all instances prevents future headaches. If you encounter permission issues while editing system files, ensure you are using sudo. Related troubleshooting steps for Vagrant environments can be found in our guide on Fixing Permission Denied Error in Vagrant with Ubuntu Jammy64, which covers similar privilege contexts.
Checking SSH Daemon Configuration
The SSH server itself (sshd) has configuration options that influence how the hostname is displayed in login banners and prompt strings. While less common, certain settings in /etc/ssh/sshd_config can override default behaviors. Specifically, check for any custom PrintMotd or Banner directives that might inject incorrect information.
Open the configuration file:
sudo nano /etc/ssh/sshd_config
Search for lines starting with PrintMotd or UseDNS. Setting UseDNS yes can sometimes cause delays or incorrect resolutions if the reverse DNS lookup fails or returns a different name. It is generally recommended to set this to no in local lab environments:
UseDNS no
If you modified the sshd_config file, you must restart the SSH service for changes to take effect:
sudo systemctl restart ssh
This restart ensures that the daemon reloads its configuration and begins using the updated settings for incoming connections. Note that restarting SSH will disconnect any active sessions, so plan accordingly. For more complex connectivity issues where SSH hangs or fails to connect entirely, refer to our article on Troubleshooting SSH Hangs on Kubuntu 24.04 and Ubuntu 24.01 in VirtualBox.
Validating Network Adapter Settings in VirtualBox
Incorrect network adapter configurations in VirtualBox can lead to IP address conflicts, which indirectly affect hostname resolution. Ensure that both VMs are attached to the same network type, such as "Host-only Adapter" or "Internal Network," depending on your isolation requirements. Go to the VirtualBox Manager, select each VM, and navigate to Settings > Network. Check the "Adapter 1" tab. Confirm that the "Attached to" option matches your intended setup. If using NAT, remember that port forwarding rules must be explicitly defined to allow direct SSH access between VMs, which can complicate hostname resolution. For pure internal communication, an Internal Network is preferred because it creates a isolated switch where VMs communicate directly without host interference. Verify that both VMs have static IP assignments within the same subnet to avoid DHCP-related hostname mismatches.Testing the Connection
Once all configurations are verified, test the SSH connection from one VM to the other. Use the following command, replacingusername and hostname with your actual credentials:
ssh username@hostname
Observe the terminal prompt upon successful login. It should display the correct hostname of the remote machine. If the prompt still shows an incorrect name, clear your local SSH cache by removing known hosts entries:
ssh-keygen -R hostname
Then attempt the connection again. This forces SSH to re-establish the connection parameters and pick up the correct hostname from the remote system’s configuration.
Frequently Asked Questions
Why does my SSH prompt show localhost instead of the VM name?
This usually happens when the SSH client falls back to resolving the connection via a loopback interface or an incorrect alias. Ensure that the target VM’s /etc/hostname is correctly set and that the /etc/hosts file maps the IP to the proper name. Restarting the SSH service on the remote machine can also refresh the identity.
Can I fix this issue without restarting the SSH service?
Yes, changing the hostname using hostnamectl applies immediately to new sessions. However, updates to /etc/hosts or sshd_config require a service restart to take full effect. You can verify immediate changes by opening a new terminal window after modifying the hostname file.
What if the hostname is correct but the banner is wrong?
If the terminal prompt is accurate but the welcome message (banner) is incorrect, check the /etc/motd file on the remote VM. This file contains the message of the day displayed upon login. Editing this file allows you to customize or correct the displayed information without affecting the core hostname configuration.
Does VirtualBox version affect SSH hostname resolution?
No, the VirtualBox hypervisor version does not directly dictate how Linux guests resolve hostnames. The issue lies within the guest OS networking stack and SSH configuration. However, newer versions may improve internal network stability, reducing transient IP conflicts that could indirectly cause resolution errors.
How do I prevent this issue in future VM clones?
When cloning VMs, always use the "I copied it" option in VirtualBox to generate new MAC addresses and UUIDs. After cloning, log in and run sudo cloud-init clean followed by sudo hostnamectl set-hostname to assign a unique identity. This prevents duplicate hostname conflicts that confuse SSH clients.