如何让ntpd仅以本地时间为参考,并同步指定Linux服务器的自定义时间?
Got it, let's walk through exactly how to set up this isolated pseudo-time NTP setup—no external time sources, just your custom server acting as the single reference. We'll split this into server and client steps since both need specific configurations.
First, we need to make sure your server ignores all external NTP sources and uses its own local clock as the authoritative time reference.
Edit the NTP config file
/etc/ntp.conf:- Comment out all existing
poolorserverlines that point to external NTP servers (likepool 0.ubuntu.pool.ntp.org iburst). - Add these lines to enable the local clock as the only time source:
server 127.127.1.0 # Local clock driver (pseudo-time reference) fudge 127.127.1.0 stratum 10 # Assign a stratum level so clients trust this source - Add a rule to allow your client device(s) to access this NTP server:
restrict 192.168.x.y mask 255.255.255.255 nomodify notrap # Replace with your client's actual IP # For a whole subnet, use: restrict 192.168.x.0 mask 255.255.255.0 nomodify notrap - Keep the default localhost restrictions (they're needed for the server's internal NTP operations):
restrict 127.0.0.1 restrict ::1
- Comment out all existing
Set and lock your custom pseudo-time:
- First, set the desired time:
date -s "2033-02-23 15:23:10" - Save this time to the hardware clock so it persists after a reboot:
hwclock -w
- First, set the desired time:
Restart the NTP service and enable it on boot:
systemctl restart ntpd systemctl enable ntpd
You already ran ntpdate 1.2.3.4 for a one-time sync—now let's set up automatic, ongoing synchronization to your custom server.
Edit
/etc/ntp.confon the client:- Comment out all external
poolorserverlines. - Add your custom server as the only time source:
server 1.2.3.4 iburst # "iburst" speeds up the initial sync process - Keep the default localhost restrictions.
- Comment out all external
Restart the client's NTP service and enable auto-start:
systemctl restart ntpd systemctl enable ntpdVerify the sync is working:
Runntpq -p—you should see1.2.3.4listed as the only peer, with an asterisk (*) next to it once synchronization is complete.
If your client uses Chrony (common on RHEL 8+, Ubuntu 20.04+):
If you're using Chrony instead of ntpd, adjust these steps:
- Edit
/etc/chrony.conf:- Comment out all
poollines. - Add:
server 1.2.3.4 iburst
- Comment out all
- Restart and enable Chrony:
systemctl restart chronyd systemctl enable chronyd - Verify with
chronyc sources—your server will be marked with a*when synced.
- Firewall Rules: Ensure both server and client allow UDP port 123 (NTP's default port). For firewalld users:
# Server side: firewall-cmd --add-service=ntp --permanent firewall-cmd --reload # Client side (if outbound NTP is blocked): firewall-cmd --add-service=ntp --permanent firewall-cmd --reload - Double-Check Configs: Make sure no external NTP sources are left uncommented in either server or client configs—this ensures your pseudo-time stays isolated.
内容的提问来源于stack exchange,提问作者Victor

