Synchronize Your Computer Time with NTP

An incorrectly set PC clock can be disastrous – if your computer loses track of the time, you could end up juggling files from the future or email from 30 years ago. The time warp could lead to misunderstandings, errors, or even crashes. 

Almost all Linux distributions set the time and time zone during the installation phase, and desktop environments such as KDE and Gnome display a clock in the panel to give users quick access to tools for configuring the computer clock (Figure 1).

Figure 1
Figure 1: Many GUI desktops let users right-click the clock in the panel to access the date and time settings.

 

In the shell, cal displays a simple but neatly formatted calendar. The date command gives you the date and time, although the output itself is fairly sparse. Additionally, this program can help the administrator set the date and time. The date tool also demonstrates its potential in combination with other command-line tools and in scripts, for example, when programs generate file names that contain the current date. 

The hwclock tool helps to synchronize the system time and the hardware clock. Of course, you will need to be root to run this program. 

If your machine has a permanent Internet connection, you can automate the process of setting the clock by synchronizing your own timekeeper with a server on the web via the Network Time Protocol (NTP).

Command-Line Calendar

If you call the calendar with cal and without parameters, the program displays the current month of the current year; the current day is highlighted. The -3 option tells cal to show you the previous and next months as well; the -y flag produces a year calendar (Figure 2).

Figure 2
Figure 2: With Cal, you can display a calendar for the whole year at the console.

 

To output a specific month, you need to pass the month to cal in the form of a two-digit number for the month and a four-digit number for the year. By default, cal will output the calendar in the language defined in the LANG environment variable.

If you prefer the time format for any other language, but would like to keep output from all other programs in the default language, you can set the LC_TIME variable to tell cal to use the language of your choice.

The following example sets the date and time output to English:

LC_TIME=C cal -y

Of course, this command isn’t necessary if your default language is already ­English.

What’s the Time?

If you type date at the command line, you will see the date, time, and also the time zone:

$ date
Tue Feb  19 15:23:41 CST 2019 

The date command also references the LANG variable to set the language, and it can also be ­influenced by setting LC_TIME just like cal.

Date is even more flexible if you set the TZ (time zone) variable with the command. Check your /usr/share/zoneinfo/ directory to find out which time zone values your computer supports with TZ

To find out the time in New York, for example, you simply run the following command:

$ TZ=America/New_York date
Tue Feb  19 16:27:24 EST 2019 

If you happen to live in Australia and need to phone friends in New York on a regular basis, you might want to set up an alias for the last command to make it easier to check the time before you call. 

To set up an alias, just add the following line to your Bash configuration file, ~/.bashrc

alias NY='TZ=America/New_York date'

and re-parse the settings after saving them by giving the source ~/.bashrc command. Then, you can simply type NY at the command line to output New York time.

Formatted Output

The date program has a large number of parameters that influence the output format. You can format the date output with a plus sign, followed by a percent sign, and a letter. For example:

$ date +%Y_%m
2019_02

Table 1 lists some of the more common options; you can refer to the man page (man date) for a complete list of the ­options. 

Table 1: Date Command-Line Parameters
Parameter Meaning

%M

Minutes (00 to 59)

%H

Hours, 24-hour clock

%I

Hours, 12-hour clock

%a

Weekday, short form

%A

Weekday, long form

%d

Day as two-digit number

%b

Name of month, short form

%B

Name of month, long form

%m

Month as two-digit number

%y

Year as two-digit number

%Y

Year as four-digit number

%D

Four-digit date (mm/​dd/​yy)

%T

Time in 24-hour clock 

%r

Time in 12-hour clock 

%t

Tabulator

%n

Line break

%%

% sign

 

 

 

These formatting options are particularly practical if you use date to generate file names made up of date, time, or both values automatically.

The command

tar -cvjf backup_$(date +%d_%m_%Y).tar.bz2 *

creates a Bzip2 compressed tarball with a name comprising the text string backup_, the date (that is the day, month, and year separated by underlines), and the file extension .tar.bz2 (for example, backup_05_11_2009.tar.bz2).

Setting the System Time

The root user can use date to set the time and date for a machine. To do so, use the -s option followed by a string that contains the new time (see the next section, “Everything is Relative”). Before you enter the following command, make sure that all NTP components have been uninstalled (see the “Automated” section):

# date -s "19 Feb 2019 16:20"
# date
Tue Feb 19 16:20:03 CST 2019

The first three parts of this are mandatory; if you leave out the year, date will just default to the current year. 

Other format options let you set the date with seconds' precision. For this information, enter man date and study the date string examples.

Everything Is Relative

As an alternative to the absolute date and time, the date tool also understands relative values and even has a couple of predefined strings to help you:

  • yesterday
  • tomorrow
  • today
  • now
  • sec(s)/second(s)
  • min(s)/minute(s)
  • hour(s)
  • day(s)
  • week(s)
  • fortnight
  • month(s)
  • year(s)

Additionally, date understands concepts such as ago, so you can say day ago instead of yesterday

If you use one of these strings to set the time, you must specify the -s para-meter like so:

# date -s '+3 mins'

To display a relative time, you need the -d parameter instead:

# date -d '+5 days -2 hours'
Sun Feb 24 13:14:18 CST 2019

The date information page tells you more about strings and how to use them. To read the documentation at the command line, use info coreutils date (see Figure 3).

Figure 3
Figure 3: To access documentation conveniently, use the info command at the command line. This information is the output of ‘info coreutils date’.

 

Setting the Hardware Clock

In addition to the software clock, your computer has another timekeeper, and this one will continue to count down the days when your computer is switched off and even when it is not plugged in.

To ensure uninterrupted timekeeping, computer mainboards have a battery-buffered clock, referred to as the CMOS clock, RTC (Real-Time Clock), BIOS clock, or even hardware clock.

The hwclock program lets you read and set the hardware clock; the commands all require root privileges. When used in combination with the -r ­option, you can display the local hardware time:

# hwclock -r
2019-02-19 15:44:09.49830-0500

Additionally, hwclock has options for setting the system time to reflect the hardware clock time (hwclock -s) or vice versa (hwclock -w).

A combination of --set and --date sets a specific time. You need to enter a string to describe the new date and time after the --date parameter. The format is exactly the same as the date program’s -s option. The command

# hwclock --set --date="+2 hours"

sets the hardware clock to a time two hours in the future.

Automated!

Network Time Protocol (NTP) is a standard for automating the synchronization of clocks in computer systems. The time signal propagates over the network from an NTP server to a client, and you can configure the point in time when your Linux machine’s NTP client contacts a server on the network. This could be at boot time or when you get onto the Internet, or you could use a manual command in the shell.

In the pre-Systemd era, most major Linux distributions had packages available for enabling NTP support. Many of those packages still exist – see the documentation for your own Linux distribution to learn about NTP package options.

Systemd provides a built-in systemd‑timesyncd service that performs basic time synchronization duties. To check whether the service is running on your system, enter:

systemctl status systemd‑timesyncd.service

The systemd‑timesyncd service is like other Systemd services. You can start, stop, or restart it using a variation of the systemctl command:

systemctl restart systemd‑timesyncd.service

Consult the systemctl man page for more on managing Systemd services.

This article originally appeared in the Linux Shell Handbook and is reprinted here with permission.

See also:
European Space Agency Considering Lunar Time Zone
Leap Seconds to be Discontinued in 2035
Network Time Keeps on Ticking with Long-Running NTP Project

Contact FOSSlife to learn about partnership and sponsorship opportunities.

woman with dark ponytail in front of computer

Comments