Setting date and time in linux

Server clocks has a tendency to drift, so to ensure correct date, time and zone once and for all is a good thing.

Setting the date
sudo date +%Y%m%d -s "20081128"

Setting the time
sudo date +%T -s "08:23:00"

Setting the timezone
This is done by making /etc/localtime into a symbolic link that goes to the timezone the server is to use.
sudo ln -sf /usr/share/zoneinfo/your/zone /etc/localtime

Setting UTC or local time
Change the content of the file /etc/sysconfig/clock to “UTC=true” or “UTC=false” (in norway it’s true).

Setting the hardware clock
Synchronizes the hw clock and system clock. Only add the “–utc” parameter if you are using UTC:
/sbin/hwclock --systohc --utc

Regurlarly synchronize the clock toward Network Time Protocol
This is done by adding this to crontab (ensure that the path to rdate is correct beforehand):
# update time with NTP server
0 3, 9, 15, 21 * * * /usr/sbin/rdate 128. 2. 136. 71 | logger -t NTP

Haridasi

About Haridasi

integrity - the state of being whole, entire, or undiminished.
This entry was posted in Linux. Bookmark the permalink.

2 Responses to Setting date and time in linux

  1. Klaatu says:

    I had literally never seen the ‘ln -s’ trick for setting timezone. That is so easy. Thank you!

    Additional bonus tip along with that; if the file /etc/localtime already exists, you can update the link to a new location with the -f flag:

    ln -f -s /usr/share/zoneinfo/NZ /etc/localtime

  2. Haridasi haridasi says:

    Great! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *