Starting with vSphere 6.7, users have been able to add a Virtual Trusted Platform Module (vTPM) to a VM, enabling guest operating systems to create and store private keys using a software-based representation of a physical TPM 2.0 chip, that is completely transparent to the underlying OS.
A major benefit of using vTPM is that a physical TPM chip is NOT required in the underlying ESXi host and the vTPM secrets are protected by encrypting the .nvram file, where the secrets are stored.
The encryption keys that are used to encrypt the vTPM is provisioned by a key provider, which can be either be an external Standard Key Provider (SKP) that is KMIP-compliant or using vCenter Server's built-in Native Key Provider (NKP). It is the management of these key providers and their workflows that requires the use of vCenter Server, providing a centralized control plane and a seamless user experience when using the vTPM feature.
Most recently, I saw an influx of inquiries from our field and customers asking about using vTPM with a standalone ESXi host that is NOT managed by vCenter Server, primarily for homelab purposes. While this question has come up in the past, the increased interests might be due to more folks looking to deploy Windows 11, which now has a requirement of a TPM.
While sharing this observation with our lead engineer for VM Encryption, I came to learn that while vCenter Server is highly recommended for a good vTPM user experience, it is technically NOT required for vTPM to function. This sounded very intriguing but surely this solution would NOT be supported right?!
Interestingly, vCenter Server simply uses a set of public vSphere APIs that are available directly on an ESXi host to add or remove encryption keys that is generated from the key provider but the functionality to manage the encryption keys are available on an ESXi host. While this "manual" method is not as seamless as using vCenter Server, you can enable vTPM for a VM using a standalone ESXi host that is not managed by vCenter Server in a completely supported manner!
The lesson here, do not always assume something is NOT supported until you have been told it is NOT supported and always be learning! 😁
🚨 ⚠️ Please carefully ready through the Key Persistency section FIRST before proceeding any further! You have been WARNED ⚠️ 🚨
To demonstrate the Crypto vSphere APIs on an ESXi host, I have created several PowerCLI functions that makes it very easy for anyone to setup vTPM for a standalone ESXi host not managed by vCenter Server. I will not bore you with the implementation details, but if you are really interested, you can look at the source code in the vTPMStandaloneESXiFunctions.ps1 file. For my setup, I am using an Intel NUC 13 Pro, which does NOT have a compliant TPM chip that ESXi recognizes just to prove that you can get this solution working on pretty much any hardware platform that is using ESXi 6.7 or later.
Note: You will need a licensed ESXi host, basically anything EXCEPT for Free ESXi Hypervisor license as that only provides you with read-only access to vSphere API
Step 1 - Download and source the functions vTPMStandaloneESXiFunctions.ps1 file by running the following command:
. ./vTPMStandaloneESXiFunctions.ps1
Step 2 - Use the Connect-VIServer to connect to your standalone ESXi host using:
Connect-VIServer -Server 192.168.30.62 -User root
Step 3 - Run the Prepare-VMHostForEncryption function which will prepare the ESXi host for encryption:
Prepare-VMHostForEncryption
Step 4 - We need to setup the initial ESXi host encryption key by running the New-InitialVMHostKey function and provide a name for the key:
New-InitialVMHostKey -Operation CREATE -KeyName "host-key-1"
Note: This operation can and only needs to be performed once.
Step 5 - Next, we can now generate new VM encryption keys that will then be used to encrypt the vTPM device for a given VM by running the New-VMTPMKey function and provide a name for the key (use something descriptive such as the VM name that you intend to use the key):
New-VMTPMKey -Operation CREATE -KeyName "windows-11-key"
Step 6 - We can also list all host and VM encryption keys by using the Get-VMHostTPMKeys function, which is useful to see what keys are stored on the ESXi host, especially for consuming the VM encryption keys when adding a vTPM device to a VM:
Get-VMHostTPMKeys
Step 7 - Finally, we will use the Reconfigure-VMWithvTPM function to add vTPM device to a VM and encrypt it using a specific VM encryption key that was generated earlier:
Reconfigure-VMWithvTPM -KeyName "windows-11-key" -VMName "Windows-11"
If everything was succesful, you should now be able to power on a VM and consume the vTPM as demonstrated in screenshot below, which I was using a Windows 11 VM. Since the ESXi Embedded Host Client is not aware of the vTPM workflow, you will not see the device listed in the summary view, you will only see that if in the edit view of the VM as shown in the screenshot below.
If you need to remove a VM encryption key, you can use the Remove-VMTPMKey function and provide the name of the key. In the scenario where you need to forcefully remove a VM encryption key, you can use the -Force $true option as shown in the screenshot below.
Remove-VMTPMKey -KeyName "windows-11-key"
Key Persistency
By default, ESXi does NOT store or save any encryption keys across reboots! You will need to re-add all host and any VM encryption keys that have been assigned to a VM or you will not be able to power on the VM. This is one of the major benefits and functions that vCenter Server provides by managing encryption keys that are provisioned by a SKP or NKP to the respective ESXi hosts to ensure they are available.
As a workaround, I have implemented an automatic encryption key backup each time a host or VM encryption key is generated using the PowerCLI functions. The encryption keys are automatically saved to a CSV file named tpm-keys.csv by default or you can choose your own name. Using the encryption key backup file, we can then easily re-import all encryption keys back on an ESXi host.
This is where having a physical TPM would be extremely useful! If you have a physical AND compliant TPM 2.0 chip (requires FIFO and NOT CRB protocol which is typically found in many consumer platforms and is not supported), then you can enable the Key Persistency feature in ESXi using the instructions outlined here, which will then automatically persist all encryption keys that have been added to an ESXi host whether that is manually through the use of the vSphere APIs through vCenter Server and store them on the TPM chip.
If you do NOT have a physical AND compliant TPM 2.0 chip then we simply need to ensure that we have a backup of the generated host and VM encryption keys, which are saved by default in the tpm-keys.csv file. We will go through a simliar enablement workflow of preparing the ESXi host, but rather then generating a new host encryption key, we will simply import our existing host and VM encryption keys that were created earlier.
Here is an example of using the same functions but now using the IMPORT operation which will look for the specific host or VM encryption key name from a given CSV file (default is tpm-keys.csv):
Prepare-VMHostForEncryption
New-InitialVMHostKey -Operation IMPORT -KeyName "host-key-1" -CSVTPMKeyFile tpm-keys.csv
New-VMTPMKey -Operation IMPORT -KeyName "windows-11-key" -CSVTPMKeyFile tpm-keys.csv
Get-VMHostTPMKeys
If the keys were successfully imported, you should now be able to power on any VM that has been assigned with the generated VM encryption keys!
While I have not confirmed myself, but I suspect this solution could also benefit standalone ESXi-Arm as today, you would need to setup an x86 vCenter Server using SKP or NKP.
Steve says
Does this work with a standalone ESXi server running a "free" license? I'm guessing based on the API requirements the answer is no, but figure I'd ask anyway. Thank you!
William Lam says
Any license EXCEPT for Free ESXi will work, as you need write access using vSphere API
vbalogh says
Hi William
in a recent project of mine I was in the process of compiling a custom kernel signed by a custom certificate and enabling SecureBoot and disk encryption via TPM. The kernel had to be signed with the DB key sitting in the TPM. (PK key -> KEK key -> DB key) While there are established methods to replace these keys in the TPM on real hardware, I could not make it on vTPM. Do you know if it is possible? I was working with the vCenter-based native key provider. Here are a bit more details - alas nobody has answered so far:
https://communities.vmware.com/t5/Virtual-Machine-Guest-OS-and-VM/VM-SecureBoot-with-vTPM-and-custom-signed-kernel/td-p/2982557
William Lam says
Yes, this should be possible by leveraging the VM Advanced Option: uefi.secureBoot.PKDefault.file0 where you'd specify the local file path to file. This is simliar to https://communities.vmware.com/t5/ESXi-Discussions/How-to-replace-default-certificate-for-Secure-Boot-Virtual/m-p/1753164/highlight/true#M166843 and I've also reached out to Darius (one of our SME) to see if he has any other commentary to on the VMTN thread
Clark Mercer says
Interesting! I have an important question. I'm familiar with the process and details when using the NKP with vCenter 7. In that scenario, vCenter is adamant about backing up the key. It even forces you to download the key in the form of a P12 file (optionally password protected) before it will let you use it. With your implementation on ESXi without vCenter how do you backup any keys needed? Perhaps just by downloading the tpm-keys.csv file?
William Lam says
Please read the blog posts, as this is already answered in detailed
Matt Mancini says
RE: The lesson here, do not always assume something is NOT supported until you have been told it is NOT supported and always be learning! << Love it!
Greivin Venegas says
These are great news. Fantastic, thank you.
Greivin Venegas says
I know this is out of scope, but it is worth asking.
Currently using Deskmeet Intel version, BIOS in latest ver, TPM enabled, forced to 2.0. Somehow ESXi does not see it.
When booting, I see this error flashing : Failed to determine TPM asset tag size: Buffer too small.
Here is the output when running: “esxcli system settings encryption set --mode=TPM”
Unable to change the encryption mode and policy. Verify that the current host configuration can satisfy the new requirement.
Do you happen to know something about it?
Maybe the TPM is my setup is not supported.
Gladly, there’s a workaround.
William Lam says
More than likely, the TPM is NOT actually a supported TPM, there are fTPM and dTPM, the latter is supported with ESXi. Please see https://williamlam.com/2022/10/quick-tip-tpm-2-0-connection-cannot-be-established-after-upgrading-to-esxi-8-0.html for more details
If you do have a supported dTPM, please review the TPM requirements https://docs.vmware.com/en/VMware-vSphere/8.0/vsphere-security/GUID-10F7022C-DBE1-47A2-BD86-3840C6955057.html#what-are-the-vsphere-requirements-to-use-a-tpm-1
David Yerger says
This is the best idea since ghettoVCB! Well written also, nicely done!
Shahram Hashemi says
Hi, for me the first command even doenst work on ESXI HOST 8.0. ?!
Brian says
I think I was a bit too hasty with this. Everything worked as expected but I didnt get as far as the persistence across reboots before I rebooted my host. After the reboot I cannot do anything with the VM such as power on or reconfigure. Is there any way to remove the tpm/encryption or will I need to just recreate the VM?
Markus says
Thanks for the great article! Everything worked as describes apart from the fact I cannot export the TPM keys into the CSV with "New-VMTPMKey"
PS C:\Users\xxx\Downloads> New-VMTPMKey -Operation CREATE -KeyName "test123-key"
Generating random 256 bit VM key ...
Adding VM key test123-key ...
New-VMTPMKey : Unable to find tpm-keys.csv ...
At line:1 char:1
+ New-VMTPMKey -Operation CREATE -KeyName "test123-key"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-VMTPMKey
Successfully added VM encryption key test123-key ...
I can use the key and reconfigure the VM, but like my preposter pointed out, the VM is locked once I reboot as the key vanishes on reboot without prior backup.
Can't I just remove the vTPM assignment from the VM and get it booting again? I did not encrypt the VM itself just used Bitlocker on the VM (which I could get past via the Recovery Key anyway). I am not using VSphere Client so I cannot use the "Unlock VM" feature there. Thanks!
Markus says
Answering my own question here:
Unregistering the VM, manually editing the VMX file and removing the lines starting with: vtpm.ekCSR, vtpm.ekCRT, vtpm.present, encryption.keysafe and encryption.data and then reregistering the VM made it bootable again - once I entered the Bitlocker Recovery Key.
Markus says
The error on export happened because I didn't create an initial host key using your script but had one from a previous vSphere Client setup, so your Cmdlet failed saying a key already exists. Would that be a problem, because the vTPM keys are derived from that key? Can I export existing keys in retrospect or only at the moment when they are created?
Creating the CSV manually made the New-vTPMKey working again, but is the vTPM key enough or do I need the hostkey as well (this one survived the ESXi reboot).
Sören says
Thank you very much, you helped me a lot! 🙂
Ben says
William, does this guide assume ESXi is not running a free license? I can get all the way to assigning a license key and then get "Current license or ESXi version prohibits execution of the requested operation."
Nigel says
I have been connecting using powercli and a root account and this works great. The team I have working in this environment would like to make their own TPM keys to assign to their VMs. They're savvy enough that I am fine with them using the commands to do this, but they don't have root access. I don't suppose you know what permissions/role they need in ESXi to be able to do this?