Being a systems administrator, at some point in your life you’re more than likely going to need to update the date and time on your linux box. If you only have command line access to a linux server you will need to do this via the command line, thankfully it’s a real easy task.
The date command
First things first, the command you will need is date. Date not only outputs the current date and time of the box but also allows you to set them too.
Mon 20 Nov 2010 11:10:38 GMT
Setting the date
To set a new date and time on the command line you can use the -s parameter.
date -s “YYYY-MM-DD H:I:S”
YYYY: Year represent with four digits
MM: Month represented with two digits (01 – 12)
DD: Day represented with two digits (01 – 31)
H: Hour of the day represented with two digis (00 – 23)
I: Minutes represented with two digits (00 – 59)
S: Seconds represented with two digits (00 – 59)
If the current date is 20th of November 2010 at 11:20 the following would set the date as required:
date -s “2010-11-20 11:20:00″
Setting the date with string literals
You can also set the date using string literals instead of digits for the month. For example:
date -s “20 Nov 2010 11:20:00″
Keeping your date/time synced with ntpdate
If your date/time is regularly becoming unsynced it’s a good idea to setup a cron to automatically sync with specific servers. In the following example I explain how to setup automatic date/time syncing with ntpdate on Ubuntu. Other distros may require entering the full path to ntpdate which can vary from distro to distro.
First, install ntpdate if it’s not already installed:
sudo apt-get install ntpdate
Once installed, test it works by syncing with the ntp pool server(s):
sudo ntpdate pool.ntp.org
20 Nov 11:29:45 ntpdate: adjust time server 126.96.36.199 offset 0.000142 sec
You can then setup a cron to update your date/time every 12 hours. To do this add the following line to your root cron
0 0,12 * * * ntpdate pool.ntp.org
You can do this by running crontab edit as root
sudo crontab -e
Importance of keeping your date/time synced correctly
In the server world it’s extremely important that you keep a servers time in sync with the current time. Issues arise when your date/time is out of sync, for example having an auction site and the date/time out of sync can result in incorrect displaying of remaining times on said auction.
Or, as another example, if there is a a website on the server pulling feeds from, for example, Twitter and timestamps do not match there is a good chance the API server will drop your requests for data.