You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过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) or ntpdate installed (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:

  1. Generate an SSH key pair (press Enter for all prompts to use default settings):
    ssh-keygen -t ed25519
    
  2. Copy the public key to the time slave VM to enable passwordless login:
    ssh-copy-id user@slave-vm-ip
    
  3. 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.

  1. On the master VM, edit the chrony configuration file:
    sudo nano /etc/chrony/chrony.conf
    
  2. Add a line to allow the slave VM to access the master’s time service:
    allow slave-vm-ip
    
  3. Restart the chronyd service to apply changes:
    sudo systemctl restart chronyd
    
  4. Verify the master is ready to serve time:
    chronyc tracking
    
    You should see output confirming the master’s time is stable and synchronized.

Step 3: Sync the slave VM to the master (two options)

This sets up the slave to continuously sync with the master, maintaining millisecond accuracy over time.

  1. On the slave VM, edit its chrony config file:
    sudo nano /etc/chrony/chrony.conf
    
  2. Comment out any existing server lines (e.g., public NTP servers) and add:
    server master-vm-ip iburst
    
    The iburst flag speeds up the initial sync process.
  3. Restart chronyd on the slave to apply changes:
    sudo systemctl restart chronyd
    
  4. 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 %u ensures we sync to UTC, avoiding timezone mismatches between VMs.
  • The %N captures nanoseconds, which the date command will round to the nearest millisecond when setting the slave’s clock.

Step 4: Verify millisecond accuracy

To confirm the clocks are properly aligned:

  1. Run this command simultaneously on both VMs (you can script this over SSH for precise timing):
    date +%s.%N
    
  2. Compare the outputs—any difference should be less than 1ms.
  3. For a more formal check, use chronyc tracking on the slave: look for the System time line, 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

火山引擎 最新活动