SSH Shows Incorrect Hostname When Connecting Another Ubuntu VM in VirtualBox

Abstract: Resolve SSH incorrect hostname display between Ubuntu VirtualBox VMs by correcting /etc/hostname, updating /etc/hosts, and configuring sshd.

Created: 2026-09-16 by

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, replacing username 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.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More