怎么判断你的linux系统是不是运行在虚拟机器上面

参考

How to know if the machine you are working on are a VM

问题

很多情况下,我们需要了解到我们的服务器是运行在虚拟机环境还是直接运行在实体机器上,因为我们了解到这些才能更好的实现我们的驱动程序。

当然我们可以有很多中方法来检测我们的远程服务器是不是一台虚拟机节点

dmidecode命令

dmidecode命令用来查看硬件信息。

dmidecode is a tool for dumping a computer DMI (some say SMBIOS) table contents in a human-readable format. So if your machine is a vm, you should not get any output:

在此之前,我们清查机器的硬件信息时,都需要跑到机房才行,而这个机房如果不在我们附近呢?

DMI ,即Desktop Management Interface。也有被称为SMBIOS,即System Management BIOS。
常用参数

较低版本的dmidecode命令不支持参数,因此要看信息的话,要用more/less/grep来配合才能更好些。
较高版本的dmidecode命令有如下参数:

参数 描述
-h 查看帮助信息
-q 不显示未知设备
-t type 查看指定类型的信息,比如bios,system,memory,processor等
-s keyword 查看指定的关键字的信息,比如system-manufacturer, system-product-name, system-version, system-serial-number等
  • 查看服务器型号:dmidecode | grep ‘Product Name’

  • 查看主板的序列号:dmidecode |grep ‘Serial Number’

  • 查看系统序列号:dmidecode -s system-serial-number

  • 查看内存信息:dmidecode -t memory

  • 查看OEM信息:dmidecode -t 11

[somebody@theserver ~]$ sudo dmidecode

  • 虚拟机运行情况–/dev/mem: mmap: Bad address

  • 实体机显示硬件信息

cat /proc/scsi/scsi

如果你使用的是虚拟机,那么你不会获取到任何关于attached device的信息

Try check on /proc/scsi/scsi, if it is a vm, you would not get any attached device:
Attached devices:

  • 虚拟机运行情况

  • 实体机

/sbin/lspci

  • 虚拟机运行情况

  • 实体机

/sbin/ethtool -i eth0

  • 虚拟机运行情况

  • 实体机

dmesg | grep -i vir

  • 虚拟机运行情况

  • 实体机

dmesg | grep -i -e virt

  • 虚拟机运行情况

  • 实体机

你可能感兴趣的:(虚拟机,linux,服务器,硬件)