- LFCA: Learn to Manage Time and Date in Linux – Part 6
- Check Time and Date On a Linux System
- How to Set a Timezone on a Linux System
- Set Date and Time on a Linux System
- Set Automatic Time Synchronization using NTP Server
- Related Posts
- 1 thought on “LFCA: Learn to Manage Time and Date in Linux – Part 6”
- How to Set Time, Timezone and Synchronize System Clock Using timedatectl Command
- How to Find and Set Local Timezone in Linux
- How to Set Time and Date in Linux
- Set Time in Linux
- Set Date and Time in Linux
- How to Find and Set Hardware Clock in Linux
- Synchronizing Linux System Clock with a Remote NTP Server
- Summary
- How to Set Date, Time and Timezone in Linux
- Set Linux date
- Set Linux time
- Set Linux date and time
- Set Linux timezone
- Backup old timezone info if needed
- Find out the appropriate timezone from /etc/localtime and create a symbolic link to it
- Verify the timezone is changed
- Some related utilities
- Update the current system time by rdate
- Set the hardware clock
- Fedora/RHEL/CentOS have a date/time setting tool
LFCA: Learn to Manage Time and Date in Linux – Part 6
This article is Part 6 of the LFCA series, here in this part, you will acquaint yourself with the general system administration commands to manage time and date settings in the Linux system.
Time is crucial in any Linux system. Multiple services such as crontab, anacron, backup and restore services depend on accurate time to carry out their tasks as expected.
Linux has 2 types of clocks:
- Hardware clock – This is the battery-powered clock also referred to as the CMOS clock or RTC ( Real Time Clock). The clock runs independently of the operating system & keeps running even when the system is powered off provided the CMOS battery is present.
- System clock ( Software clock ) – This is also referred to as the kernel clock. At boot time, the system clock is initialized from the hardware clock and takes over from there.
Usually, there exists a time difference between the two clocks such that they gradually drift from each other. We will come to this later and show you how you can sync these clocks.
For now, we will see how you can check time and date on a Linux system.
Check Time and Date On a Linux System
There are two main utilities used to check time and date on a Linux system. The first is the date command. Without any arguments, it provides quite a bit of information shown
$ date Friday 26 March 2021 11:15:39 AM IST
To view the date in dd-mm-yy time format only, execute the command:
$ date +"%d-%m-%y" 26-03-21
If you just want to view the current time only and nothing else, use the command:
$ date "+%T" 11:17:11
The timedatectl command is a new utility used in modern Linux systems such as Ubuntu 18.04, RHEL 8 & CentOS 8. It’s a replacement of the date command which was prominent in the old SysVinit systems. It can be used to query and adjust the time on a Linux system.
Without any options, the timedatectl command prints out an array of information such as the local time, UTC time, RTC time, and the timezone to mention a few.
How to Set a Timezone on a Linux System
On a Linux system, time is dependent on the timezone that is set. To check the timezone that is configured on your system, issue the command:
From the output in the snippet above, I am in the Africa/Nairobi timezone. To view the available timezones, run the command:
$ timedatectl list-timezones
Press ENTER to scroll through the whole list of the possible time zones that are available.
Timezones are also defined in the /usr/share/zoneinfo/ path as shown.
There are a couple of ways that you can use to configure the timezone. Using the timedatectl command, you can set the timezone, for instance, to America/Chicago, using the syntax shown.
$ timedatectl set-timezone 'America/Chicago'
The other way you can set the timezone is to create a symbolic link from a timezone file in the /usr/share/zoneinfo path to /etc/localtime. For example, to set the local time zone to EST (Eastern Standard Time), issue the command:
$ sudo ln -sf /usr/share/zoneinfo/EST /etc/localtime
Set Date and Time on a Linux System
To set time only on a Linux system using the format HH:MM:SS (Hour: Minute: Second ), use the syntax below
$ timedatectl set-time 18:30:45
To set the date only in YY-MM-DD (Year: Month: Day) format, use the syntax:
$ timedatectl set-time 20201020
To set both date and time, run:
$ timedatectl set-time '2020-10-20 18:30:45'
NOTE: Manually setting time and date in this manner is not recommended since you are likely to configure inaccurate time and date settings. In fact, by default, automatic time synchronization is turned on to prevent you from making manual time and date settings.
The most recommended way to set time is by either specifying the time zone that you are in as shown earlier or turning on automatic time synchronization with a remote NTP server.
Set Automatic Time Synchronization using NTP Server
NTP is short for Network Time Protocol, which is an internet protocol that is used to automatically synchronize the system’s time clock with a pool on online NTP servers.
Using the timedatectl command, you can set automatic time synchronization as follows:
To disable automatic NTP time synchronization, execute:
Conclusion
The timedatectl and date commands are handy command-line tools that can help you check and adjust your time on Linux.
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I’m not running commands on the terminal, I’m taking listening to some cool music. taking a casual stroll or watching a nice movie.
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
1 thought on “LFCA: Learn to Manage Time and Date in Linux – Part 6”
“The timedatectl command is a new utility used in MODERN Linux systems such as Ubuntu 18.04, RHEL 8 & CentOS 8. It’s a replacement of the date command which was prominent in the OLD SysVinit systems.” (emphases mine) That statement insinuates that systemd is the new standard and SysVinit, and other non-systemd inits, are totally deprecated, which is very far from the truth. Whether a Linux distro is Modern or OLD is determined by the kernel version it uses, not the version of any of the distro’s components, even if it is the init. There still are many MODERN Linux systems, such as Gentoo, PCLinuxOS, Devuan, Artix, and others that use SysVinit, runit, or other inits rather than systemd. BTW – Ubuntu 18.04 can in no way be considered MODERN as Ubuntu 21.04 has been already released. “There are two main utilities used to check time and date on a Linux system.” You should differentiate right up front which command is used by distros using systemd as init and which command is used by distros using non-systemd inits. Somehow you did not provide the “date” command options for setting time zone, date & time, and how to sync with NTP servers. Reply
How to Set Time, Timezone and Synchronize System Clock Using timedatectl Command
The timedatectl command is a new utility for RHEL/CentOS 7/8 and Fedora 30+ based distributions, which comes as a part of the systemd system and service manager, a replacement for old traditional date command used in sysvinit daemon based Linux distributions.
The timedatectl command allows you to query and change the configuration of the system clock and its settings, you can use this command to set or change the current date, time, and timezone or enable automatic system clock synchronization with a remote NTP server.
In this tutorial, am going to take you through the ways you can manage time on your Linux system by setting the date, time, timezone, and synchronize time with NTP from the terminal using the new timedatectl command.
It is always a good practice to maintain the correct time on your Linux server or system and it can have the following advantages:
- maintain a timely operation of system tasks since most tasks in Linux are controlled by time.
- the correct time for logging events and other information on the system and many more.
How to Find and Set Local Timezone in Linux
1. To display the current time and date on your system, use the timedatectl command from the command line as follows:
In the screencast above, RTC time is the hardware clock time.
2. The time on your Linux system is always managed through the timezone set on the system, to view your current timezone, do it as follows:
# timedatectl OR # timedatectl | grep Time
3. To view all available timezones, run the command below:
# timedatectl list-timezones
4. To find the local timezone according to your location, run the following command:
# timedatectl list-timezones | egrep -o "Asia/B.*" # timedatectl list-timezones | egrep -o "Europe/L.*" # timedatectl list-timezones | egrep -o "America/N.*"
5. To set your local timezone in Linux, we will use the set-timezone switch as shown below.
# timedatectl set-timezone "Asia/Kolkata"
It is always recommended to use and set the coordinated universal time, UTC.
# timedatectl set-timezone UTC
You need to type the correct name timezone otherwise you may get errors when changing the timezone, in the following example, the timezone “Asia/Kolkata” is not correct therefore causing the error.
How to Set Time and Date in Linux
6. You can set the date and time on your system, using the timedatectl command as follows:
Set Time in Linux
To set time only, we can use a set-time switch along with the format of time in HH:MM:SS (Hour, Minute, and Seconds).
# timedatectl set-time 15:58:30
You may get the below error when setting the date as shown above:
Failed to set time: NTP unit is active
7. The error says that the NTP service is active. You need to disable it using the below command.
# systemctl disable --now chronyd
Set Date and Time in Linux
8. To set date and time, we can use a set-time switch along with the format of date in YY:MM:DD (Year, Month, Day) and time in HH:MM:SS (Hour, Minute, and Seconds).
# timedatectl set-time '2015-11-20 16:14:50'
How to Find and Set Hardware Clock in Linux
9. To set your hardware clock to coordinated universal time, UTC, use the set-local-rtc boolean-value option as follows:
First Find out if your hardware clock is set to local timezone:
Set your hardware clock to local timezone:
# timedatectl set-local-rtc 1
Set your hardware clock to coordinated universal time (UTC):
# timedatectl set-local-rtc 0
Synchronizing Linux System Clock with a Remote NTP Server
NTP stands for Network Time Protocol is an internet protocol, which is used to synchronize the system clock between computers. The timedatectl utility enables you to automatically sync your Linux system clock with a remote group of servers using NTP.
Please note that you must have NTP installed on the system to enable automatic time synchronization with NTP servers.
To start automatic time synchronization with a remote NTP server, type the following command at the terminal.
To disable NTP time synchronization, type the following command at the terminal.
Summary
These are very easy examples described in this tutorial and I hope you will find them helpful for setting various Linux system clocks and timezones. To learn more about this tool, head over to timedatectl man page.
If you have anything to say about this article, feel free to leave a comment for any more information to add. Stay connected to Tecmint.
How to Set Date, Time and Timezone in Linux
How to set date, time and timezone on Linux/Unix box will be introduced in this post.
Unix time, or POSIX time which is a system for describing points in time is the number of seconds elapsed since midnight UTC on the morning of January 1, 1970, not counting leap seconds.
The number of seconds elapsed can be got by this command on Linux/Unix systems:
Set Linux date
Linux date can be set using following syntax:
yyyy is year, mm is month and dd is day.
For example, we can set the date to June 22, 2010 by:
Set Linux time
Linux time can be set using following syntax:
hh is hour, mm is minite and ss is second.
For example, we can set the time to 11:28 by:
Set Linux date and time
The date and time can be set by date command at the same time by:
The first mm means month while the second mm means minite.
For example, we can set the date and time to 11:28 on June 22, 2010 by:
Another way to set new date and time is using the following syntax:
The method to set the date and time above is:
# date -s "22 JUN 2010 11:28:00"
# date --set="22 JUN 2010 11:28:00"
Set Linux timezone
The configuration file for timezone is usally /etc/localtime which is often a symlink to the file localtime or to the correct time zone file in the system. The time zone directory is /usr/share/zoneinfo where you can find a list of time zone regions. In some distro such as Fedora/RHEL/Cent OS, the zone files use /usr/share/zoneinfo/REGION/CITY like format.
The method for setting Linux timezone:
Backup old timezone info if needed
# mv /etc/localtime /etc/localtime.bak
Find out the appropriate timezone from /etc/localtime and create a symbolic link to it
For example we want to set the time zone to Hong Kong time:
# ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime
Verify the timezone is changed
You may get a output like this:
$ date Tue Jun 22 12:33:10 HKT 2010
Some related utilities
Here is some related Linux time/date related utilities.
Update the current system time by rdate
For a list of available time servers, please check NIST Internet Time Servers.
Set the hardware clock
Fedora/RHEL/CentOS have a date/time setting tool
You can use the GUI tool on Red Hat’s distros:
and then select the timezone entry.