Set Linux Time, Date, and Timezone

Time_Date_TimezoneIn a previous article, we discussed configuring NTP on your server.  NTP is the obviously the first choice for setting your server’s date and time.  The best part about using NTP is that the continues to be updated over time so your machine never strays far from the real time.  However, sometimes, you don’t have the luxury of having your machine on a network where you can use NTP, or perhaps you just haven’t had an opportunity to configure it yet.  If that is the case, you can still set the date and time of your machine manually.

Configure Your Linux Server’s Time Manually

Setting the linux time manually is as easy as using the date command.  Simply using the following syntax:

date MMDDhhmmYYYY.ss

where:

MM = Month (05 – May, etc…)

DD = Date (05 – 5th day of the month)

hh = 24 hour clock hour

mm = minute

YYYY = 4 digit year

ss = seconds

for example:

# date 050510302014.00

will set the date / time to May 5, 2014 10:30:00am


Set Linux Date Only

Let’s imagine that the time on your Linux server is just fine, but you want to configure the date only.  Well, you can do that too:

# date --set="20140505"

will set the Linux system time to May 5, 2014 where 20140505 equals YYYYMMDD.

Set Linux Time Only

What if the machine date is fine, but you really want to manually set the Linux system time only.

date +%T -s "10:30:00"

will set the Linux system clock to 10:30:00am where “10:30:00” is using the format:  “HH:MM:SS”

Check the Server’s Hardware Clock

The server hardware that your operating system is running on has it’s own clock which is called the “hardware clock”.  Sometimes you want to see how far off the system’s hardware clock is from the Linux time.  We can use the hwclock command to show the current hardware clock time as well as to show the difference between the hardware clock and the linux “system clock”

# hwclock --show
Sat May 10 21:02:07 2014  -0.875386 seconds

hwclock –show in the examle above is showing us that the hardware clock and the system clock are off nearly a full second.

Change Your Machine’s Hardware Clock

We can set our hardware clock to match our linux system clock’s time using the hwclock –systohc command:

# hwclock --systohc

Conversely, we can set the linux system clock to the hardware system clock’s time using the –hctosys option.

# hwclock --hctosys


Set your Timezone Environment Variable

Machines with users from different timezones means that each user may want to set the timezone for their respective shell environment.  You can do this by setting the TZ environment variable in your shell as shown below:

TZ='America/Denver'; export TZ

This will set our timezone in our shell to be Mountain Time.  You can put this in your .profile or bash_profile file.

Use Tzselect to Find Timezone

If you are unsure what the exact text you should use to set your time zone, you can use the tzselect command to learn what you should be using.  To install tzselect, you actually need to install the tzdata package which is part of the EPEL repo that we have described in other articles.

# yum install tzdata-2013b-1.el6.noarch

Now that tzselect is installed, let’s run it:

# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 2

Please select a country.
 1) Anguilla                           28) Haiti
 2) Antigua & Barbuda                  29) Honduras
 3) Argentina                          30) Jamaica
 4) Aruba                              31) Martinique
 5) Bahamas                            32) Mexico
 6) Barbados                           33) Montserrat
 7) Belize                             34) Nicaragua
 8) Bolivia                            35) Panama
 9) Bonaire Sint Eustatius & Saba      36) Paraguay
10) Brazil                             37) Peru
11) Canada                             38) Puerto Rico
12) Cayman Islands                     39) Sint Maarten
13) Chile                              40) St Barthelemy
14) Colombia                           41) St Kitts & Nevis
15) Costa Rica                         42) St Lucia
16) Cuba                               43) St Martin (French part)
17) Curacao                            44) St Pierre & Miquelon
18) Dominica                           45) St Vincent
19) Dominican Republic                 46) Suriname
20) Ecuador                            47) Trinidad & Tobago
21) El Salvador                        48) Turks & Caicos Is
22) French Guiana                      49) United States
23) Greenland                          50) Uruguay
24) Grenada                            51) Venezuela
25) Guadeloupe                         52) Virgin Islands (UK)
26) Guatemala                          53) Virgin Islands (US)
27) Guyana
#? 49

Please select one of the following time zone regions.
 1) Eastern Time
 2) Eastern Time - Michigan - most locations
 3) Eastern Time - Kentucky - Louisville area
 4) Eastern Time - Kentucky - Wayne County
 5) Eastern Time - Indiana - most locations
 6) Eastern Time - Indiana - Daviess, Dubois, Knox & Martin Counties
 7) Eastern Time - Indiana - Pulaski County
 8) Eastern Time - Indiana - Crawford County
 9) Eastern Time - Indiana - Pike County
10) Eastern Time - Indiana - Switzerland County
11) Central Time
12) Central Time - Indiana - Perry County
13) Central Time - Indiana - Starke County
14) Central Time - Michigan - Dickinson, Gogebic, Iron & Menominee Counties
15) Central Time - North Dakota - Oliver County
16) Central Time - North Dakota - Morton County (except Mandan area)
17) Central Time - North Dakota - Mercer County
18) Mountain Time
19) Mountain Time - south Idaho & east Oregon
20) Mountain Time - Navajo
21) Mountain Standard Time - Arizona
22) Pacific Time
23) Alaska Time
24) Alaska Time - Alaska panhandle
25) Alaska Time - southeast Alaska panhandle
26) Alaska Time - Alaska panhandle neck
27) Alaska Time - west Alaska
28) Aleutian Islands
29) Metlakatla Time - Annette Island
30) Hawaii
#? 18

The following information has been given:
United States
Mountain Time
Therefore TZ='America/Denver' will be used.
Local time is now:Sat May 10 19:53:38 MDT 2014.
Universal Time is now:Sun May 11 01:53:38 UTC 2014.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line

TZ='America/Denver'; export TZ

to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
America/Denver

Tzselect gave me the text “America/Denver” to set for my TZ variable. So I need to set it to set my timezone to Mountain Time (Denver / USA):

$ TZ=America/Denver; export TZ

I can set this in the .profile file or the bash_history to set it permanently.

You may be interested in setting the timezone for the machine itself and not just for your session on the machine. To do this, you need to modify the /etc/localtime file:

Find the Timezone File for Your Timezone

First, we find the timezone file that we want to use:

# ls /usr/share/zoneinfo/
Africa      Canada   GB         Indian     Mexico    ROC        Zulu
America     Chile    GB-Eire    Iran       Mideast   ROK        iso3166.tab
Antarctica  Cuba     GMT        Israel     NZ        Singapore  posix
Arctic      EET      GMT+0      Jamaica    NZ-CHAT   Turkey     posixrules
Asia        EST      GMT-0      Japan      Navajo    UCT        right
Atlantic    EST5EDT  GMT0       Kwajalein  PRC       US         zone.tab
Australia   Egypt    Greenwich  Libya      PST8PDT   UTC
Brazil      Eire     HST        MET        Pacific   Universal
CET         Etc      Hongkong   MST        Poland    W-SU
CST6CDT     Europe   Iceland    MST7MDT    Portugal  WET

In each of these Geographical region directories, there are specific timezone files. Choose the one listed from the tzselect command above, then replace the /etc/localtime file with it:

# rm /etc/localtime; ln -s /usr/share/zoneinfo/America/Denver /etc/localtime

This has set our Linux System timezone to America/Denver permanently.

 

CentOS 7

Many things have changed in CentOS 7, one of many new features is the timedatectl comand.  In CentOS 7, you can easily view what your timezone is set to by simply running the timedatectl command in your shell.  Timedatectl is an excellent command as it tells you everything that you need to understand about your machines time setup.

Example:  Check out our server’s time and timezone and all about Daylight Savings Time using the timedatectl command.

# timedatectl
      Local time: Mon 2014-12-29 15:40:04 MST
  Universal time: Mon 2014-12-29 22:40:04 UTC
        Timezone: n/a (MST, -0700)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2014-11-02 01:59:59 MDT
                  Sun 2014-11-02 01:00:00 MST
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2015-03-08 01:59:59 MST
                  Sun 2015-03-08 03:00:00 MDT

 

Now, that we know the server is configured for UTC, and we want to change the timezone to our local timezone, we can use timedatectl again, this time with the “set-timezone” option.

Example:   Setting the server’s timezone using the timedatectl command

# timedatectl set-timezone America/Denver

Now, we can run the timedatectl command again and see that the timezone has changed.

# timedatectl
 Local time: Mon 2014-12-29 15:40:21 MST
 Universal time: Mon 2014-12-29 22:40:21 UTC
 Timezone: America/Denver (MST, -0700)
 NTP enabled: yes
 NTP synchronized: yes
 RTC in local TZ: no
 DST active: no
 Last DST change: DST ended at
 Sun 2014-11-02 01:59:59 MDT
 Sun 2014-11-02 01:00:00 MST
 Next DST change: DST begins (the clock jumps one hour forward) at
 Sun 2015-03-08 01:59:59 MST
 Sun 2015-03-08 03:00:00 MDT

Other options for the timedatectl command are the “status” option which shows the current time settings, the “set-time” option which helps you adjust the server’s clock, and the set-ntp command which allows you to configure you NTP server.

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: http://uptimemadeeasy.com. You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *