Set Time Zone and Synchronize System Clock to Your Time Zone in Linux
In this tutorial we will look at how to change your time zone settings in Linux and how to use NTP to sync Linux system clock to your time zone.
Check Time Zone Settings
Check the current time zone settings.
2016年 03月 02日 星期三 02:50:15 EST
As you can see, my system is set to EST (Eastern Standard Time). But I live in China right now. So I need to change time zone settings from EST to CST (China Standard Time).
Change Time Zone on Debian-Based Linux Distros
On Debian-based Linux distros, including Debian, Ubuntu, Linux Mint, Elementary OS etc, you can change your time zone settings with this command.
sudo dpkg-reconfigure tzdata
It will open up a dialog in the terminal to let your select your geographic area.
Next, select the city or region corresponding to your time zone. I live in China, so I selected Shanghai.
After you hit the OK button, your system clock will be immediately changed to your time zone clock.
A Distro-Agnostic Way to Change Time Zone Settings
This method works on all Linux distributions. First open a terminal and run this command:
sudo ln -sf /usr/share/zoneinfo
Don’t press Enter yet, press Tab key instead.
A list of geographic area will appear, now enter the name of your area after sudo ln -sf /usr/share/zoneinfo/. For example, I entered Asia. Then press Tab key again. A list of City will appear.
Enter the name of city corresponding to your time zone. For instance, I entered Shanghai. Now press space key and enter /etc/localtime. So the final command will be
sudo ln -sf /usr/share/zoneinfo/your-geographic-area/your-city /etc/localtime
Press enter to run this command. It will create a symbolic link pointing /etc/localtime file to your specific time zone file. Your system’s clock will be updated immediately, as you can find out by entering date command in the terminal. But users with a desktop environment such as XFCE need to log out and log back in to see the changes on the task bar.
Real Time Clock
RTC stands for real time clock, also known as hardware clock. This clock is powered by CMOS battery of your computer’s motherboard. This clock runs all the time even if you shutdown your computer. Sometimes RTC time is incorrect.
To find out RTC time on your computer, run this command:
Local time: Wed 2016-03-02 17:47:19 CST Universal time: Wed 2016-03-02 09:47:19 UTC RTC time: Wed 2016-03-02 09:47:19 Time zone: Asia/Shanghai (CST, +0800) Network time on: yes NTP synchronized: yes RTC in local TZ: no
As you can see, my local time is now 17:49:19, but RTC time is 09:47:19. RTC time is not in my local time zone and sometimes this can causes problems if the RTC time is inaccurate. RTC time is unreliable.
To let your system read RTC time in UTC standard, execute the following command.
timedatectl set-local-rtc 0
To let your system read RTC time in local time zone, run this command
timedatectl set-local-rtc 1
It’s recommended your system read RTC time in UTC standard to prevent unexpected behavior.
Synchronize System Clock to Your Time Zone
Sometimes, your system clock can still be incorrect even if you set the correct time zone. To make sure your system clock is synchronized to your local time zone, you need NTP.
NTP’s job is to provide accurate time on your network and there are many service that rely on accurate time to function correctly. NTP synchronize clock across your network so that time is as accurate as possible. NTP is an application layer protocol and it use UDP as the transport layer protocol. Network time servers get their time from atomic clocks.
On systemd you can enable NTP synchronization using timedatectl.
sudo timedatectl set-ntp true
The above command will start and enable the systemd-timesyncd service. Now check status
Local time: Wed 2016-03-02 18:30:35 CST Universal time: Wed 2016-03-02 10:30:35 UTC RTC time: Wed 2016-03-02 10:30:35 Time zone: Asia/Shanghai (CST, +0800) Network time on: yes NTP synchronized: yes RTC in local TZ: no
You can see that network time is on and NTP synchronization is enabled. To check the status of timesyncd:
sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2016-03-02 18:30:29 CST; 1h 37min ago Docs: man:systemd-timesyncd.service(8) Main PID: 2243 (systemd-timesyn) Status: "Synchronized to time server 184.108.40.206:123 (0.arch.pool.ntp.org)." Tasks: 2 (limit: 512) CGroup: /system.slice/systemd-timesyncd.service └─2243 /usr/lib/systemd/systemd-timesyncd