- 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”
- Linux Get Current Time
- Conclusion:
- About the author
- Omar Farooq
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
Linux Get Current Time
The time function is utilized within all operating systems like Windows, Linux, Unix, etc. You may see the current date and time at the desktop screens of your operating system within standard formats. But what about the exact current time displayed on the Linux operating system. If you are looking for a guide toward using date and time functions to display the current date and time in the Linux shell, then this article is for you. So let’s start this guide by logging in to your Ubuntu 20.04 system, as we will be performing each command on the Ubuntu 20.04 Linux shell.
After a successful login, you first need to open the Ubuntu’s terminal shell via the activity area at the desktop taskbar. Tap on it and write “terminal” in the search bar shown on your screen. The pop-up terminal screen will be shown, and you have to tap on it to open it quickly. If this process is lengthy, try using the “CTRL+Alt+T” to launch it faster. Now, your terminal will be opened within no more than 10 seconds on your screen. Let’s start with the most basic command to display the date and time of our current time zone on the shell. On executing the following command, the time has been displayed in the “hour: minute: second” format along with the time zone, i.e., PKT. It also shows the current date according to the time zone. Upon execution, you will get the output, as shown below:
If you want to only display the date on your shell with the specific format, you need to specify the format in the date command. Use the inverted commas to add the format in “%d” for day, %m for month, %y for year separated by “-” signs. This command execution shows us the date in standard “day-month-year” format. Upon execution, you will get the following output:
If you only want to display the current time on your shell using the “date” command, you must use the “+%T” character flag. On execution of this command, the current time for a specific time zone will be displayed in a standard format, i.e., “hour: minute: second”. Upon execution, you will get the output, as shown below:
If you want to display both the current and time in a single line with the time and date specification, you can also do that with the date command. So, to display the title “Date” and “Time”, we need to add “+DATE: %D” for date and “TIME: “%T” for time. The output of this instruction shows the date and time in standard format with titles of date/time on the shell. Upon execution, you will get the following output:
For example, we want to get the exact date and time of the same time zone and same time for some past year. We need to utilize the “—date” flag with the “=” sign to get the value to be searched. For example, we want to get a date and time exactly three years back for the same moment. On execution, the following instruction shows the date and time for the exact three years back, i.e., February 27, 2019:
Upon execution, you will get the affixed output.
If we want to take an exact date and time for the very next day on the shell, we will use the same “date” command with the “—date” flag. Use the value “tomorrow” in the inverted commas and execute the command. This will show the next exact date from the current exact date and time, i.e., February 28, 2022.
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Another command is also known to get the current date and time for the current time zone of Linux. This command is the “timedatectl” instruction of Bash. It will not only show you the current local time but also the universal time, RTC time, your current time zone, and if the NTP services is enabled on your system. The execution of this command shows all the mentioned specifications on the shell, i.e., time and date. Upon execution, you will get the output, as shown below:
Let’s use the time zone date command to get the current time of our choice of time zone. So, we have to use the “TZ” variable with the “=” sign to get the time zone value. We want to get the current time for “Asia/Karachi” this time. The use of the “date” keyword with the “+%T” is necessary for fetching time for this time zone. We have the time displayed for the “Asia/Karachi” time zone upon execution. If you want to get the value for another time zone, use the specific time zone as a value to TZ. Let’s say we have been using the time zone “Asia/Istanbul” to get the current time for Istanbul, Turkey. The instruction shows the time for “Istanbul, Turkey” on the shell. Upon execution, you will get the following output:
$ TZ = “Asia / Karachi” date “+ % T”
$ TZ = “Asia / Istanbul” date “+ % T”
Conclusion:
In this article, we have tried to implement almost all the commands to get the current date and time for our current time zone. We have also tried to get the current time for other time zones, the past time and date, the coming date and time, and many more. You can also use %r and %R to get the current time. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.