ThisCalculator

Everyday

Linux VPS Time Sync Setup Guide

Updated · Standard formula, shown below

Linux VPS Time Sync Setup Guide

This guide walks you through setting up accurate time synchronization on a Linux VPS. You’ll learn how to check the current time settings, install and configure NTP (Network Time Protocol) or systemd-timesyncd, and verify that your server’s clock stays in sync with global time servers. Accurate time is critical for log accuracy, cron jobs, SSL certificate validation, and database replication.

What You’ll Need

  • Root or sudo access to your Linux VPS.
  • An SSH client to connect to your server.
  • Basic familiarity with the command line.

Step-by-Step

  1. Check current time and timezone. Run timedatectl to see the current system time, timezone, and whether NTP is active. The output will show something like System clock synchronized: yes/no and NTP service: active/inactive.
  2. Set the correct timezone (if needed). Use timedatectl list-timezones to see available zones, then set yours with sudo timedatectl set-timezone Your/Zone (e.g., sudo timedatectl set-timezone America/New_York).
  3. Choose a synchronization method. Most modern Linux distributions use either systemd-timesyncd (lightweight, built into systemd) or the full ntp daemon (more feature-rich, better for servers that need high accuracy). For most VPS use cases, systemd-timesyncd is sufficient.
  4. Enable and start systemd-timesyncd (if using it). Run sudo timedatectl set-ntp true to enable automatic time sync. This will start the systemd-timesyncd service if it’s installed. Check its status with systemctl status systemd-timesyncd.
  5. Install NTP (alternative method). If you prefer the full NTP daemon, install it with your package manager: sudo apt install ntp (Debian/Ubuntu) or sudo yum install ntp (RHEL/CentOS). Then start and enable it: sudo systemctl start ntp and sudo systemctl enable ntp.
  6. Configure NTP servers (optional). The default configuration usually points to pool.ntp.org servers, which is fine. To customize, edit /etc/ntp.conf (for NTP) or /etc/systemd/timesyncd.conf (for timesyncd) and add or change server lines. For example, in timesyncd.conf, under [Time], set NTP=time.google.com pool.ntp.org.
  7. Restart the service. After any configuration change, restart the service: sudo systemctl restart systemd-timesyncd or sudo systemctl restart ntp.
  8. Verify synchronization. Run timedatectl again. You should see System clock synchronized: yes and NTP service: active. For more detail, use timedatectl timesync-status (for timesyncd) or ntpq -p (for NTP) to see the list of servers and offset.
  9. Check for hardware clock issues (if using a VPS). Some VPS providers use a virtual hardware clock that can drift. If you notice persistent drift, consider adding a cron job to run sudo hwclock --systohc periodically, but this is rarely needed on modern systems.

Common Mistakes

  • Forgetting to set the timezone. If the timezone is wrong, the system time will be correct in UTC but displayed incorrectly locally. Always set the timezone first.
  • Using both timesyncd and NTP simultaneously. They conflict. If you install NTP, disable timesyncd with sudo timedatectl set-ntp false before starting NTP.
  • Firewall blocking UDP port 123. NTP uses UDP port 123. If your VPS firewall (like ufw or firewalld) blocks it, sync will fail. Allow it with sudo ufw allow 123/udp or equivalent.
  • Assuming the VPS provider’s time is correct. The hypervisor may have a skewed clock. Always rely on external NTP servers.

Tips

  • Use multiple NTP servers. Configure at least three servers from different pools to ensure redundancy and better accuracy.
  • Monitor sync status. Add a cron job to check timedatectl output and alert you if sync fails (e.g., if it shows System clock synchronized: no).
  • Consider using chrony. On RHEL/CentOS 8+, chrony is the default NTP implementation and is often better for VPS environments because it handles intermittent network connectivity well. Install with sudo yum install chrony and start with sudo systemctl start chronyd.
  • For high-accuracy needs, use a dedicated time server. If your application requires sub-millisecond accuracy, consider using a public NTP server with a known low latency, or a GPS-synced time server if you have physical hardware.

Troubleshooting

  • Time is not syncing. Check if the service is running: systemctl status systemd-timesyncd or systemctl status ntp. If it’s inactive, start it. Also check your firewall and that UDP 123 is open.
  • Sync works but time is off by seconds. This is normal for NTP; it adjusts gradually. Wait a few minutes and check again. If it’s off by minutes, your timezone may be wrong, or the hardware clock is way off. Try sudo ntpdate -u pool.ntp.org (if installed) to force an immediate sync.
  • Error: “Failed to set ntp: NTP not supported” This usually means systemd-timesyncd is not installed. Install it with sudo apt install systemd-timesyncd or use the NTP daemon instead.
  • After installing NTP, timedatectl shows NTP inactive. You need to disable timesyncd: sudo timedatectl set-ntp false, then start NTP with sudo systemctl start ntp.
  • ntpq -p shows no servers. Check your /etc/ntp.conf for correct server lines. If you’re using a restricted network, you may need to specify a local NTP server provided by your VPS host.

Frequently Asked Questions

Why does my VPS time drift even after setting up NTP?

Virtual machines are prone to drift because the virtual hardware clock is not as stable as physical hardware. NTP corrects this gradually, but if you see persistent drift, check that the NTP service is running and that your firewall allows UDP 123. Also, ensure you’re not running both timesyncd and NTP simultaneously.

Should I use systemd-timesyncd or the full NTP daemon?

For most VPS workloads, systemd-timesyncd is sufficient. It’s lightweight, keeps time synchronized, and requires no configuration. The full NTP daemon (or chrony) is better if you need high precision, or if you want to act as a time server for other machines.

How often does NTP sync the time?

NTP doesn’t sync on a fixed schedule. It continuously monitors the offset and adjusts the clock gradually. The polling interval typically ranges from 64 seconds to 1024 seconds, depending on the stability of the connection and the server’s configuration.

Can I use a public NTP server like time.google.com?

Yes. Public NTP servers are free and reliable. You can add them to your configuration. For example, in /etc/systemd/timesyncd.conf, set NTP=time.google.com. Make sure to use multiple servers for redundancy.

What is the difference between UTC and local time?

UTC (Coordinated Universal Time) is the primary time standard used across the internet. Local time is UTC plus your timezone offset. On a server, it’s best to keep the system clock in UTC and only set the timezone for display purposes. This avoids confusion when logs are compared across regions.

Do I need to configure NTP on a managed VPS?

Most managed VPS providers already have time sync configured. However, it’s good practice to verify. If you have root access, you can check with timedatectl and adjust if needed. If you don’t have root, contact your provider’s support.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.