Knowing how to manage time via the command line is a useful skill to acquire. To best understand time, we need to understand the two types of clocks specified in modern operating systems:
- Real-time clock (RTC) – is aka hardware clock, which is essentially an IC in the motherboard. It does not depend on the OS in anyway such that, it runs when computer is shutdown and operates even when the computer doesn’t have an OS.
- System clock – is aka software clock. It is maintained by the kernel and it’s initial value is based on the RTC.
There are 3 tools in RHEL7/Centos 7 that can be used to manage date/time:
1. Use date command
This utility is available in all Linux based systems and allows you to configure date/time. Below are some few examples of the command in use.
- Display Current Date/Time Use date function with no options to display local time
[wm@serv0-wal ~]$ date
Tue Jan 22 23:37:16 EAT 2019
For UTC time. Use the command below
[wm@serv0-wal ~]$ date --utc
Tue Jan 22 20:38:56 UTC 2019
- Change date/time format The format of the date/time can be formatted using options to the date functions. For a list of possible options, access man page of date command. An example of this formatting is as below:
[wm@serv0-wal ~]$ date +"%Y-%m-%d %H:%M"
2019-01-22 23:43
- Change current time To change the current time, use date together with –set or -s options. Here are the examples
[wm@serv0-wal ~]$ date –set 23:21:12
[wm@serv0-wal ~]$ date -s 23:21:12 --utc
- Change current date To change the current date, use date together with –set or -s options too. Here are th examples
[wm@serv0-wal ~]$ date –set 2010-03-26 23:21:12
- Change current date To change the current date, use date together with –set or -s options too. Here are th examples
[wm@serv0-wal ~]$ date –set 2010-03-26 23:21:12
2. Use timedatectl command
This utility comes with systemd service manager. You can use this utility for a load of functions. For comprehensive information on timedatctl, please visit the man page of this utility. Some examples of the command in use are as follows:
- Display Current Date/Time Running the command below displays current date alongside other detailed information
[wm@serv0-wal ~]$ timedatectl
Local time:
Wed 2019-01-23 23:30:31 EAT
Universal time:
Wed 2019-01-23 20:30:31 UTC
RTC time:
Wed 2019-01-23 23:30:31
Time zone:
Africa/Nairobi (EAT, +0300)
NTP enabled:
yes
NTP synchronized: yes
RTC in local TZ:
yes
DST active:
n/a
Warning: The system is configured to read the
RTC time in the local time zone.
This mode
can not be fully supported. It will create various problems
with time
zone changes and daylight saving time adjustments. The RTC
time is
never updated, it relies on external facilities to maintain it.
If at all
possible, use RTC in UTC by calling
'timedatectl
set-local-rtc 0'.
- Change timezones List the available timezones using the command below.
[wm@serv0-wal ~]$ timedatectl list-timezones
Then issue the command below to set your system timezone as you’d like, replacing Africa/Nairobi with your prefered timezone
[wm@serv0-wal ~]$ timedatectl set-timezone Africa/Nairobi
- Change current time To change the current time, set-time argument as follows
[wm@serv0-wal ~]$ timedatectl set-time 23:36:24
- By default the system is configured in UTC. To make it maintain time in local time, run the command below, replacing bool with 1(or yes, y, true, t) or 0(or no, n, false,f). The default choice is no
[wm@serv0-wal ~]$ timedatectl set-local-rtc
bool
- Change current date To change the current date to say 23rd January 2019 11:43pmuse the command below:
[wm@serv0-wal ~]$ timedatectl set-time ‘2019-01-23 23:43:00’
3. Use hwclock command
This is a utility use dto read the real-time clock (RTC). The man page contains more information about the hwclock.
- Display current Date/Time To display current time
[wm@serv0-wal ~]$ hwclock
Wed 23 Jan 2019 11:50:11 PM EAT -1.000652 seconds
- Set date/time To set current date to 24th August 2019 at 11:43pm, use the command below
[wm@serv0-wal ~]$ hwclock –set –date “24 08 2019 23:43”