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

poi*_*rez 4 linux bash google-compute-engine

我想在bash脚本中查看我是在Google Compute Engine实例中还是在我的Linux笔记本电脑中.我该如何区分它们?

Ant*_*xon 7

请参阅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/
Run Code Online (Sandbox Code Playgroud)

您可以使用内部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
Run Code Online (Sandbox Code Playgroud)

要么

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

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