Setting the Date and Time
This topic describes how to set the system date, time, and time zone by using timedatectl, date, and hwclock commands.
Using the timedatectl Command
Displaying the Current Date and Time
To display the current date and time, run the following command:
timedatectl
Example command output:
$ timedatectl Local time: 2015-08-14 15:57:24 CST Universal time: 2015-08-14 07:57:24 UTC RTC time: 2015-08-14 07:57:24 Timezone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a
Changing the Current Time
To change the current time, run the following command as the root user:
timedatectl set-time HH:MM:SS
For example, to change the current time to 15:57:24 pm, run the following command as the root user:
# timedatectl set-time 15:57:24
Changing the Current Date
To change the current date, run the following command as the root user:
timedatectl set-time YYYY-MM-DD
For example, to change the current date to 14 August 2015, run the following command as the root user:
# timedatectl set-time '2015-08-14'
Changing the Time Zone
To list all available time zones, run the following command:
timedatectl list-timezones
To change the current time zone, run the following command as the root user:
timedatectl set-timezone time_zone
Imagine you want to identify which time zone is closest to your present location while you are in Asia. You can check that by listing all available time zones in Asia with the following command:
# timedatectl list-timezones | grep Asia Asia/Aden Asia/Almaty Asia/Amman Asia/Anadyr Asia/Aqtau Asia/Aqtobe Asia/Ashgabat Asia/Baghdad Asia/Bahrain …… Asia/Seoul Asia/Shanghai Asia/Singapore Asia/Srednekolymsk Asia/Taipei Asia/Tashkent Asia/Tbilisi Asia/Tehran Asia/Thimphu Asia/Tokyo
To change the time zone to shanghai, run the following command:
# timedatectl set-timezone Asia/Shanghai
Synchronizing the System Clock with a Remote Server
Your system clock can be automatically synchronized with a remote server using the Network Time Protocol (NTP). To enable or disable this feature, run the following command as the root user:
timedatectl set-ntp boolean
For example, to automatic synchronization of the system clock with a remote server, run the following command:
# timedatectl set-ntp yes
Using the date Command
Displaying the Current Date and Time
To display the current date and time, run the following command:
date
By default, the date command displays the local time. To display the time in Coordinated Universal Time (UTC), run the command with the --utc or -u command line option:
date --utc
You can also customize the format of the displayed information by providing the + "format" option on the command line:
date +"format"
Format Option |
Description |
---|---|
%H |
The hour in the HH format (for example, 17) |
%M |
The minute in the MM format (for example, 37) |
%S |
The second in the SS format (for example, 25) |
%d |
The day of the month in the DD format (for example, 15) |
%m |
The month in the MM format (for example, 07) |
%Y |
The year in the YYYY format (for example, 2015) |
%Z |
The time zone abbreviation (for example, CEST) |
%F |
The full date in the YYYY-MM-DD format (for example, 2015-7-15). This option is equal to %Y-%m-%d. |
%T |
The full time in the HH:MM:SS format (for example, 18:30:25). This option is equal to %H:%M:%S. |
Example commands and outputs:
- To display the current date and time:
$ date 2015 08 17 Monday 17:26:34 CST
- To display the current date and time in UTC:
$ date --utc 2015 08 17 Monday 09:26:18 UTC
- To customize the output of the date command:
$ date +"%Y-%m-%d %H:%M" 2015-08-17 17:24
Changing the Current Time
To change the current time, run the date command with the --set or -s option as the root user:
date --set HH:MM:SS
By default, the date command sets the local time. To set the system clock in UTC instead, run the command with the --utc or -u command line option:
date --set HH:MM:SS --utc
For example, to change the current time to 23:26:00 p.m, run the following command as the root user:
# date --set 23:26:00
Changing the Current Date
To change the current date, run the date command with the --set or -s option as the root user:
date --set YYYY-MM-DD
For example, to change the current date to 2 November 2015, run the following command as the root user:
# date --set 2015-11-02
Using the hwclock Command
The hwclock command is used to set the real-time clock (RTC).
Real-Time Clock and System Clock
Linux distinguishes between the system clock and the real-time clock. The system clock is maintained by Linux kernel, whereas the real-time clock is an integrated clock on the system board and is battery-powered. The real-time clock is defined in the Standard BIOS Feature option of BIOS.
Once Linux is booted, the real-time clock reads system clock settings and then the system clock is completely independent of the real-time clock.
All commands in the system, including functions, are based on the system clock. The system clock does not conflict with the real-time clock, and the two can even be different. This is of little help to common users, but is greatly enjoyed by Linux network administrators. Imagine a network covers multiple time zones and files on server A in time zone A need to be synchronized to server B in time zone B. The network administrator can merely change the system clock on server B to time zone A without changing the real-time clock on that server. After the file synchronization is completed, the administrator can change the system clock on server B back to time zone B.
Displaying the Current Date and Time
To display the current RTC date and time, run the following command as the root user:
hwclock
Example command output:
# hwclock
2015-08-17 Monday 14:34:42. -0.094973 second
Setting the Date and Time
To change the RTC date and time, run the following command as the root user:
hwclock --set --date "dd mmm yyyy HH:MM"
For example, to change the RTC time to 21:17, 21 October 2015, run the following command as the root user:
# hwclock --set --date "21 Oct 2015 21:17" --utc