Linux: установка даты, времени и временной зоны

Автор: | 28/09/2013

linux_logoВ дополнение к статье FreeBSD: изменение даты и времени – выполним аналогичные действия на Linux.

В примере используется операционная система:

# cat /etc/redhat-release
CentOS release 6.4 (Final)

Текущее время:

# date
Sat Sep 28 11:49:05 MSK 2013

Время неверное, так как установлено по MSK, вместо Киева.

Что бы изменить время – выполняем команду date с ключём -s (--set) с указанием формата %T (--time):

# date +%T -s "10:50:30"
10:50:30

Посмотрим результат:

# date
Sat Sep 28 10:50:35 MSK 2013

Всё верно. Если же надо изменить изменить и дату – пропускаем опцию %T:

# date -s  "28 SEP 2013 10:51:00"
Sat Sep 28 10:51:00 MSK 2013

Можно упростить операцию, ограничившись вводом только даты. Для этого используются ключи указания формата %Y, %m и %d (Y – год, m – месяц, d – день):

# date +%Y%m%d -s "20130928"
20130928

Больше параметров доступно в man date.

Перейдём к установке временной зоны.

Файлы описания зон хранятся в каталоге /usr/share/zoneinfo/. К примеру, MSK описывается в файле:

# file /usr/share/zoneinfo/Europe/Moscow
/usr/share/zoneinfo/Europe/Moscow: timezone data, version 2, 13 gmt time flags

Соответственно Киев – в файле:

# file /usr/share/zoneinfo/Europe/Kiev
/usr/share/zoneinfo/Europe/Kiev: timezone data, version 2, 12 gmt time flags

Локальная зона указывается в файле /etc/localtime:

# file /etc/localtime
/etc/localtime: timezone data, version 2, 13 gmt time flags, 13 std time flags

Что бы установить новую зону – выполняем:

# rm -rf /etc/localtime
# ln -s /usr/share/zoneinfo/Europe/Kiev /etc/localtime

Проверяем:

# date
Sat Sep 28 11:10:03 EEST 2013

При необходимости – установите корректное время.

Другой вариант – с помощью утилиты tzselect (Time Zone select):

# 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.
#? 8
Please select a country.
1) Aaland Islands        18) Greece                35) Norway
2) Albania               19) Guernsey              36) Poland
3) Andorra               20) Hungary               37) Portugal
4) Austria               21) Ireland               38) Romania
5) Belarus               22) Isle of Man           39) Russia
6) Belgium               23) Italy                 40) San Marino
7) Bosnia & Herzegovina  24) Jersey                41) Serbia
8) Britain (UK)          25) Latvia                42) Slovakia
9) Bulgaria              26) Liechtenstein         43) Slovenia
10) Croatia               27) Lithuania             44) Spain
11) Czech Republic        28) Luxembourg            45) Sweden
12) Denmark               29) Macedonia             46) Switzerland
13) Estonia               30) Malta                 47) Turkey
14) Finland               31) Moldova               48) Ukraine
15) France                32) Monaco                49) Vatican City
16) Germany               33) Montenegro
17) Gibraltar             34) Netherlands
#? 48
Please select one of the following time zone regions.
1) most locations
2) Ruthenia
3) Zaporozh'ye, E Lugansk / Zaporizhia, E Luhansk
4) central Crimea
#? 1

The following information has been given:

Ukraine
most locations

Therefore TZ='Europe/Kiev' will be used.
Local time is now:      Fri Sep 27 23:10:36 EEST 2013.
Universal Time is now:  Fri Sep 27 20:10:36 UTC 2013.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
TZ='Europe/Kiev'; 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:
Europe/Kiev