howtouselinux

3 ways to check time in Linux

Table of Contents

 

There are a few different ways to check the time on your Linux machine. In this blog post, we will discuss three of them. The first way is to use the date command. This command displays the current date and time in human-readable format. The second way is to use the timedatectl command. This command allows you to view and change the system time and date settings. The third way is to use the hwclock command. This command shows or sets hardware clock information on Linux systems.

Understanding time in Linux

In Linux, time is measured in seconds since the Unix epoch. The Unix epoch is January 1, 1970 UTC. This means that the time on your Linux machine is always relative to this date.

Checking the time on your Linux machine is important for a number of reasons. For example, if you are running a cron job, you will want to make sure that the time is set correctly so that the job will run at the correct time. Additionally, if you are troubleshooting an issue on your machine, checking the time can be helpful in determining when the issue occurred.

check time with date command in Linux

The best way to check time in Linux is using the date command. Open the terminal and type “date” at the command prompt. This command displays the current date and time in human-readable format.  The output will look something like this: Tue Jan 22 23:59:59 EST 2020

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year,%m with month, and %d with the day of the month:

date +”Year: %Y, Month: %m, Day: %d”
Year: 2022, Month: 04, Day: 16

Here are more examples of time format.
Command: $date “+%D”
Output: 10/11/30
Command: $date “+%D %T”
Output: 10/11/30 16:13:27
Command: $date “+%Y-%m-%d”
Output: 2030-10-11
Command: $date “+%Y/%m/%d”
Output: 2030/10/11

The following are the date format options.
$date +%[format-option]
%D: Display date as mm/dd/yy.
%d: Display the day of the month (01 to 31).
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

check time with timedatectl command in Linux

We can also use timedatectl command to check time in Linux. This command allows you to view and change the system time and date settings. To use the timedatectl command, type “timedatectl” at the command prompt. This will display the current system time and date settings.

$ timedatectl
Local time: Sat 2022-04-16 13:06:00 GMT
Universal time: Sat 2022-04-16 13:06:00 UTZ
RTC time: Sat 2022-04-16 13:02:34
Time zone: GMT (GMT, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes

To change the system time, use the set-time option. For example, to set the system time to 12:00pm on January 22nd, 2030, you would type the following command: timedatectl set-time “2030-01-22 12:00:00”

check time with hwclock command in Linux

We can also check the time in Linux with the hwclock command. This command shows or sets hardware clock information on Linux systems. The hardware clock is a clock that is set by the BIOS and is used to keep track of the time when the system is turned off. The system time is the time that is set by the operating system and is used when the system is turned on.

To use the hwclock command, type “hwclock” at the command prompt. This will display the current system time in hardware clock format. To set the system time, use the –set option. For example, to set the system time to 12:00pm on January 22nd, 2019, you would type the following command: hwclock –set –date “2030-01-22 12:00:00”

Why is my computer’s time wrong?

There are a few reasons why your computer’s time might be wrong. It could be because your computer’s BIOS is not set correctly, or because your computer is not syncing its time with an NTP server. To fix this, you can try setting your computer’s BIOS to the correct time, or syncing your computer’s time with an NTP server.

To set your computer’s BIOS to the correct time, you will need to enter the BIOS setup utility. This is usually done by pressing a key during bootup, such as F12 or Esc. Once in the BIOS setup utility, look for the “Date” and “Time” settings. Set the date and time to the correct values, then save your changes and exit.

How to change the date and time in Linux?

The date command can be used to do a few different things, such as displaying the current date and time, setting the date and time.

To set the date and time, you can use the “-s” option with the date command. For example, to set the date to January 22nd, 2030 and the time to 23:59:59, you would type the following command: date -s “22 Jan 2030 23:59:59”

The date command has a few different format options that you can use. The most common format is “MMDDhhmmYYYY”. For example, to set the date to February 2nd, 2020 at 6pm, you would type the following command: date -s “02 Feb 2020 18:00:00”

The second most common format is “YYYYMMDD”. For example, to set the date to February 29th, 2020, you would type the following command: date -s “20200229”. You can also use the ISO 8601 standard date format. For example, to set the date to February 29th, 2020, you would type the following command: date -s “2020-02-29”

How to change the timezone in Linux?

To change the timezone in Linux, you can use the “timedatectl” command. For example, to set your timezone to America/New_York, you would type the following command: sudo timedatectl set-timezone America/New_York

How to Sync time with  ntp service in Linux

The ntp service is a daemon that runs on Linux systems and synchronizes the system time with an NTP server. This ensures that the system time is accurate even when the system is turned off. To use the ntp service, you will need to install the ntp package. Once installed, you can start the service by running “sudo service ntp start”. To configure the ntp service, you can edit the “/etc/ntp.conf” file.

To check if your computer’s NTP service is active, you can use the “timedatectl” command. For example, to check if the NTP service is active, you would type the following command: sudo timedatectl status | grep -i ntp

If the NTP service is active, you will see a line that says “NTP synchronized: yes”. If the NTP service is not active, you will see a line that says “NTP synchronized: no”.

To manually sync your computer’s time with an NTP server, you can use the “ntpdate” command. For example, to sync your computer’s time with the NTP server pool.ntp.org, you would type the following command: sudo ntpdate pool.ntp.org

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press