63

The Ubuntu server's current date and time is different from the time zone date and time. I have tried using:

sudo date "30 Sep 2015 4:43:42"

to change it but it did not change the date and time, just printed on terminal the date and time I changed, but when I executed:

sudo hwclock --show

The date and time is still the old one.

What is the correct way to change date and time of Ubuntu Server?

chaos
  • 27,506
  • 12
  • 74
  • 77

7 Answers7

105

You can set the system date with this command:

sudo date --set="2015-09-30 10:05:59.990"

Then when using date, it should be showed correctly.

Now you should also the set hardware clock in the BIOS of the system, that the setting persists over a reboot (dureing the startup the system time is set to the value of the hardware clock). Do that with hwclock:

sudo hwclock --systohc

This gets the system clocks (sys) value and sets the hardware clock (hc). Check it with the hwclock command. Both hwclock and date should now show the same date and time.


To set your timezone, you can use this command:

sudo dpkg-reconfigure tzdata

BTW: If you use a this machine as a server, I strongly recommend using an NTP-Client to sync the time over network. So you can guarantee that all your servers have the exactly same time set. This will sync the time while the machine runs. If you have applications which are dependent of synced time over server, I recommend the NTP-Daemon. The longer it runs in the background, the more precise is the time.

Arronical
  • 19,893
chaos
  • 27,506
  • 12
  • 74
  • 77
  • 1
    I missed the clock synchronization step. Thank you for your answer, it helped me! – Priska Aprilia Sep 30 '15 at 08:19
  • 1
    3 option helps, it changed etc/timezone =) like http://php.net/manual/en/timezones.php !!! absolutely identical !!! – Vladimir Ch Feb 25 '17 at 17:08
  • @VladimirCh Fortunatelly time zones names are stadarized, so we don't have to make any adjustments between systems. Oh, wait... there are some Microsoft version too... – PeterM Mar 25 '17 at 12:46
  • 3
    I try to change but its not changing:- $ sudo date --set="2015-09-30 10:05:59.990" Wed Sep 30 10:05:59 +0530 2015 $ date Thu May 23 15:10:37 +0530 2019 – Vinod May 23 '19 at 09:43
  • this should not be the accepted answer since it mentions timezone explicitly – Nick Andriopoulos Jun 03 '20 at 08:43
  • pls add sudo timedatectl set-ntp 0 to make sure NTP is disable, this one helps me – Ryan Arief Jun 24 '20 at 15:19
  • I get an error with sudo date --set="2015-09-30 10:05:59.990": invalid date '=2015-09-30 10:05:59.990'. It works when I use the same command omitting '=': sudo date --set "2015-09-30 10:05:59.990" – Giorgos Betsos Nov 07 '22 at 13:35
  • I tried sudo date --set="2024-02-06 23:58:00.0" but it did not work in Ubuntu 23.04 running in VirtualBox 7. Any idea why? I don't want to change the actual BIOS time, just within VirtualBox machine. – rusty Feb 06 '24 at 12:45
40
  1. Search for your timezone
timedatectl list-timezones
  1. Set your timezone
sudo timedatectl set-timezone America/Toronto
  1. Enable timesyncd
sudo timedatectl set-ntp on

With this, time should be set and synchronized.

You can see more on this tutorial : https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-18-04

Nato Boram
  • 1,188
10

I dislike setting system time manually. So to fix this issue I had to combine two different answers.
To fix system time you have to use this code:

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

as given in this answer
Then you sync the hardware clock with system clock using

sudo hwclock --systohc

as given by @chaos in this thread.

muru
  • 197,895
  • 55
  • 485
  • 740
twitu
  • 295
3

Me helped:


1 - step (preparation)

 timedatectl set-local-rtc 0
 sudo timedatectl set-ntp 1
 sudo hwclock --systohc
 sudo timedatectl set-ntp 0


2 - step (set datetime)

sudo timedatectl set-time "06:24:00"
sudo timedatectl set-time "2020-04-23"
sudo hwclock --systohc

or

sudo date --set="2020-04-23 06:24:25.990"
sudo hwclock --systohc


3 - step (check datetime)

timedatectl

3

I'm using Ubuntu-based servers on Amazon AWS. All of the SUDO DATE answers DID NOT WORK for me. SUDO DATE returned the new date as the output, but subsequent DATE invocations still shows the old date. Also, HWCLOCK did nothing but return an error.

The answer for me was:

sudo timedatectl set-ntp false

sudo timedatectl set-time "date-time-string"

1

For those looking for the epoch seconds version

  1. Set the current date and time

    sudo date -s @1565864862
    
  2. Get the current date and time

    date +%s
    

    outputs 1565864862

1

just type in

sudo date newdatestring

with newdatestring in the format nnddhhmmyyyy.ss

  • nn: the (two digit) month (01 to 12)
  • dd: the (two digit) day (01 to 31), with the regular rules for days according to month and year applying
  • hh: the (two digit) hour (00 to 23)
  • mm: the (two digit) minute (00 to 59)
  • yyyy: the year; it can be two digit or four digit
  • ss is two digit seconds (00 to 59). Notice the period ‘.’ before the ss.

But beside the date command, maybe you prefer the NTP "solution" (network time protocol): Serverguide - NTP, much easier to handle and more precise than setting the date by hand. You can use a cronjob or the ntp daemon (ntpd) to update you time every x hours/minutes...

Hope this helps!

Wolfgang
  • 718
  • 5
  • 13