
Updating NTP settings ESXi hosts
Network Time Protocol (NTP) is used to synchronize the time of a computer with that of an NTP server. It is important to ensure that the time of your ESXi host is accurate, as it can affect the timestamps of logs, system events, and network performance. In this blog, we will learn how to check and change the NTP settings of an ESXi host.
Checking NTP Settings
To check the current NTP settings of an ESXi host, you can use the vSphere Client or the ESXi Shell.
Using the vSphere Client
- Connect to the vSphere Client and log in to your vCenter Server.
- In the Inventory panel, select the ESXi host that you want to check the NTP settings of.
- Click the Configuration tab and then click Time Configuration in the Software panel.
- The NTP configuration will be displayed in the Time Configuration window.
Using the ESXi Shell
- Connect to the ESXi Shell using SSH or the local console.
- Run the following command:
esxcli system ntp get
- The current NTP configuration will be displayed.
Changing NTP Settings
To change the NTP settings of an ESXi host, you can use the vSphere Client, the ESXi Shell, or a PowerShell script. It is recommended to use at least three NTP servers to ensure accurate time synchronization.
Using the vSphere Client
- Connect to the vSphere Client and log in to your vCenter Server.
- In the Inventory panel, select the ESXi host that you want to change the NTP settings of.
- Click the Configuration tab and then click Time Configuration in the Software panel.
- In the Time Configuration window, click Edit.
- Select the NTP Client option and enter the IP address or hostname of the NTP server that you want to use. You can also specify additional NTP servers as backups.
- Click OK to save the changes.
Using the ESXi Shell
- Connect to the ESXi Shell using SSH or the local console.
- Run the following command to set the NTP server:
esxcli system ntp set -s [NTP server]
- Run the following command to enable the NTP service:
esxcli system ntp start
Note: If you are using the ESXi Shell to change the NTP settings, you will need to manually restart the NTP service for the changes to take effect. To do this, run the following command: /etc/init.d/ntpd restart
But what if you have a large environment I hear you say?
Changing NTP Settings using a PowerShell Script
In addition to using the vSphere Client or the ESXi Shell, you can also use a PowerShell script to change the NTP settings of an ESXi host. Here is an example of how you can do this. Enter the vCenter, Cluster and NTP servers and the script will take care of everything.
<# Author: Kabir Ali - info@kablog.nl Scriptname: ESXi_NTP Version: 1.0 (Tested) Date: January 2 2023 Why: Large number of hosts are not in time sync after the NTP server crashed. #> <# Example: .\ESXi_NTP.ps1 -vCenter "vCenter@local.domain" -Cluster "Management" -NTP_servers "192.168.1.100, 192.168.1.101, 192.168.1.103" #> Param ( [Parameter(Mandatory = $true)][string]$vCenter, [Parameter(Mandatory = $true)][string]$Cluster, [Parameter(Mandatory = $true)][string]$NTP_server ) # Bypass SSL certificate verification add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy # Connect source vCenter Write-Host "Trying to connect to the vCenter server..." -ForegroundColor Yellow try { Connect-VIServer -Server $vCenter -ErrorAction Stop } Catch { Write-Warning -Message "Error: Failed to connect to the source vCenter. Stopping script." Break } # Show current NTP server(s) and the date and time of the ESXi host(s). Write-Host "Configured NTP server(s) and local date and time on the ESXi host(s)" -ForegroundColor Yellow Get-cluster -name $cluster | Get-VMHost | sort Name | select Name,@{Name="NTP Server";Expression={($_ | Get-VMHostNtpServer)}},@{Name="Current VMHost Time";Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}} | ft -AutoSize # Ask if configured NTP server(s) should be replaced with new ones. If so set new server(s) and restart NTP deamon. $confirm_ntp_update = Read-Host "Remove configured NTP and replace with new NTP? (Y/N)" if ($confirm_ntp_update -eq "y" -or $confirm_ntp_update -eq "Y") { Write-Host "Removing old NTP settings..." -ForegroundColor Red foreach ($vmhost in (get-cluster -Name $cluster | Get-VMHost)) { $old_ntp = get-vmhost -Name $vmhost | Get-VMHostNtpServer foreach ($old_ntp_entry in $old_ntp.Split(',')) { Get-VMHost -name $vmhost | Remove-VMHostNtpServer -NtpServer $old_ntp_entry -Confirm:$false } } Write-Host "Setting new NTP settings..." -ForegroundColor Green foreach ($ntp_ip in $NTP_server.Split(',') -replace '\s') { Get-cluster -name $cluster | Get-VMHost | Add-VMHostNtpServer $ntp_ip -Confirm:$false | Out-Null } Write-Host "Restarting NTP deamon..." -ForegroundColor Green get-cluster -name $cluster | get-vmhost | Get-VMHostService | where {$_.Key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | Out-Null Write-Host "Display new NTP server and local time ESXi host" -ForegroundColor Green Get-cluster -name $cluster | Get-VMHost | sort Name | select Name,@{Name="NTP Server";Expression={($_ | Get-VMHostNtpServer)}},@{Name="Current VMHost Time";Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}} | ft -AutoSize } else { Write-Host "Stopping script without any changes" -ForegroundColor Yellow } # Disconnect vCenter Write-Host "All changes applied, disconnecting from the vCenter..." -ForegroundColor Yellow Disconnect-VIServer -Server * -Confirm:$false
With this PowerShell script, you can easily change the NTP settings of your ESXi hosts.