如何判断当前程序是否运行在虚拟机中?


=Start=

缘由:

突然想到的一个点,觉得有必要收集整理一下,方便以后使用。

正文:

参考解答:

这里只简单谈一谈应用层面的判断,太底层的我也不会、也不了解,就不说了,如果有懂的还请多指点一下。

# Windows

systeminfo | findstr /i model
wmic baseboard get manufacturer,product
Win+R => msinfo32 => 'System Manufacturer'

# Mac 

ioreg -l | grep -e 'Manufacturer'

# Linux

核心思路基本上就是通过在 dmesg输出或是/proc/cpuinfo 等文件中查找关键字来进行判断(不同版本可能不一样,所以可以借助一些工具或脚本来加速查找,比如 virt-what)。

dmidecode -t system | egrep -i 'manufacturer|product'
dmidecode -s system-product-name

sudo lshw -class system

dmesg | egrep -i 'hypervisor|vmware'

systemd-detect-virt

grep -q '^flags.*hypervisor' /proc/cpuinfo

大致内容如上,想到了再补充。

参考链接:

=END=


《“如何判断当前程序是否运行在虚拟机中?”》 有 4 条评论

  1. 如何根据机器的mac地址来判别该机器是否为vmware的虚拟机
    How to recognise a VMware’s virtual machine by its MAC address?
    https://macaddress.io/faq/how-to-recognise-a-vmwares-virtual-machine-by-its-mac-address
    `
    # Range or Prefix Vendor Virtual Machine
    1 00:50:56 VMWare VMware vSphere, VMware Workstation, VMware ESX Server
    2 00:50:56:80:00:00 → 00:50:56:BF:FF:FF VMWare VMware vSphere managed by vCenter Server
    3 00:0C:29 VMWare Standalone VMware vSphere, VMware Workstation, VMware Horizon
    4 00:05:69 VMWare VMware ESX, VMware GSX Server
    5 00:1C:14 VMWare VMWare
    `

  2. How to recognise an Oracle Virtual machine by its MAC address?
    https://macaddress.io/faq/how-to-recognise-an-oracle-virtual-machine-by-its-mac-address
    `
    # Range or Prefix Vendor Virtual Machine
    1 08:00:27 Pcs Systemtechnik GmbH Oracle VirtualBox 5.2
    2 52:54:00 (Exact MAC: 52:54:00:C9:C7:04) N/A Oracle VirtualBox 5.2 + Vagrant
    3 00:21:F6 Oracle Corp Oracle VirtualBox 3.3
    4 00:14:4F Oracle Corp Oracle VM Server for SPARC
    5 00:0F:4B Oracle Corp Oracle Virtual Iron 4
    `

  3. 7 ways to find your MAC address in Windows
    https://www.digitalcitizen.life/4-ways-learn-mac-address-windows-7-windows-81/
    `
    # Windows 下获取mac地址的命令
    getmac

    wmic nic where PhysicalAdapter=True get MACAddress,Name

    ipconfig /all

    ==
    1. Find your MAC address using the getmac command in Terminal, PowerShell, or Command Prompt
    2. How to find your MAC address with the “get-netadapter” command in PowerShell
    3. Get your MAC address by running the ipconfig command in Windows Terminal, PowerShell, or Command Prompt
    4. How to find your MAC address using the “wmic nic get” command in Windows Terminal, PowerShell, and Command Prompt
    5. How to get your MAC address in Windows using the Settings app
    __1. Identify your MAC address using Settings in Windows 11
    __2. Identify your MAC address using Settings in Windows 10
    6. Find the MAC address of any of your network adapters using System Information
    7. How to find the MAC address of the active network adapter from the Control Panel
    `

    Get mac address from command line (CMD)
    https://www.windows-commandline.com/get-mac-address-command-line/

    Windows: Command to Find MAC Address
    https://www.technipages.com/windows-find-mac-address

发表评论

您的电子邮箱地址不会被公开。