11

I am looking for a terminal command which doesn't require the executing user to be in the sudoers group and also to be universal and not requiring to install additional packages. So far I have found that if the system has systemd installed then I can use:

$ hostnamectl status
   Static hostname: mint
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
           Boot ID: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  Operating System: Linux Mint LMDE
            Kernel: Linux 3.16.0-6-amd64

and under Icon name and Chassis I can see if it is VM or physical machine. But I was wondering if I can use lscpu, especially since it is more universal method than hostnamectl and it doesn't require systemd. My theory is that if the CPU has only one thread per core and also not listed minimum and maximum CPU frequency this should be an indication that the server is indeed virtualized.

$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    2
Core(s) per socket:    4
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 60
Model name:            Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz
Stepping:              3
CPU MHz:               2500.488
CPU max MHz:           3500.0000
CPU min MHz:           800.0000
BogoMIPS:              4988.18
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              6144K
NUMA node0 CPU(s):     0-7

I know that the if a CPU has only one thread per core doesn't necessarily means that it is VM for sure, but then all modern CPUs should have 2 threads per core and in addition I can also take into account the lack/presence of minimum and maximum CPU frequency in the lscpu output.

  • 4
    "all modern CPUs should have 2 threads per core" - Where do you get that idea? Intel has released 20 processors this year that don't have that. And that's just Intel. – marcelm Jul 26 '18 at 9:50
  • @marcelm, I didn't know that. – Georgе Stoyanov Jul 26 '18 at 10:10
  • 2
    There are also use cases where best performance requires that hyperthreading be turned off in the BIOS. – doneal24 Jul 26 '18 at 14:34
14

Under given conditions:

terminal command which doesn't require the executing user to be in the sudoers group and also to be universal and not requiring to install additional packages.

the obvious simplest method for unmodified VMs, which owners intentionally haven't tried to hide fact the OS is VM, is

cat /sys/class/dmi/id/product_name

More possibilities:

Outside of the given by OP author conditions there are more complicated approaches like this one: Where am I? Operating System and Virtualization Identification Without System Calls

  • 1
    @GeorgеStoyanov You're welcome! – Bob Jul 26 '18 at 7:04
  • Without additional configuration, a libvirt-kvm-VM shows as "Standard PC (i440FX + PIIX, 1996)" when running cat /sys/class/dmi/id/product_name, so I’m not sure how useful that is. – Jonas Schäfer Jul 26 '18 at 13:41
  • 2
    @JonasWielicki That's a well defined system description used by default for any QEMU-based VM though, and is never seen on normal hardware. – Austin Hemmelgarn Jul 26 '18 at 13:57
  • 1
    @JonasWielicki Standard PC (i440FX + PIIX, 1996) is well known fingerprint of QEMU/KVM virtual machines. By the way, this fingerprint can be easily overriden: askubuntu.com/questions/564643/… – Bob Jul 26 '18 at 14:01
  • 2
    @JonasWielicki, pr -t /sys/class/dmi/id/sys_vendor /sys/class/dmi/id/product_name the content of these two files should also give you pretty accurate idea whether the system is physical or virtual. – Georgе Stoyanov Jul 26 '18 at 14:49
12

This also requires systemd (which is pretty ubiquitous these days anyways), but systemd-detect-virt is a better tool to tell whether this is running on physical or virtual hardware.

You can take a look at the logic used by systemd-detect-virt, you'll notice that it actually looks at many places to detect several distinct virtualization technologies...

I think something naïve such as looking at lscpu output might work in some cases some of the time, but I think it would hardly work all the time. Also please note that many technologies make it possible (and even likely) for VMs to have more than one thread per core, so I don't even think that particular feature is sufficient to do any kind of reliable detection here.

  • I am thinking that if the minimum and maximum frequency are missing from the lscpu then this could also be an indication that the machine is indeed VM. But your method seems to be more reliable. – Georgе Stoyanov Jul 26 '18 at 7:03
  • 1
    @GeorgеStoyanov That could also mean that frequency scaling is completely disabled for some other reason though, so it's not reliable. – Austin Hemmelgarn Jul 26 '18 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.