首页 > 代码库 > HowTo: Linux Server Change OR Setup The Timezone

HowTo: Linux Server Change OR Setup The Timezone

My Linux systems timezone is pointing to the wrong timezone. How do I setup or change the timezone under Linux operating systems?

Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds elapsed since midnight UTC on the morning of January 1, 1970, not counting leap seconds. The definition for time zones can be written in short form as UTC?±n (or GMT?±n), where n is the offset in hours. You can use the following commands:

[a] setup or redhat-config-date for RHEL based distros.

[b] dpkg-reconfigure tzdata for Debian based distros.

[c] ln command – Generic method for all other distros.

Command to change the Linux timezone

You need to type the following commands as per your Linux distribution.

If you are using Fedora / RHEL / Cent OS Linux

Type the redhat-config-date command at the command line to start the time and date properties tool, run:
# redhat-config-date
OR type setup and select time zone configuration. This tool is recommended for remote ssh text based sessions.
# setup
Select timezone configuration

技术分享Fig.01: Redhat / CentOS Server Setting Up Timezone
Just follow on screen instructions to change the timezone.

If you are using Debian / Ubuntu Linux

To change the timezone for you run the following command as root user:
# dpkg-reconfigure tzdata
Again, just follow on screen instructions.

Set timezone using /etc/localtime configuration file [any Linux distro]

Often /etc/localtime is a symlink to the file localtime or to the correct time zone file in the system time zone directory.

Generic procedure to change timezone under Linux

Cd to /etc, run:
# cd /etc
Create a symlink to file localtime:
# ln -sf /usr/share/zoneinfo/EST localtime
OR some distro use /usr/share/zoneinfo/dirname/zonefile format (Red hat and friends):
# ln -sf /usr/share/zoneinfo/EST localtime
OR if you want to set up it to IST (Asia/Calcutta):
# ln -sf /usr/share/zoneinfo/Asia/Calcutta localtime
Please note that in above example you need to use directory structure i.e. if you want to set the timezone to Calcutta (India) which is located in the Asia directory.

How do I verify new settings?

Use the date command to verify that your timezone is changed:
$ date
Output:

Tue Aug 27 14:46:08 EST 2006

How do I use of environment variable called TZ?

You can use TZ environment variable to display date and time according to your timezone:
$ export TZ=America/Los_Angeles
$ date

Sample Output:

Thu Aug 27 11:10:08 PST 2006
转自:https://www.cyberciti.biz/faq/howto-linux-unix-change-setup-timezone-tz-variable/

HowTo: Linux Server Change OR Setup The Timezone