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.
It is the responsability of the sysadmin to keep their Linux servers with clock running as accurately as possible.
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 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:
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):
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.
hwclock [function] [options...]
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:
You should get an output like this:
Sat 18 Aug 2007 09:19:03 PM BOT -0.677125 seconds
If you want to keep your system time accurate and on time automatically, you can install ntp
To do this just run on Ubuntu or Debian:
sudo apt-get install ntp
On CentOS run:
yum install ntp
Once installed you have to configure it, its configuration file is:
It looks like this by default on Debian or Ubuntu 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
Here we have two commands:
Its usages is:
rdate [-psau] host [port]
Where the options are:
So to update your clock with this command issue this command:
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:
To use ntpdate if you ntp daemon is off enter:
To start the daemon enter
sudo service ntp start
If you enjoyed the article, please share it