In Linux server management, time synchronization is a seemingly simple yet crucial basic task. Imagine if in your server cluster, Server A records completing an operation at “10:00” while Server B records it at “09:00” – this not only complicates log analysis but also may cause data conflicts in distributed systems and service authentication failures. The Network Time Protocol (NTP) is the core tool to solve these issues, allowing Linux servers to stay synchronized with authoritative time sources over the network.
一、Why NTP Time Synchronization is Needed?¶
In short, NTP ensures Linux servers’ system clocks align with a high-precision, stable time source. Common use cases include:
- Log Management: Uniform timestamps simplify troubleshooting (e.g., a 1-hour discrepancy between logs from Server A and B makes diagnosis much harder).
- Service Authentication: Many services (SSH, databases) rely on timestamps for identity verification; misaligned times can cause connection failures.
- Data Synchronization: Distributed systems (multi-node databases, cloud services) require precise time coordination for data consistency.
二、Basic NTP Principles¶
NTP uses a “hierarchical structure” for synchronization:
- Stratum Levels: Time sources are categorized into 1 to 16 Stratum levels, with lower numbers indicating higher authority. For example, Stratum 1 is the top-tier source (GPS satellites, atomic clocks), Stratum 2 devices synchronize from Stratum 1, and so on.
- Time Servers vs. Clients: Authoritative sources (e.g., GPS stations) act as “root servers,” syncing time to lower-stratum devices (Stratum 2), ultimately reaching ordinary Linux servers (clients).
三、NTP Tools in Linux¶
Linux servers commonly use two NTP services:
- NTPD: A classic, feature-rich implementation with slightly higher resource usage, suitable for traditional servers.
- Chrony: A lightweight alternative with faster startup and lower resource consumption, ideal for memory-constrained servers (default on CentOS 7+ and Ubuntu 16.04+).
This article focuses on NTPD for installation and configuration, with a brief Chrony overview at the end.
四、Installing NTP Service (NTPD Example)¶
Installation commands vary by Linux distribution:
CentOS/RHEL (version 7.9 and earlier)¶
yum install ntp -y # Install NTP service
Ubuntu/Debian¶
apt update && apt install ntp -y # Install NTP service
Note: CentOS 7+ uses Chrony by default. To use NTPD, uninstall Chrony first to avoid conflicts:
bash yum remove chrony -y # Uninstall default Chrony yum install ntp -y # Install NTPD
五、Configuring NTP Service¶
1. Edit NTP Configuration File¶
The core NTP config file is /etc/ntp.conf. Open it for editing:
vi /etc/ntp.conf
2. Key Configuration & Example¶
Replace default redundant servers with authoritative ones (recommended for domestic use):
# Comment default servers (avoid duplication)
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# Add authoritative time servers (faster access in China)
server ntp.aliyun.com iburst # Aliyun NTP server
server time1.aliyun.com iburst
server pool.ntp.org/cn iburst # Backup Chinese pool
# Allow local network access (block external by default)
restrict 127.0.0.1 # Allow loopback access
restrict default nopeer noquery # Deny external modification
# Optional: Allow specific subnets
# restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # Allow 192.168.1.x subnet
Parameter Explanation:
-server: Specify time servers;iburstenables rapid initial synchronization.
-restrict: Controls access;nomodifyprevents client-side config changes,notrapdisables trap messages (security).
3. Start Service & Enable Auto-Start¶
# Start NTPD
systemctl start ntpd
# Enable on boot
systemctl enable ntpd
# Verify status
systemctl status ntpd
六、Manual Sync & Verification¶
1. Check Sync Status¶
ntpq -p # Show NTP server status (-p = list peers)
Sample Output:
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntp.aliyun.com .GPS. 2 u 123 1024 377 12.345 0.012 0.034 # * = preferred source
+time1.aliyun.com .GPS. 2 u 234 1024 377 15.678 0.056 0.021
Key Columns:
- remote: NTP server address.
- st: Server stratum (2 = secondary authoritative, lower = more reliable).
- *: The primary time source for local sync.
- offset: Local time deviation from the server (ideally near 0).
2. Force Immediate Sync¶
To synchronize time immediately (instead of waiting for periodic sync):
ntpdate -u ntp.aliyun.com # -u avoids UDP broadcast, enables quick sync
七、Firewall & Network Settings¶
For servers with firewalls (e.g., firewalld), open NTP’s default port (UDP 123):
CentOS/RHEL/firewalld¶
firewall-cmd --add-port=123/udp --permanent # Permanent rule
firewall-cmd --reload # Apply changes
Ubuntu/ufw¶
ufw allow 123/udp # Allow UDP 123 access
八、Chrony (Lightweight NTP Tool)¶
If your system uses Chrony by default (e.g., CentOS 7+), basic operations are similar:
- Install: yum install chrony -y (pre-installed on CentOS/RHEL).
- Config: /etc/chrony.conf (replace server entries with your preferred time sources).
- Start: systemctl start chronyd && systemctl enable chronyd.
- Verify:
- chronyc sources -v (list sync sources)
- chronyc tracking (check local time offset).
九、Common Issues & Solutions¶
-
Service Startup Failure:
- Port Conflict:netstat -tuln | grep 123(check for other NTP services).
- Config Syntax: Test withntpq -cor debug withntpd -d. -
Slow/Failed Sync:
- Network Issues:ping ntp.aliyun.com; fix DNS/firewall if unreachable.
- Server Selection: Use domestic servers likentp.aliyun.comorpool.ntp.org/cn. -
System vs. Hardware Clock Mismatch:
- Force sync:ntpd -qg(quits after forcing sync).
十、Summary¶
NTP synchronization is fundamental for stable Linux operations. Key steps:
1. Install: Choose NTPD or Chrony based on your distribution.
2. Configure: Edit /etc/ntp.conf or /etc/chrony.conf to specify authoritative servers.
3. Start: Ensure the service runs and enables auto-start on boot.
4. Verify: Use ntpq or chronyc to check sync status.
By following these steps, your Linux servers will maintain accurate, uniform time, avoiding issues caused by time discrepancies.