How to change ubuntu's server date and time via command line?
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.
- Search for your timezone
timedatectl list-timezones
- Set your timezone
sudo timedatectl set-timezone America/Toronto
- 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
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.