欢迎来到爱学习爱分享,在这里,你会找到许多有趣的技术 : )

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

Linux 611℃

之前我一直是用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

转载请注明:爱学习爱分享 » 使用 systemd-detect-virt 判断 Linux 是否运行在虚拟机中

喜欢 (0)or分享 (0)