Enter your email address:

Delivered by FeedBurner

Setting the time of your system


StumbleUpon Toolbar

How important is time for Linux

Lots of the task your Linux machine are controlled by the time, things like cron jobs, emails' Date, file's dates, so it is really important to have your computer's clock on time.

Setting the time using Gnome

If you are using Gnome you can set the time this way.

Right click on your clock applet, should be in the upper-right corner of you screen, you will be asked for the password needed to gain root rights, and then you will see this screen.

Gnome time adjust windows screenshot

There you can adjust time, or make Linux to do it automatically, and select the time servers (If you have Internet conection), you can also adjust the Time Zone where you are.

Utilities for the command Line

From the command line we have lots of utilities to set up the clock, set the date of the system, set the date of the hardware or have time automatically updated from Internet time servers.

date
Date displays the date and time of the system, used by root it can also be used to change the date and time of the system.

If you just enter:

date

You will get the date of the system, like this;

Sat Aug 18 19:36:53 BOT 2007

You can set the date by issuing (as root):
Usage:

date nnddhhmm[[cc]yy][.ss]

where

  • nn = month of the year (01 to 12)
  • dd = day of the month (01 to 31)
  • hh = hour of the day (00 to 23)
  • mm = minute of the hour (00 to 59>
  • cc = The first to digits of the year
  • yy = The last two digits of the year
  • .ss = The seconds

hwclock

With this command you are able to set or get the time of you hardware clock, the hardware clock is the clock that runs in you PC hardware even if you disconnect it from the main power supply, this is because it has a lithium battery in the modern computers and another type of battery in the old ones.

Usage

hwclock [function] [options...]

Functions:
--help show this help
--show read hardware clock and print result
--set set the rtc to the time given with --date
--hctosys set the system time from the hardware clock
--systohc set the hardware clock to the current system time
--adjust adjust the rtc to account for systematic drift since
the clock was last set or adjusted
--getepoch print out the kernel's hardware clock epoch value
--setepoch set the kernel's hardware clock epoch value to the
value given with --epoch
--version print out the version of hwclock to stdout

Options:
--utc the hardware clock is kept in coordinated universal time
--localtime the hardware clock is kept in local time
--directisa access the ISA bus directly instead of /dev/rtc
--badyear ignore rtc's year because the bios is broken
--date specifies the time to which to set the hardware clock
--epoch=year specifies the year which is the beginning of the
hardware clock's epoch value
--noadjfile do not access /etc/adjtime. Requires the use of either --utc or --localtime

If you want to set your hardware clock to your local time you can use this command:

hwclock --set --date='08/18/07 21:08:40' --localtime

That will set the clock you August 8th, 2007 at 21:08 and will tell your clock that is the local time, you can use --utc instead of --localtime to set your clock to the Universal Time (Greenwich Time).

If you want to read it issue this command

hwclock --show

You should get an output like this:

Sat 18 Aug 2007 09:19:03 PM BOT -0.677125 seconds

Keeping it on time automatically

If you want to keep your system time accurate and on time automatically, you can install ntp

To do this just run:

On Debian and Ubuntu

apt-get install ntp

On Fedora and CentOS

yum install ntp

Once installed you have to configure it, its configuration file is:

/etc/ntpd.conf

It looks like this by default on Debian systems:

# /etc/ntp.conf, configuration for ntpd

driftfile /var/lib/ntp/ntp.drift
statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to more than 300 low-stratum NTP servers.
# Your server will pick a different set every time it starts up.
#  *** Please consider joining the pool! ***
#  ***  ***
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst

# By default, exchange time with everybody, but don't allow configuration.
# See /usr/share/doc/ntp-doc/html/accopt.html for details.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access,
# but only if cryptographically authenticated
#restrict 192.168.123.0  mask  255.255.255.0 notrust

# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet,
# de-comment the next lines. Please do this only if you trust everybody
# on the network!
#disable auth
#broadcastclient

Commands to update manually from the server

Here we have two commands:

  • rdate
  • ntpdate

rdate

Its usages is:

rdate [-psau] host [port]

Where the options are:

-p
Do not set, just print the remote time
-s
Do not print the time.
-a
Use the adjtimex(2) call to gradually skew the local time to the remote time rather than just hopping.
-u
Use UDP instead of TCP as the transport.

So to update your clock with this command issue this command:

rdate 129.6.15.28

ntpdate

Its usage is:

ntpdate [-bBdoqsuv] [-a key] [-e authdelay] [-k keyfile] [-o version] [-p samples] [-t timeout] server [...]

As ntpdate will decline to set the date if an NTP server daemon (e.g., ntpd) is running on the same host so you should decide if use the daemon way or use ntpdate in the cron.

To check if your ntp daemon is running enter:

/etc/init.d/ntp status

To use ntpdate if you ntp daemon is off enter:

ntp [server]

i.e.

ntpdate 129.6.15.28

To start the daemon enter:

/etc/init.d/ntp start

Related articles

ntpdate man page
date man page
hwclock man page

Trackback URL for this post:

http://www.go2linux.org/trackback/207
StumbleUpon Toolbar

 If you like this article, subscribe to our full rss

If this article was somehow useful for you, you can leave something in the tip's jar

Please post your question in our forum and use comments only to leave your comments about the article, thanks.

Don't forget to "chkconfig

Don't forget to "chkconfig ntpd on" and "service ntpd start" (on the RH variants).

Hi, Thanks for the article.

Hi,

Thanks for the article. On this subject I've been trying to figure out how to have my server act as an ntp server for the lan (rather than pushing a whole bunch of traffic out to the internet).

I've not yet been able to successfully do this - any hints on what I'll need to do? It doesn't seem to be as simple as setting other machines to use a machine with ntpd running as the server.

Cheers,

Alan.

you need to change this

you need to change this line, on the /etc/ntp.conf

# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

uncomment the last line, and use your own subnet IP.

I have done this in the past, but I will do it again, and write about it, subscribe to the RSS to know when that is posted, let me know if that helped you.

Guillermo Garron

Hi Guillermo, I've given

Hi Guillermo,

I've given this a swing and set my ntpd.conf on the client machines to listen on *
I also set my ntp.conf to broadcastclient yes and set the servername to my server

Not sure if it's working or not yet, but there doesn't seem to be any errors. Thanks for the help.

Cheers,

Alan.

Thank to you for posting,

Thank to you for posting, please let us all know how does it goes?

Guillermo Garron

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

This site is proudly hosted at Bluefur Hosting