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.
It is the responsability of the sysadmin to keep their Linux servers with clock running as accurately as possible.
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 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):
- 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
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...]
- –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
- –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:
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 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
Commands to update manually from the server
Here we have two commands:
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:
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