暗无天日

=============>DarkSun的个人博客

使用systemd-detect-virt判断Linux是否运行在虚拟机中

之前我一直是用 dmidecode 来判断Linux是否运行在虚拟环境:

在物理机上:

sudo dmidecode -s system-product-name
2353ABU

在虚拟机上:

sudo dmidecode -s system-product-name
VirtualBox

但是它有个缺陷,就是不支持容器的检查。在容器中运行 dmidecode 会提示错误信息 /dev/mem: No such file or directory 若你把 /dev 挂载进容器,则又会被识别为物理急。

不过今天我发现,原来 systemd 已经提供了一个命令来帮你完成这项任务了,那就是 systemd-detect-virt.

在物理机上:

systemd-detect-virt;exit 0
none

在虚拟机上:

systemd-detect-virt;exit 0
oracle

在容器上:

systemd-detect-virt;exit 0
docker