/ Linux

Properly configure time and date on Linux

Nothing worse than struggling with incorrect times and dates. Your whole application stack from logs to websites will rely on your systems time and date, it is therefor not only important to set it but also to keep it automatically syncronised. In the following I will walk you through, how you set your timezone, how you find your nearest NTP (Network Time Protocol) server and make it syncronise.

Find and set your timezone

First you need to find your timezone. The timezones are defined as Region/City you can either list all available options, but there will be a long list

timedatectl list-timezones

or better to add some keyword to filter the output. You can either swap keyword with a close metropol or your region.

timedatectl list-timezones | grep -i keyword

Once you have found your desired location make your system use that

sudo timedatectl set-timezone 'Asia/Bangkok'

Automatic syncronised

To syncronise your date and time, you need to install the NTP client

sudo apt-get install ntp

Then find a nearby NTP pool server at The NTP Pool Project. Once you have found it open the configurations file and replace the 4 lines with servers (Server 0 to 3).

sudo nano /etc/ntp.conf

If you are located in Asia you could replace with this

server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org

Restart the NTP service and check that all configurations went correct

sudo /etc/init.d/ntp restart
sudo timedatectl set-ntp on
sudo systemctl enable ntp
timedatectl

You should get an output that looks similar to this

     Local time: Fri 2016-06-17 12:37:47 ICT
  Universal time: Fri 2016-06-17 05:37:47 UTC
        RTC time: Tue 2016-06-07 22:50:28
       Time zone: Asia/Bangkok (ICT, +0700)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

What important here is that your Local time and Time zone are correct and NTP enabled and NTP synchronized are yes.