- 11 Useful Linux date command examples (How to set date and time in Linux)
- Syntax
- Linux date command Examples
- Example 1: How to check current date and time using date command
- Example 2: How to check future date using date command
- Example 3: How to check version of date command
- Example 4: How to Check current UTC Time using date command
- Example 5: How to Set date and time in Linux
- Example 6: How to Set only time in Linux using date command
- Example 7: How to Set only date in Linux using date command
- Example 8: How to Check the Last Modification date timestamp of a file
- Example 9: How to Display date in RFC Format
- Example 10: Using Format Specifiers with date command
- Example 11: Check Other date command options
- Set Time, Date Timezone in Linux from Command Line or Gnome | Use ntp
- Set Time/Date/Timezone in Ubuntu Linux
- Check Current Time
- Using the date command
- Using timedatectl command
- Changing Time
- using date command
- Change Date
- Create custom date format
- List/Change time zone
- Set the Local-rtc
- Check/Change CMOS Time
- Conclusion
- System time
- Contents
- Hardware clock
- Read hardware clock
- Set hardware clock from system clock
- System clock
- Read clock
- Set system clock
- Time standard
- UTC in Microsoft Windows
- Historical notes
- UTC in Ubuntu
- Time zone
- Setting based on geolocation
- Update timezone every time NetworkManager connects to a network
- Time skew
- Time synchronization
- Per-user/session or temporary settings
- Troubleshooting
- Clock shows a value that is neither UTC nor local time
- Tips and tricks
- fake-hwclock
11 Useful Linux date command examples (How to set date and time in Linux)
Table of Contents
In this article, I will take you through 11 Useful Linux date command examples. date is an important Linux command frequently used to set System date and time. You might have encountered few cluster based set up where each and every node needs to be in date and time sync with each other to be able to run successfully. Once it goes out of date or time sync beyond few seconds or minutes, cluster goes down. Hence it is very important to set the date and time correctly in the System. In this session we will look at how to set date and time in Linux using date command examples.
Syntax
date [OPTION]. [+FORMAT]
date [-u|—utc|—universal] [MMDDhhmm[[CC]YY][.ss]]
Linux date command Examples
Example 1: How to check current date and time using date command
If you want to check current date and time of your System then you need to simple run date command as shown below.
Example 2: How to check future date using date command
If you want to check date of some future day then you can check it by using —date option.
To check Next Tueday date you can use below command.
To check Next Sunday date you can use below command.
To check tomorrow date you can use below command.
To check yesterday date you can use below command.
To check after 2 Years date you can use below command.
To check after 2 months date you can use below command.
—date : display time described by STRING, not ‘now’. More info on date command Man page.
Example 3: How to check version of date command
If you want to check the version of date command then you need to use —version option as shown below. As you can see from below output current version is 8.22
—version : output version information and exit. More info on date command Man page.
Example 4: How to Check current UTC Time using date command
You can check the current Coordinated Universal Time by using date -u command.
-u : print or set Coordinated Universal Time (UTC)
Example 5: How to Set date and time in Linux
If you want to change both data and time then you can use —set option with date command to change that as shown below.
—set : set time described by STRING
Example 6: How to Set only time in Linux using date command
If you want to only change the time then you can use the same —set option as shown in above example and set the time only instead of setting both date and time.
Example 7: How to Set only date in Linux using date command
If you want to change only date but not the time then you need to first check the current time and then set accordingly. For example, if you check the current date and time you can see that current date is 16th May 2020 and current time is 13:16:04 EDT .
So, if you want to change the date only say to 17 May,2020 in this example keeping the time same then you need to use below date command.
Example 8: How to Check the Last Modification date timestamp of a file
If you want to check the last modification date timestamp of a file then you need to use -r option with date command as shown below. In this example we are checking the last modification date timestamp of a file file1.txt using date -r file1.txt command.
-r : display the last modification time of FILE
Example 9: How to Display date in RFC Format
If you want to display date in RFC format then you need to use date -R command as shown below.
-R : output date and time in RFC 2822 format
Example 10: Using Format Specifiers with date command
There are lot of format specifiers available to use with date command as per the needs. You can check and find its usages below.
%a locale’s abbreviated weekday name (e.g., Sun)
%A locale’s full weekday name (e.g., Sunday)
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale’s equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale’s 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale’s date representation (e.g., 12/31/99)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
Example 11: Check Other date command options
If you want to check all the other options available with date command you can use date —help command as shown below.
—help : display this help and exit
Источник
Set Time, Date Timezone in Linux from Command Line or Gnome | Use ntp
Written by Guillermo Garron
Date: 2012-04-19 15:55:00 00:00
To have the correct time and date in Linux is very important, a lot of things depends on it. It does not matter if you are using Linux to power your personal computer or you have a Linux server. The server and system clock needs to be on time.
Set date from the command line
Set time from the command line
Set time and date from the command line
Linux check date from command line
Will show you something like this:
Set hardware clock
The hardware clock is the clock that runs in you PC hardware even if you disconnect it from the main power supply. This is because it has a lithium battery in the modern computers and another type of battery in the old ones.
We’ll see differences between hardware clock and system clock
Will output something like this:
Now check the system clock
Will output something like this:
Let’s set the hardware clock to local time:
If you want to set it to UTC time use:
Set the timezone
To set the timezone of your system clock do the following:
Choose the right timezone for you.
Automatically adjust your computer clock
To have your system to automatically adjust time we need to install ntp . Get it from your repository. Once installed you can configure it this way:
Edit the file /etc/ntpd.conf . It will look like this:
Be sure to start the daemon, and to make it start automatically when the system boots.
On Arch Linux is: /etc/rc.d/ntpd start on Debian and derivatives /etc/init.d/ntpd start
Update from the command line against a time server
You can update the clock manually, without the need of the daemon with ntpdate
You will get something like this:
Bonus: Set the time and Date on Gnome
If you are using Gnome right click on the clock and select adjust, or go to: System > Administration > Time and Date (You may be asked for root password)
You will see a window similar to this one:
If you enjoyed the article, please share it
Источник
Set Time/Date/Timezone in Ubuntu Linux
Time is an important aspect in Linux systems especially in critical services such as cron jobs. Having the correct time on the server ensures that the server operates in a healthy environment that consists of distributed systems and maintains accuracy in the workplace.
In this tutorial, we will focus on how to set time/date/time zone and to synchronize the server clock with your Ubuntu Linux machine.
Check Current Time
You can verify the current time and date using the date and the timedatectl commands. These linux commands can be executed straight from the terminal as a regular user or as a superuser. The commands are handy usefulness of the two commands is seen when you want to correct a wrong time from the command line.
Using the date command
Log in as a root user and use the command as follows
Output
You can also use the same command to check a date 2 days ago
Output
Using timedatectl command
Checking on the status of the time on your system as well as the present time settings, use the command timedatectl as shown
Changing Time
We use the timedatectl to change system time using the format HH:MM: SS. HH stands for the hour in 24-hour format, MM stands for minutes and SS for seconds.
Setting the time to 09:08:07 use the command as follows (using the timedatectl)
using date command
Changing time means all the system processes are running on the same clock putting the desktop and server at the same time. From the command line, use date command as follows
Where,
• 10: Hour (hh)
• 13: Minute (mm)
• 13: Second (ss)
To change the locale to either AM or PM use the %p in the following format.
Change Date
Generally, you want your system date and time is set automatically. If for some reason you have to change it manually using date command, we can use this command :
It will set your current date and time of your system into ‘January 25, 2014′ and ’09:17:00 AM’. Please note, that you must have root privilege to do this.
You can use timedatectl to set the time and the date respectively. The accepted format is ‘YYYY-MM-DD’, ‘YYYY’ represents the year, ‘MM’ the month in two digits and ‘DD’ for the day in two digits.
Changing the date to ’15 January 2019′, you should use the following command:
Create custom date format
To create custom date format, use a plus sign (+)
%D format follows Year/Month/Day format.
You can also put the day name if you want. Here are some examples :
List/Change time zone
Changing the time zone is crucial when you want to ensure that everything synchronizes with the Network Time Protocol. The first thing to do is to list all the region’s time zones using the list-time zones option or grep to make the command easy to understand
The above command will present a scrollable format.
Recommended timezone for servers is UTC as it doesn’t have daylight savings. If you know, the specific time zones set it using the name using the following command
To display timezone execute
Set the Local-rtc
The Real-time clock (RTC) which is also referred to as the hardware clock is independent of the operating system and continues to run even when the server is shut down.
Use the following command
In addition, the following command for the local time
Check/Change CMOS Time
The computer CMOS battery will automatically synchronize time with system clock as long as the CMOS is working correctly.
Use the hwclock command to check the CMOS date as follows
To synchronize the CMOS date with system date use the following format
Conclusion
To have the correct time for your Linux environment is critical because many operations depend on it. Such operations include logging events and cron jobs as well.
If you have any questions or feedback, feel free to leave a comment.
Источник
System time
This article or section needs expansion.
In an operating system, the time (clock) is determined by three parts: time value, whether it is local time or UTC or something else, time zone, and Daylight Saving Time (DST) if applicable. This article explains what they are and how to read/set them. Two clocks are present on systems: a hardware clock and a system clock which are also detailed in this article.
Standard behavior of most operating systems is:
- Set the system clock from the hardware clock on boot.
- Keep accurate time of the system clock, see #Time synchronization.
- Set the hardware clock from the system clock on shutdown.
Contents
Hardware clock
The hardware clock (a.k.a. the Real Time Clock (RTC) or CMOS clock) stores the values of: Year, Month, Day, Hour, Minute, and Seconds. Only 2016, or later, UEFI firmware has the ability to store the timezone, and whether DST is used.
Read hardware clock
Set hardware clock from system clock
The following sets the hardware clock from the system clock. Additionally it updates /etc/adjtime or creates it if not present. See hwclock(8) section «The Adjtime File» for more information on this file as well as the #Time skew section.
System clock
The system clock (a.k.a. the software clock) keeps track of: time, time zone, and DST if applicable. It is calculated by the Linux kernel as the number of seconds since midnight January 1st 1970, UTC. The initial value of the system clock is calculated from the hardware clock, dependent on the contents of /etc/adjtime . After boot-up has completed, the system clock runs independently of the hardware clock. The Linux kernel keeps track of the system clock by counting timer interrupts.
Read clock
To check the current system clock time (presented both in local time and UTC) as well as the RTC (hardware clock):
Set system clock
To set the local time of the system clock directly:
sets the time to May 26th, year 2014, 11:13 and 54 seconds.
Time standard
There are two time standards: localtime and Coordinated Universal Time (UTC). The localtime standard is dependent on the current time zone, while UTC is the global time standard and is independent of time zone values. Though conceptually different, UTC is also known as GMT (Greenwich Mean Time).
The standard used by the hardware clock (CMOS clock, the BIOS time) is set by the operating system. By default, Windows uses localtime, macOS uses UTC, other UNIX and UNIX-like systems vary. An OS that uses the UTC standard will generally consider the hardware clock as UTC and make an adjustment to it to set the OS time at boot according to the time zone.
If multiple operating systems are installed on a machine, they will all derive the current time from the same hardware clock: it is recommended to adopt a unique standard for the hardware clock to avoid conflicts across systems and set it to UTC. Otherwise, if the hardware clock is set to localtime, more than one operating system may adjust it after a DST change for example, thus resulting in an over-correction; problems may also arise when traveling between different time zones and using one of the operating systems to reset the system/hardware clock.
The hardware clock can be queried and set with the timedatectl command. You can see the current hardware clock time standard of the Arch system using:
To change the hardware clock time standard to localtime, use:
To revert to the hardware clock being in UTC, type:
These generate /etc/adjtime automatically and update the RTC accordingly; no further configuration is required.
During kernel startup, at the point when the RTC driver is loaded, the system clock may be set from the hardware clock. Whether this occurs depends on the hardware platform, the version of the kernel and kernel build options. If this does occur, at this point in the boot sequence, the hardware clock time is assumed to be UTC and the value of /sys/class/rtc/rtcN/hctosys (N=0,1,2. ) will be set to 1.
Later, the system clock is set again from the hardware clock by systemd, dependent on values in /etc/adjtime . Hence, having the hardware clock using localtime may cause some unexpected behavior during the boot sequence; e.g system time going backwards, which is always a bad idea (there is a lot more to it). To avoid it systemd will only synchronize back, if the hardware clock is set to UTC and keep the kernel uninformed about the local timezone. As a consequence timestamps on a FAT filesystem touched by the Linux system will be in UTC.
UTC in Microsoft Windows
To dual boot with Windows it is recommended to configure Windows to use UTC, rather than Linux to use localtime. (Windows by default uses localtime [1].)
It can be done by a simple registry fix: Open regedit and add a DWORD value with hexadecimal value 1 to the registry:
You can do this from an Administrator Command Prompt running:
Alternatively, create a *.reg file (on the desktop) with the following content and double-click it to import it into registry:
Should Windows ask to update the clock due to DST changes, let it. It will leave the clock in UTC as expected, only correcting the displayed time.
The #Hardware clock and #System clock time may need to be updated after setting this value.
If you are having issues with the offset of the time, try reinstalling tzdata and then setting your time zone again:
Historical notes
For really old Windows, the above method fails, due to Windows bugs. More precisely,
- For 64-bit versions of Windows 7 and older builds of Windows 10, there was a bug that made it necessary to have a QWORD value with hexadecimal value of 1 instead of a DWORD value. This bug has been fixed in newer builds and now only DWORD works.
- Before Vista SP2, there is a bug that resets the clock to localtime after resuming from the suspend/hibernation state.
- For XP and older, there is a bug related to the daylight saving time. See [2] for details.
- For even older versions of Windows, you might want to read https://www.cl.cam.ac.uk/
mgk25/mswish/ut-rtc.html — the functionality was not even documented nor officially supported then.
For these operating systems, it is recommended to use localtime.
UTC in Ubuntu
Ubuntu and its derivatives have the hardware clock set to be interpreted as in «localtime» if Windows was detected on any disk during Ubuntu installation. This is apparently done deliberately to allow new Linux users to try out Ubuntu on their Windows computers without editing the registry.
For changing this behavior, see above.
Time zone
To check the current zone defined for the system:
To list available zones:
To set your time zone:
This will create an /etc/localtime symlink that points to a zoneinfo file under /usr/share/zoneinfo/ . In case you choose to create the link manually (for example during chroot where timedatectl will not work), keep in mind that it must be a symbolic link, as specified in archlinux(7) [dead link 2021-02-08] :
Setting based on geolocation
To set the timezone automatically based on the IP address location, one can use a geolocation API to retrieve the timezone, for example curl https://ipapi.co/timezone , and pass the output to timedatectl set-timezone for automatic setting. Some geo-IP APIs that provide free or partly free services are listed below:
Update timezone every time NetworkManager connects to a network
Alternatively, the tool tzupdate AUR automatically sets the timezone based on the geolocation of the IP address. This comparison of the most popular IP geolocation apis may be helpful in deciding which API to use in production.
Time skew
Every clock has a value that differs from real time (the best representation of which being International Atomic Time); no clock is perfect. A quartz-based electronic clock keeps imperfect time, but maintains a consistent inaccuracy. This base ‘inaccuracy’ is known as ‘time skew’ or ‘time drift’.
When the hardware clock is set with hwclock , a new drift value is calculated in seconds per day. The drift value is calculated by using the difference between the new value set and the hardware clock value just before the set, taking into account the value of the previous drift value and the last time the hardware clock was set. The new drift value and the time when the clock was set is written to the file /etc/adjtime overwriting the previous values. The hardware clock can therefore be adjusted for drift when the command hwclock —adjust is run; this also occurs on shutdown but only if the hwclock daemon is enabled, hence for Arch systems which use systemd, this does not happen.
If the hardware clock keeps losing or gaining time in large increments, it is possible that an invalid drift has been recorded (but only applicable, if the hwclock daemon is running). This can happen if you have set the hardware clock time incorrectly or your time standard is not synchronized with a Windows or macOS install. The drift value can be removed by first removing the file /etc/adjtime , then setting the correct hardware clock and system clock time. You should then check if your time standard is correct.
The software clock is very accurate but like most clocks is not perfectly accurate and will drift as well. Though rarely, the system clock can lose accuracy if the kernel skips interrupts. There are some tools to improve software clock accuracy:
Time synchronization
The Network Time Protocol (NTP) is a protocol for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks. The following are implementations of NTP available for Arch Linux:
- Chrony — A client and server that is roaming friendly and designed specifically for systems that are not online all the time.
https://chrony.tuxfamily.org/ || chrony
- ConnMan — A lightweight network manager with NTP support.
https://01.org/connman (waybackmachine) || connman
- Network Time Protocol daemon — The reference implementation of the protocol, especially recommended to be used on time servers. It can also adjust the interrupt frequency and the number of ticks per second to decrease system clock drift, and will cause the hardware clock to be re-synchronised every 11 minutes.
https://www.ntp.org/ || ntp
- ntpclient — A simple command-line NTP client.
http://doolittle.icarus.com/ntpclient/ || ntpclientAUR
- NTPsec — A fork of NTPd, focused on security.
https://ntpsec.org/ || ntpsecAUR
- OpenNTPD — Part of the OpenBSD project and implements both a client and a server.
https://www.openntpd.org/ || openntpd
- sntp — An SNTP client that comes with NTPd. It supersedes ntpdate and is recommended in non-server environments.
https://www.ntp.org/ || ntp
- systemd-timesyncd — A simple SNTP daemon that only implements a client side, focusing only on querying time from one remote server. It should be more than appropriate for most installations.
https://www.freedesktop.org/wiki/Software/systemd/ || systemd
Per-user/session or temporary settings
For some use cases it may be useful to change the time settings without touching the global system values. For example to test applications relying on the time during development or adjusting the system time zone when logging into a server remotely from another zone.
To make an application «see» a different date/time than the system one, you can use the faketime(1) utility (from libfaketime ).
If instead you want an application to «see» a different time zone than the system one, set the TZ environment variable, for example:
This is different than just setting the time, as for example it allows to test the behavior of a program with positive or negative UTC offset values, or the effects of DST changes when developing on systems in a non-DST time zone.
Another use case is having different time zones set for different users of the same system: this can be accomplished by setting the TZ variable in the shell’s configuration file, see Environment variables#Defining variables.
Troubleshooting
Clock shows a value that is neither UTC nor local time
This might be caused by a number of reasons. For example, if your hardware clock is running on local time, but timedatectl is set to assume it is in UTC, the result would be that your timezone’s offset to UTC effectively gets applied twice, resulting in wrong values for your local time and UTC.
To force your clock to the correct time, and to also write the correct UTC to your hardware clock, follow these steps:
- Setup ntpd (enabling it as a service is not necessary).
- Set your time zone correctly.
- Run ntpd -qg to manually synchronize your clock with the network, ignoring large deviations between local UTC and network UTC.
- Run hwclock —systohc to write the current software UTC time to the hardware clock.
Tips and tricks
fake-hwclock
alarm-fake-hwclock designed especially for system without battery backed up RTC, it includes a systemd service which on shutdown saves the current time and on startup restores the saved time, thus avoiding strange time travel errors.
Источник