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
- Check current time and timezone. Run
timedatectlto see the current system time, timezone, and whether NTP is active. The output will show something likeSystem clock synchronized: yes/noandNTP service: active/inactive. - Set the correct timezone (if needed). Use
timedatectl list-timezonesto see available zones, then set yours withsudo timedatectl set-timezone Your/Zone(e.g.,sudo timedatectl set-timezone America/New_York). - Choose a synchronization method. Most modern Linux distributions use either
systemd-timesyncd(lightweight, built into systemd) or the fullntpdaemon (more feature-rich, better for servers that need high accuracy). For most VPS use cases,systemd-timesyncdis sufficient. - Enable and start systemd-timesyncd (if using it). Run
sudo timedatectl set-ntp trueto enable automatic time sync. This will start thesystemd-timesyncdservice if it’s installed. Check its status withsystemctl status systemd-timesyncd. - Install NTP (alternative method). If you prefer the full NTP daemon, install it with your package manager:
sudo apt install ntp(Debian/Ubuntu) orsudo yum install ntp(RHEL/CentOS). Then start and enable it:sudo systemctl start ntpandsudo systemctl enable ntp. - 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], setNTP=time.google.com pool.ntp.org. - Restart the service. After any configuration change, restart the service:
sudo systemctl restart systemd-timesyncdorsudo systemctl restart ntp. - Verify synchronization. Run
timedatectlagain. You should seeSystem clock synchronized: yesandNTP service: active. For more detail, usetimedatectl timesync-status(for timesyncd) orntpq -p(for NTP) to see the list of servers and offset. - 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 --systohcperiodically, 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 falsebefore 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/udpor 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
timedatectloutput and alert you if sync fails (e.g., if it showsSystem clock synchronized: no). - Consider using chrony. On RHEL/CentOS 8+,
chronyis the default NTP implementation and is often better for VPS environments because it handles intermittent network connectivity well. Install withsudo yum install chronyand start withsudo 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-timesyncdorsystemctl 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-timesyncdor 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 withsudo systemctl start ntp. - ntpq -p shows no servers. Check your
/etc/ntp.conffor 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.