如果我在Google Compute Engine实例中,请检查bash

我想在bash脚本中查看我是在Google Compute Engine实例中还是在Linux笔记本中。 我怎样才能区分它们?

请参阅在GCE文档中检测您是否在Compute Engine中运行 :

$ curl metadata.google.internal -i HTTP/1.1 200 OK Metadata-Flavor: Google Content-Type: application/text Date: Thu, 12 Mar 2015 14:24:47 GMT server: Metadata server for VM Content-Length: 22 X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN 0.1/ computeMetadata/ 

您可以使用内部IP(169.254.169.254)代替元数据服务器。

另请参阅检测您是否在Google Compute Engine中运行 :

 sudo dmidecode -s bios-vendor | grep -q Google case $? in (0) echo On a GCE instance;; (*) echo Not a GCE instance;; esac 

要么

 $ dmesg | grep -q "BIOS Google" case $? in (0) echo On a GCE instance;; (*) echo Not a GCE instance;; esac 

在dmesg输出中检查相关字符串为“google”,“virt”或“kvm”也可以提供提示。