Thursday, January 8, 2009

How to change date and time on Linux using hand and NTP

I will write about two methods to change the time in Linux.
1. By hand. This setting may be be accurate as lots of clocks tend to drift over time.
2. Using NTP (Network Time Protocol). With this settings, you can get accurate time from a network time server.

1.By hand.
In order to see the current time and date settings issue,
[root@racnode-2 ~]# date
Thu Aug 14 17:49:04 BDT 2008
So current time is month 8 day 17, year 2008, time 17:49:04.

Now to change only time to 11:51.30 issue,
[root@racnode-2 ~]# date 08141151.30
Thu Aug 14 11:51:30 BDT 2008
Now issue date,
[root@racnode-2 ~]# date
Thu Aug 14 11:51:32 BDT 2008

Note that setting date in this way will not cause the date to remain after a reboot. The date command sets the Linux system clock which is separate from the hardware clock.
We can check linux hardware clock by,
[root@racnode-2 ~]# hwclock
Thu 14 Aug 2008 05:57:01 PM BDT -0.788181 seconds

To make date setting persistent update the hardware clock with the date from the Linux system clock.
[root@racnode-2 ~]# /sbin/hwclock --systohc
If you are using universal time then use,
hwclock --utc --systohc

An alternative way is if you want to set the hardware(BIOS) clock so the system will keep the time when it reboots using,

clock -w
or
setclock
after changing date and time by date command.

To set date and time we can also use,
# date -s "2 OCT 2008 8:00:00"

If you wanted to change the year as well, for example to change year to 2007 of 30th April time 11:16pm you could type
#date 043023162007

To change only the time to 10:10 am you also can use,
#date -s 10:10

2.Using NTP (Network Time Protocol).
This is simple process. Just adding the time server to /etc/ntp.conf and to /etc/ntp/step-tickers Here I use time server as 192.168.1.91.
#vi /etc/ntp.conf
server 192.168.1.91
#vi /etc/ntp/step-tickers
192.168.1.91
Then of course make sure that ntp will start at boot time by,
chkconfig --level 2345 ntpd on
chkconfig --list ntpd


And let's start the service now by,
service ntpd start

It will display output as
ntpd: Synchronizing with time server: [ OK ]
Starting ntpd: [ OK ]

No comments:

Post a Comment