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 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
$ 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:




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

## : sync time Vietnam – dq



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

$ sudo cp /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.



A coder, husband and brother...
This entry was posted in Linux. Bookmark the permalink.

7 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 in the /etc/cron.hourly

    ## sync time for VN

    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

  5. Pingback: Kill processes on Win | DucQuoc's Blog

  6. Josephsycle says:

    celeb sex scene – blowjob scenes, gina stiebitz nude

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s