Monday, September 29, 2014

Timesync on Linux servers

For CentOS


  • Open the terminal or login over the ssh session. You must login as as the root user. Type the following yum command to install ntp
# yum install ntp ntpdate ntp-doc -y

  • Turn on service, enter:
# chkconfig ntpd on

  • Synchronize the system clock with 0.pool.ntp.org server (use this command only once or as required):
# ntpdate pool.ntp.org

  • Start the NTP server. The following will continuously adjusts system time from upstream NTP server. No need to run ntpdate:
# /etc/init.d/ntpd start

Configure ntpd (optional)

  • Edit /etc/ntp.conf, enter:
# vi /etc/ntp.conf
Set public servers from the pool.ntp.org project:
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org


Setup a cron job to keep the time in sync

  • Create a script for ntpdate
# vim ntpdate.sh
#!/bin/sh

/usr/sbin/ntpdate pool.ntp.org
:wq (save and exit)

  • Create a cron job to sync the time in every 5 minutes
# crontab -e
0,5,10,15,20,25,30,35,40,45,50,55 * * * * sh /root/ntpdate.sh >> /root/ntpdate.log 2>&1  # This will sync the time in every 5 minutes and redirect the logs to root/ntpdate.log

:wq (save and exit)

For Ubuntu

Ubuntu comes with ntpdate as standard, and will run it once at boot time to set up your time according to Ubuntu's NTP server. However, a server's clock is likely to drift considerably between reboots, so it makes sense to correct the time occasionally.

  • Create a script for ntpdate
# vim ntpdate.sh
#!/bin/sh

/usr/sbin/ntpdate ntp.ubuntu.com
:wq (save and exit)

  • Create a cron job to sync the time in every 5 minutes
# crontab -e
0,5,10,15,20,25,30,35,40,45,50,55 * * * * sh /root/ntpdate.sh >> /root/ntpdate.log 2>&1  # This will sync the time in every 5 minutes and redirect the logs to root/ntpdate.log

:wq (save and exit)

No comments:

Post a Comment