Virtual or physical?
Charly's Column – VM Detection
To write low-level scripts, as an admin, you need to know whether you are currently on a physical or a virtual machine. Charly finds out with a couple of clever hacks.
Of the systems I work on, about 90 percent are virtualized and 10 percent are legacy hardware servers. For many jobs, this makes no difference, but when I write scripts that call or change hardware-related functions, I need this information.
If I have root privileges on the system and am also allowed to retroactively install software, the problem can be solved very quickly. I install either Facter [1] or virt-what
[2]. Facter provides extensive information about the system's hardware, much like lshw
, and is actually overkill for answering the "virtual or not" question. Calling facter virtual
returns the virtualization platform as the answer, such as vmware
or kvm
. The same result is returned by a call to virt-what
. If I don't need the power of Facter elsewhere, I prefer the leaner virt-what
.
If I have root privileges but am not allowed to install software (for example, because of restricted repositories), there is another possibility. The command
dmidecode -t system
gives me the desired information (Figure 1).
But what if I don't have root privileges on the system? There are solutions for this, too, even several of them. The first is the command:
dmesg | grep DMI
On a VMware guest, the output looks like the second line of Listing 1. If I run the same command on a physical server, I usually see some information about the server model at this point (line 4).
Listing 1
Virtual or Physical?
01 $ dmesg | grep DMI 02 [ 0.000000] DMI: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/28/2020 03 [...] 04 [ 0.000000] DMI: HP ProLiant DL320e Gen8, BIOS J05 12/10/2012 05 06 $ cat /proc/scsi/scsi 07 Attached devices: 08 Host: scsi2 Channel: 00 Id: 00 Lun: 00 09 Vendor: QEMU Model: QEMU HARDDISK Rev: 2.5+ 10 Type: Direct-Access ANSI SCSI revision: 05 11 [...] 12 Attached devices: 13 Host: scsi2 Channel: 00 Id: 00 Lun: 00 14 Vendor: ATA Model: SanDisk SSD PLUS Rev: 00RL 15 Type: Direct-Access ANSI SCSI revision: 05
Another possibility is the command:
cat /proc/scsi/scsi
On a virtualized system, I would see output like that shown in lines 7 to 10 of Listing 1. The physical system, on the other hand, again responds with information about the hardware (starting in line 12).
There are quite a few other possibilities, but whenever it is technically possible, I use virt-what
. It doesn't get any faster or easier than that.
Infos
- Facter: https://github.com/puppetlabs/facter
- virt-what: https://people.redhat.com/~rjones/virt-what/
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.