Skip to content

Latest commit

 

History

History
64 lines (51 loc) · 1.5 KB

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

File metadata and controls

64 lines (51 loc) · 1.5 KB

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

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

在物理机上:

sudo dmidecode -s system-product-name

在虚拟机上:

sudo dmidecode -s system-product-name

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

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

在物理机上:

systemd-detect-virt;exit 0

在虚拟机上:

systemd-detect-virt;exit 0

在容器上:

systemd-detect-virt;exit 0