Getting Current Date, Time and Timezone
Time synchronization is enabled on Ubuntu 18.04 by default using systemd's timesyncd service. Usetimedatectl
to confirm that your system is set with time synchronization:$ timedatectl
Local time: Wed 2018-03-21 07:21:41 AEDT
Universal time: Tue 2018-03-20 20:21:41 UTC
RTC time: Tue 2018-03-20 20:21:42
Time zone: Australia/Sydney (AEDT, +1100)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no
From the above timedatectl
command output on Line 6 we can see that the Ubuntu time sync is enabled. Furthermore, Line 5 we can also see the configured timezone.Timezone settings
In order to have an accurate time and time synchronization to work properly, the system has to be configured with the correct timezone. Set your correct timezone before proceeding further by following our How to change timezone on Ubuntu 18.04 tutorial.Enable/Disable Time Synchronization
To turn off your Ubuntu time sync enter:$ sudo timedatectl set-ntp off $ timedatectl Local time: Wed 2018-03-21 07:44:53 AEDT Universal time: Tue 2018-03-20 20:44:53 UTC RTC time: Tue 2018-03-20 20:44:54 Time zone: Australia/Sydney (AEDT, +1100) System clock synchronized: yes systemd-timesyncd.service active: no RTC in local TZ: noIf your time synchronization is disabled, execute the following linux command to enable your Ubuntu time sync:
$ sudo timedatectl set-ntp onYour Ubuntu 18.04 system is now time synchronized.
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
Test Time Sync on Ubuntu
The following linux commands will confirm that the time sync is set correctly.First, use the
date
command to check the current date and time:$ date Wed 21 Mar 07:54:50 AEDT 2018Next, with time sync enabled set an incorrect time. For example the following linux command will set the time few minutes ahead:
$ sudo date 03210800 Wed 21 Mar 08:00:00 AEDT 2018If your time synchronization is enabled correctly the time will reset to the synced time:
$ date Wed 21 Mar 07:55:09 AEDT 2018Next, disable Ubuntu systemd's timesyncd service:
$ sudo timedatectl set-ntp offSet an incorrect time, check the date and re-enable time sync:
$ sudo date 03210800 Wed 21 Mar 08:00:00 AEDT 2018 $ date Wed 21 Mar 08:00:14 AEDT 2018 $ sudo timedatectl set-ntp on $ date Wed 21 Mar 07:55:45 AEDT 2018If your time has switched back after you had re-enabled time synchronization then your Ubuntu time sync is enabled correctly.
--------------------------------------------------------------------------------------------------------------------------
Check Current Timezone Settings
Let's start by checking the current timezone settings. Use thetimedatectl
command to show the current timezone and time:$ timedatectl Local time: Tue 2018-06-06 10:27:34 PST Universal time: Tue 2018-06-06 18:27:34 UTC RTC time: Tue 2018-06-06 18:27:35 Time zone: Canada/Yukon (PST, -0800) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: noAnother alternative to check current timezone on Ubuntu 18.04 system, if the above command from some reason fails, is to check for
/etc/localtime
symbolic link:$ ls -l /etc/localtime lrwxrwxrwx 1 root root 32 Jun 6 10:27 /etc/localtime -> /usr/share/zoneinfo/Canada/Yukon
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
Show all Available Timezones
In order to change timezone on Ubuntu 18.04, we first need to obtain a timezone name of the timezone we wish to change to. This is usually a CONTINENT/CITY pair.The
timedatectl
command comes again handy:$ timedatectl list-timezonesThe timezone list is quite extensive. Scroll page down and up with PgDn and PgUp key respectively.
Alternatively, use
grep
command to narrow down your search. For example the bellow command will list all Europe cities:$ timedatectl list-timezones | grep -i europe Europe/Amsterdam Europe/Andorra Europe/Astrakhan Europe/Athens Europe/Belgrade Europe/Berlin Europe/Bratislava Europe/Brussels Europe/Bucharest Europe/Budapest Europe/Busingen ... Europe/ZurichNOTE:
The
timedatectl
command is using /usr/share/zoneinfo/
directory to generate the timezone list.Change Timezone
Now that we know the timezone name we wish to change our timezone settings to, usetimedatectl
command to set a new timezone.For example let's change timezone to
Europe/Bratislava
:$ sudo timedatectl set-timezone Europe/BratislavaUsing the
timedatectl
command is a preferred way on how to set a timezone on Ubuntu 18.04. However, note that you can also change timezone settings manually:NOTE:Changing the timezone using the manual method involving
ln
command may take minute or so to take an effect.$ sudo unlink /etc/localtime $ sudo ln -s /usr/share/zoneinfo/Europe/Bratislava /etc/localtime
Confirm Timezone Change
Lastly, confirm your new timezone settings:$ timedatectl Local time: Tue 2018-06-06 19:57:17 CET Universal time: Tue 2018-06-06 18:57:17 UTC RTC time: Tue 2018-06-06 18:57:18 Time zone: Europe/Bratislava (CET, +0100) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: noAlternatively, confirm a new timezone settings using
ls
command:$ ls -l /etc/localtime lrwxrwxrwx 1 root root 37 Jun 6 20:00 /etc/localtime -> /usr/share/zoneinfo/Europe/Bratislava
Change timezone from GUI
To change timezone from a default GNOME graphical user interface. Navigate toSettings-->Details-->Date & Time
:
Tidak ada komentar:
Posting Komentar