简体   繁体   English

从 Linux 虚拟机如何检测/识别 Azure 平台

[英]From Linux Virtual machine how to detect/identify Azure platform

Am looking for a unique way to identify that the Virtual machine (VM) is running on Azure or Hyperv.我正在寻找一种独特的方法来识别虚拟机 (VM) 是在 Azure 还是 Hyperv 上运行。 Something like all the VM's running only on Azure will have this identity.类似于所有仅在 Azure 上运行的 VM 都将具有此标识。 That too I need to know on VM bootup stage itself and not after VM is up and running.我也需要在 VM 启动阶段本身而不是在 VM 启动并运行之后知道。 I want to mainly differentiate between a VM running on Azure and Hyper-v.我想主要区分在 Azure 和 Hyper-v 上运行的 VM。

For example we can use PCI BUS ID 14145353(reading bus/pci/devices file) to detect VM runs on Hyper-v.例如,我们可以使用 PCI BUS ID 14145353(读取 bus/pci/devices 文件)来检测在 Hyper-v 上运行的 VM。 I am looking for something similar for azure.我正在为 azure 寻找类似的东西。

-Thanks -谢谢

The option “unknown-245” is an Azure-proprietary option which only gets issued by an Azure DHCP server. 选项“unknown-245”是Azure专有选项,只能由Azure DHCP服务器发出。 Since in Azure you always will get an address via DHCP (static IPs are also managed by the DHCP and with the REST management API) you will always (and in theory only) get this option as part of the DHCP lease attributes when your machine runs in Azure. 因为在Azure中,您始终将通过DHCP获取地址(静态IP也由DHCP和REST管理API管理),您将始终(并且理论上)仅在机器运行时将此选项作为DHCP租约属性的一部分获取在Azure中。

You can create a bash-script such as the following one to detect if the option unknown-245 is available to have a first indicator of whether you run in Azure or not. 您可以创建一个bash脚本(如下所示)来检测选项unknown-245是否可用于获得是否在Azure中运行的第一个指示符。

Ubuntu Ubuntu的

if `grep -q unknown-245 /var/lib/dhcp/dhclient.eth0.leases`; then
    echo “Running in an Azure VM”
fi

For CentOS based virtual machines the DHCP lease options are indeed stored in the path /var/lib/dhclient/dhclient.leases (or in case of multiple network interfaces dhclient-eth0.leases whereas the part eth0 needs to be replaced with the networking interface device you're going to check against). 对于基于CentOS的虚拟机,DHCP租约选项确实存储在路径/var/lib/dh​​client/dhclient.leases中(或者在多个网络接口dhclient-eth0.leases的情况下,而部分eth0需要替换为网络接口)你要检查的设备)。

Therefore in a default configuration with just one ethernet adapter the script needs to be updated as follows to work inside of a CentOS virtual machine: 因此,在只有一个以太网适配器的默认配置中,脚本需要按如下方式更新才能在CentOS虚拟机内部工作:

# manually start dhclient (seems to be a workaround)
dhclient

# then check against the lease files
if `grep -q unknown-245 /var/lib/dhclient/dhclient.leases`; then
   echo "Running in Azure VM"
fi

Another approach is reading the configuration from the Microsoft Azure Agent which is always installed on a Linux VM and try to reach it's ping-counterpart on the host-agent side. 另一种方法是从Microsoft Azure代理读取配置,该代理始终安装在Linux VM上,并尝试在主机代理端访问它的ping对应项。 If a VM does not run in Azure, trying to reach the host-agent end-point would always result in a timeout. 如果VM未在Azure中运行,则尝试访问主机代理端点将始终导致超时。 Here's a sample script for doing so: 这是一个示例脚本:

curl –connect-timeout 1 `grep FullConfig /var/lib/waagent/GoalState.1.xml | perl -pe ‘s/<.?FullConfig>//g; s/\s//g’` && echo azure || echo no-azure

source and source 来源来源

You can go through /var/log/waagent.log and look for a recent Retrieved GoalState from Windows Azure Fabric or, alternatively, look for the fabric endpoint IPv4 address and check if it's reachable. 您可以浏览/var/log/waagent.logRetrieved GoalState from Windows Azure Fabric查找最近的Retrieved GoalState from Windows Azure Fabric ,或者查找结构端点IPv4地址并检查它是否可访问。 A less friendly but equally effective way is to look at your latest DHCP lease and look for option 245. 一种不太友好但同样有效的方法是查看最新的DHCP租约并查找选项245。

The answer may be late but as it is the case for Cloud Init ( here ) the detection is done by looking at the chassis asset tag by running:答案可能晚了,但正如 Cloud Init( 此处)的情况一样,检测是通过运行以下命令查看机箱资产标签来完成的:

dmidecode --string chassis-asset-tag

Or by looking directly at the file:或者直接查看文件:

/sys/devices/virtual/dmi/id/chassis_asset_tag

If the value returned is equal to 7783-7084-3265-9085-8269-3286-77 it means you are running on the azure platform.如果返回的值等于7783-7084-3265-9085-8269-3286-77则表示您在 azure 平台上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python在Azure中从磁盘创建Linux虚拟机 - Creating a linux virtual machine from disk in azure using python 如何在Azure中连接到在Linux虚拟机上运行的mysql服务器 - how to connect to a mysql server running on a linux virtual machine in azure Azure上的Linux虚拟机 - 如何应用IP限制 - Linux Virtual Machine at Azure - how to apply IP restriction 寻找一种检测我是否在虚拟机(Linux)中运行的好方法 - Looking for a nice way to detect if I am running in a Virtual Machine (Linux) 如何检测脚本是否在虚拟机上运行? - How to detect if the script is running on a virtual machine? 如何将 Jupyter notebook 安装到 Linux 虚拟机中 - How to install Jupyter notebook into a linux virtual machine 为Azure云服务启用HTTPS(运行Linux Server的虚拟机) - Enable HTTPS for Azure cloud service (Virtual Machine running Linux Server) Kurento-WebRTC在Linux虚拟机Azure服务器上不起作用 - Kurento - WebRTC does not work on linux virtual machine azure server 在Windows Server上以Azure虚拟机运行的Linux容器 - Linux Container on Windows Server Running as Azure Virtual Machine 使用适用于Linux的Azure命令行工具创建虚拟机 - Creating a Virtual machine using Azure command line tool for linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM