Synchronize time in Linux

Using command line, as usual ;-) .

Well, in a computer with Linux we can check the time and set the time in that using “date”:

$ datedate --set="2 FEB 2012 18:20:30"

But we usually don’t want to set time manually like that, practically – is likely to adjust time manually again and again. Rather we want the time on that computer to be in sync with another computer’s time.

On Windoze there is some built-in support for synchronized time with times.windows.com or something like that . On Linux, we have NTP (Network Time Protocol) for the job. Thus we have to install NTP daemon and get it running, also require to set time once (using “ntpdate” instead of “date” ) .

Below are the commands to do that on CentOS Linux, given user has “sudo” permission. Otherwise you can login as root ( “su root”) and use the below commands without the prefix “sudo” :

$ sudo yum install -y ntp
$ sudo /etc/init.d/ntpd stop ##sudo service ntpd stop
$ sudo ntpdate vn.pool.ntp.org
$ sudo /etc/init.d/ntpd start ##sudo service ntpd start

+ To make it auto sync we can do either:

1/ edit the config file ( /etc/ntp.conf ) and enable the service script: change the servers in config file:

server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org

to

server 0.vn.pool.ntp.org
server 1.vn.pool.ntp.org
server 2.vn.pool.ntp.org

Then enable service to run when computer start:

(update-rc.d in Debian, chkconfig in CentOS/FedoraCore)

$ sudo chkconfig ntpd on

2/ Use cron job with custom script: first wrap the sync command into a shell script

#!/bin/sh
## ntpdate-vn.sh : sync time Vietnam – dq

ntpdate vn.pool.ntp.org

 

Then put it in a cron folder (or edit crontab file) :

$ sudo cp ntpdate-vn.sh /etc/cron.daily/

 

.

Some more (optional) commands:

+ Change hardware clock to the set time of system:

$ sudo hwclock --systohc

+ Check the locale (timezone) :

$ cat /etc/localtime

+ In case the timezone is not correct (for example, VietNam should be ICT ), set timezone:

$ sudo cp -f /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime

(old versions:

$ sudo cp -f /usr/share/zoneinfo/Asia/Saigon /etc/localtime

)

.

That’s it.

./.

About DucQuoc.wordpress.com

A coder, brother, and player at times...
This entry was posted in Linux. Bookmark the permalink.

5 Responses to Synchronize time in Linux

  1. If NTPd does not work, we can use ntpdate with cron jobs as an alternative solution. For example, drop a bash script ntpdate-vn.sh in the /etc/cron.hourly

    #!/bin/sh
    ## sync time for VN
    ntpdate vn.pool.ntp.org

    Also htpdate and rdate are alternatives to ntpdate, in case your ports are blocked by the firewalls.

  2. The two ntpdate commands above send, and expect UDP responses, on a random public port number, not port 123, on your computer so they avoid potential complications from using that privileged port inbound. Of course outbound filters could still be a problem.

  3. Pingback: Linux Directory Structure | DucQuoc's Blog

  4. Pingback: Shrink VirtualBox image | DucQuoc's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s