CentOS虚拟机静态日期自动变更致应用中断求助
Hey there, let's troubleshoot this annoying date reset issue on your CentOS VM. It's a real headache when your static date requirement gets disrupted unexpectedly—especially when it breaks your application. Let's walk through potential fixes based on what you've already tried and other common culprits:
1. Hunt Down Hidden Time Sync Services
Even if you uninstalled NTP, CentOS (especially 7 and newer) often uses alternative time sync tools like chronyd or systemd-timesyncd by default. These could be quietly overriding your date:
- First, check if these services are running:
systemctl status chronyd systemctl status systemd-timesyncd - If either shows as active, stop, disable, and mask them to prevent them from starting back up:
# For chronyd systemctl stop chronyd systemctl disable chronyd systemctl mask chronyd # For systemd-timesyncd (if applicable) systemctl stop systemd-timesyncd systemctl disable systemd-timesyncd systemctl mask systemd-timesyncd
2. Double-Check VMware Time Sync Settings
You mentioned adjusting VMware sync, but let's make sure we've covered all bases here:
- On your ESXi/vSphere host, navigate to the VM's settings > Options > VMware Tools, and ensure the "Synchronize guest time with host" option is unchecked.
- Inside the VM, edit the VMware Tools config file
/etc/vmware-tools/tools.confto explicitly disable time sync:[timesync] enabled = false - Restart the VMware Tools service to apply changes:
systemctl restart vmtoolsd
3. Check for Unexpected Scheduled Tasks
Your daily cron job resets the date at midnight, but another scheduled task might be running at 3 PM to change it back. Let's audit all cron-related items:
- List cron jobs for every user on the system:
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done - Check system-wide cron directories for scripts that could modify the date:
ls -la /etc/cron.d/ /etc/cron.hourly/ /etc/cron.daily/ /etc/cron.weekly/ /etc/cron.monthly/ - Also, check
anacron—it can run missed tasks when the VM is online, which might trigger a date change:anacron -t /etc/anacrontab -n
4. Lock Down Systemd's Timedatectl
Ensure systemd isn't secretly syncing the time in the background:
- Run
timedatectland verify thatNTP serviceandSystem clock synchronizedboth showno. - If either is
yes, disable NTP sync with:timedatectl set-ntp false
5. Dig Into System Logs to Find the Culprit
Logs can tell us exactly what's changing the date at 3 PM. Let's search for relevant entries:
- For CentOS 7+, use
journalctlto narrow down to the 3 PM window:journalctl --since "14:59:00" --until "15:01:00" | grep -i "date\|time" - For older CentOS versions, check
/var/log/messages:grep -i "date\|time" /var/log/messages | grep -B2 -A2 "Jun 23"
Look for entries that show a process or user executing date -s or timedatectl set-time—this will point you directly to the source.
6. Lock Date-Modifying Commands (As a Last Resort)
To prevent any unauthorized changes, you can make the date and timedatectl commands immutable with chattr. This way, only root can modify them (and you'll need to adjust your cron job to temporarily remove the lock):
- First, apply the immutable flag:
chattr +i /usr/bin/date chattr +i /usr/bin/timedatectl - Update your cron job to remove the lock, set the date, then reapply the lock:
0 0 * * * /bin/chattr -i /usr/bin/date && /bin/date -s "2017-06-23 00:00:00" && /bin/chattr +i /usr/bin/date
内容的提问来源于stack exchange,提问作者Santosh

