如何通过SSH将两台VM配置为毫秒级精度的时间同步?
Absolutely, you can sync two VMs to millisecond-level precision over SSH—here's a practical, step-by-step approach I’ve used repeatedly for distributed systems and latency-sensitive workloads:
Prerequisites
- Both VMs must have passwordless SSH access set up between them (this avoids manual password prompts and streamlines automation)
- Root or sudo privileges on both machines
- A time synchronization tool like
chrony(preferred for millisecond accuracy) orntpdateinstalled (most Linux distros include these by default)
Step 1: Set up passwordless SSH (if not already configured)
On the VM you want to use as the time master:
- Generate an SSH key pair (press Enter for all prompts to use default settings):
ssh-keygen -t ed25519 - Copy the public key to the time slave VM to enable passwordless login:
ssh-copy-id user@slave-vm-ip - Test the connection to confirm no password is required:
ssh user@slave-vm-ip "echo Connected successfully"
Step 2: Configure the master VM as a local time source
We’ll use chrony here because it’s far more reliable for millisecond-level sync than older tools like ntpd, especially over variable network links.
- On the master VM, edit the
chronyconfiguration file:sudo nano /etc/chrony/chrony.conf - Add a line to allow the slave VM to access the master’s time service:
allow slave-vm-ip - Restart the
chronydservice to apply changes:sudo systemctl restart chronyd - Verify the master is ready to serve time:
You should see output confirming the master’s time is stable and synchronized.chronyc tracking
Step 3: Sync the slave VM to the master (two options)
Option 1: Permanent, ongoing sync (recommended)
This sets up the slave to continuously sync with the master, maintaining millisecond accuracy over time.
- On the slave VM, edit its
chronyconfig file:sudo nano /etc/chrony/chrony.conf - Comment out any existing
serverlines (e.g., public NTP servers) and add:
Theserver master-vm-ip iburstiburstflag speeds up the initial sync process. - Restart
chronydon the slave to apply changes:sudo systemctl restart chronyd - Verify the sync status—look for a low offset (under 1ms is ideal):
chronyc sources -v chronyc tracking
Option 2: One-time manual sync
If you only need to align the clocks once (not ongoing sync), run this SSH-based command directly on the slave VM:
sudo date -s "$(ssh user@master-vm 'date -u +"%Y-%m-%d %H:%M:%S.%N"')"
- Using
%uensures we sync to UTC, avoiding timezone mismatches between VMs. - The
%Ncaptures nanoseconds, which thedatecommand will round to the nearest millisecond when setting the slave’s clock.
Step 4: Verify millisecond accuracy
To confirm the clocks are properly aligned:
- Run this command simultaneously on both VMs (you can script this over SSH for precise timing):
date +%s.%N - Compare the outputs—any difference should be less than 1ms.
- For a more formal check, use
chronyc trackingon the slave: look for theSystem timeline, which shows the exact offset from the master’s clock.
Important Notes
- Ensure both VMs use the same timezone (UTC is strongly recommended for server environments) to avoid confusion.
- If working with virtual machines, disable host-level time sync (e.g., VMware Tools, Hyper-V time sync)—this can interfere with manual sync efforts.
内容的提问来源于stack exchange,提问作者Kostas Tsakos




