RHEL7 and CentOS7 Time Settings (date, hwclock) - System Management (2)

Keywords: Mobile Unix Attribute

RHEL7 and CentOS7 provide three command line modes to set and display dates and times. Time datectl is part of the newly added system D in RHEL7 and CentOS7. Date is the traditional date and time setting command. The hwclock unit accesses the hardware clock.

I. Time datectl

[root@Geeklp-Administrator ~]# timedatectl
      Local time: Six 2017-12-16 19:49:53 CST
  Universal time: Six 2017-12-16 11:49:53 UTC
        RTC time: Six 2017-12-16 11:43:53
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

The default is to turn on your NTP time synchronization. It will not take effect immediately after modifying the time settings of NTP or chrony. You need to restart the timedatectl service.

[root@Geeklp-Administrator ~]# systemctl restart systemd-timedated.service

(1) Modify the current time
timedatectl set-time HH:MM:SS
This command updates the system time and hardware clock at the same time, and the result is similar to the date set and hwclock systohc commands. Turning on NTP time synchronization is not able to use this command to set the time, which needs to be noted.

[root@Geeklp-Administrator ~]# timedatectl set-time 13:00:27
Failed to set time: Automatic time synchronization is enabled
[root@Geeklp-Administrator ~]# timedatectl set-ntp no
[root@Geeklp-Administrator ~]# timedatectl set-time 13:00:27
[root@Geeklp-Administrator ~]# timedatectl 
      Local time: Six 2017-12-16 13:01:12 CST
  Universal time: Six 2017-12-16 05:01:12 UTC
        RTC time: Six 2017-12-16 05:01:13
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

The default time of the system is UTC, and the system time is local time.

timedatectl set-local-rtc boolean

The boolean value is yes (or y, true, t, 1). When boolean value is no, UTC time is used. At this time, the value is no (or n, false, f, 0), and the default value is No.

[root@Geeklp-Administrator ~]# timedatectl set-local-rtc no

If set to yes, there may be a warning. It is not recommended to change to yes here.
(2) Modify or set the time zone.
Enumerate all available time zones and type the following commands. Of course, you can also enter tzselect to set it according to boot (recommendation).

timedatectl list-timezones

When the root user types the following commands to modify the currently used area. For example, we changed our district time to Hong Kong.

[root@Geeklp-Administrator ~]# timedatectl set-timezone Asia/Hong_Kong
[root@Geeklp-Administrator ~]# timedatectl 
      Local time: Six 2017-12-16 16:43:12 HKT
  Universal time: Six 2017-12-16 08:43:12 UTC
        RTC time: Six 2017-12-16 08:43:11
       Time zone: Asia/Hong_Kong (HKT, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

(3) If you use remote NTP time server to provide time, you need to use the timedatectl settings to synchronize the NTP time.

[root@Geeklp-Administrator ~]# timedatectl set-ntp yes
[root@Geeklp-Administrator ~]# timedatectl 
      Local time: Six 2017-12-16 16:46:15 HKT
  Universal time: Six 2017-12-16 08:46:15 UTC
        RTC time: Six 2017-12-16 08:46:15
       Time zone: Asia/Hong_Kong (HKT, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

Two, date

(1) Display the current date and time

[root@Geeklp-Administrator ~]# date
2017 Monday, 18 December 15:45:10 CST

(2) By default, date displays local time. If you need to display UTC time, type the following command:

[root@Geeklp-Administrator ~]# date --utc
2017 Monday, 18 December 07:32:43 UTC

(3) We can also control the display information by + format.

[root@Geeklp-Administrator ~]# date +"%H"
16
[root@Geeklp-Administrator ~]# date +"%M"
23
[root@Geeklp-Administrator ~]# date +"%D"
12/18/17
[root@Geeklp-Administrator ~]# date +"%Y"
2017
[root@Geeklp-Administrator ~]# date +"%F"
2017-12-18
[root@Geeklp-Administrator ~]# date +"%T"
16:23:31
[root@Geeklp-Administrator ~]# date +"%d"
18
[root@Geeklp-Administrator ~]# date +"%Y-%m-%d %H:%M"
2017-12-18 16:26

Some available options are detailed in the following table:

Control character describe
%H Hours (for example, 17).
%M Minutes (for example, 30).
%S Second (for example, 24).
%d One day in the month (for example, 16).
%m A month in a month (for example, 09).
%Y Year (for example, 2016).
%F Complete date format (for example, 2016-09-16). This option corresponds to:% Y-%m-%d.
%T Complete time format (for example, 17:30:24). This option corresponds to% H:%M:%S

(4) Modify the current time by the date command. You can use the date - set or date -s commands. The option --- UTC or - u can be used to specify whether the time is UTC.

[root@Geeklp-Administrator ~]# date --set 12:43:23
2017year 12month 18Sunday, Monday 12:43:23 CST
[root@Geeklp-Administrator ~]# date --set 2017-12-19
2017year 12month 19Sunday, Tuesday 00:00:00 CST
[root@Geeklp-Administrator ~]# date --set '2017-12-17 12:43:23'
2017year 12month 17Sunday and Sunday 12:43:23 CST
[root@Geeklp-Administrator ~]# date --set '2017-12-17 12:43:23' -u
2017year 12month 17Sunday and Sunday 12:43:23 UTC
[root@Geeklp-Administrator ~]# date --set '2017-12-17 12:43:23' --utc
2017year 12month 17Sunday and Sunday 12:43:23 UTC
[root@Geeklp-Administrator ~]# date --s '2017-12-17 12:43:23' --utc
2017year 12month 17Sunday and Sunday 12:43:23 UTC
[root@Geeklp-Administrator ~]# date -s '2017-12-17 12:43:23' --utc
2017year 12month 17Sunday and Sunday 12:43:23 UTC

It should be noted that quotation marks should be used when setting the complete date and time.

3. hwclock

Hardware clocks, as the name implies, are the clocks stored on the server motherboard. The values stored in the hardware clock include year, month, day, hour, minute and second. It does not store time standard, local time, UTC, DST. These settings are stored in / etc/adjtime, which is created when we modify the time settings, including manual settings and automatic synchronization. In RHEL6 or CentOS6 systems, this command is automatically executed every time the system shuts down or restarts. In RHEL7 or CentOS7, when the system time is synchronized to NTP server or PTP server, the kernel automatically synchronizes the hardware clock to the system clock every 11 minutes.
(1) Display hardware clock.

[root@Geeklp-Administrator ~]# hwclock 
2017year12month18Sunday, Monday 16time39branch40second  -0.110050 second
[root@Geeklp-Administrator ~]# hwclock --show
2017year12month18Sunday, Monday 16time39branch57second  -0.293885 second

(2) Synchronize the hardware clock to the system clock.

[root@Geeklp-Administrator ~]# hwclock -s
[root@Geeklp-Administrator ~]# hwclock --hctosys
[root@Geeklp-Administrator ~]# hwclock 
2017year12month18Sunday, Monday 17time17branch39second  -0.953704 second

The effect of-s and-hctosys is the same.
(3) Write system time into hardware clock.

[root@Geeklp-Administrator ~]# hwclock -w
[root@Geeklp-Administrator ~]# hwclock 
2017year12month18Sunday, Monday 17time02branch16second  -0.646804 second
[root@Geeklp-Administrator ~]# hwclock --systohc
[root@Geeklp-Administrator ~]# date
2017year 12month 18Sunday, Monday 17:14:08 CST
[root@Geeklp-Administrator ~]# hwclock 
2017year12month18Sunday, Monday 17time14branch17second  -0.285800 second

(4) Setting hardware clock manually. Use the command: hwclock - set - date.

[root@Geeklp-Administrator ~]# hwclock -s --date "12 oct 2013 13:01"
[root@Geeklp-Administrator ~]# hwclock 
2017 Monday, 18 December, 17:09:37  -0.047061 second
[root@Geeklp-Administrator ~]# date
2017 Monday, 18 December 17:09:44 CST
[root@Geeklp-Administrator ~]# hwclock --set --date "2015-11-23 21:17:23" --utc
[root@Geeklp-Administrator ~]# hwclock 
2015 Monday, November 23, 2001, 21:18:53  -0.208850 second
[root@Geeklp-Administrator ~]# date 
2017 Monday, 18 December 17:12:54 CST

IV. ON TIME ZONE

1,UTC:
Universal Time Coordinated coordinates world time, also known as world standard time. Most of the schedules in both places are expressed by GMT, but some of them can not see the word GMT on their schedules. Instead, the three letters UTC appear. What exactly is UTC? In fact, UTC refers to the Coordinated Universal Time - the time of world coordination (also known as the world standard time, the world unified time), which is the time of the average solar time (GMT), the new time scale revised by the geodesic axis motion and the international atomic time in seconds. The calculation process is quite precise, so if we take the "world time" as the unit, the calculation process is rather precise. UTC is more accurate than GMT in terms of bounded standard time. The error value must be kept within 0.9 seconds. If the error is greater than 0.9 seconds, leap seconds will be issued by the International Bureau of Earth Rotation in Paris to make UTC consistent with the Earth rotation cycle. So basically the essence of UTC emphasizes the more precise world time standard than GMT, but for the current statement, there is no difference between the function and accuracy of GMT and UTC.
2,GMT:
Greenwich Mean Time Greenwich Mean Time
UTC and GMT are the same as when they were in London, UK, so UTC and GMT are not different in programs. Meaning UTC=GMT is equal
In the seventeenth century, the Royal Observatory of Greenwich made astronomical observations for the expansion of maritime hegemony. The Old Royal Observatory was officially established in 1675, and in 1884 it was decided to adopt the noon line of Greenwich as the longitude zero for dividing the eastern and western hemispheres of the Earth. There is a 24-hour clock on the wall at the entrance of the Observatory, which shows the current time. Globally, the time set here is the reference point of world time. The time set by the whole world is based on Greenwich Time. This is the origin of the well-known "Greenwich Mean Time" (G.M.T.), which is marked on the watch and replaced by the time set by Greenwich Mean Time. This table has the function of time in two places, that is, it can show the time of one place of origin and another country at the same time.
3. Unix timestamp:
The UTC time seen in the computer is calculated in seconds (0:00:00 on January 01, 1970). The UTC time you see is how many seconds from this point in 1970 to the specific time. This number of seconds is the Unix timestamp.
4. CST time:
CST can represent four different time zones at the same time:
Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30
China Standard Time UT+8:00
Cuba Standard Time UT-4:00
5,CET
Central European Time (CET) Central European Time (CET) is one of the time zone names one hour earlier than the World Standard Time (UTC). It has been adopted by most European countries and some North African countries. UTC+1 in winter and UTC+2 in summer.
6,DST:
Daylight Saving Time
In summer, when the sun rises earlier, the clock is set one hour faster to use light earlier. In Britain, it is called Summer Time. The idea was put forward by Benjamin Franklin in 1784. In 1915, Germany became the first country to officially implement daylight saving in order to reduce lighting and electricity consumption. Since then, about 70 countries around the world, mainly in Europe and North America, have cited this approach. India, currently divided into two time zones, is also discussing whether it should unify daylight saving across the country. There are also many base stations of GSM system on European mobile phones, which not only transmit local time, but also save time in daylight. As the time standard of mobile phones, users can decide whether to turn on or off. It is worth noting that some countries have a system of "saving time in summer". When you go abroad, don't forget to follow the local customs and adjust the table. This is a functional design that mechanical meters do not have.
7. Relations:
UTC=GMT
CET=UTC/GMT + 1 hour
CST=UTC/GMT +8 hours
CST=CET+9
Reference material:
http://www.jinbuguo.com/systemd/timedatectl.html#list-timezones
https://www.cnblogs.com/zhi-leaf/p/6282301.html

Posted by jdh on Fri, 14 Dec 2018 21:06:04 -0800