How to change timezone in Linux server (RedHat, CentOS, Ubuntu)


This article is beginners guide to change timezone of Linux server. Timezone is crucial for server since server’s native time is displayed according to timezone.
Server’s current timezone can be checked using simple date command
In above output, UTC (Coordinated Universal Time) is your server’s timezone. If you want to change it to match your region or application’s user’s timezone there are three ways you can do it.
If you want to change timezone without much hassle and without messing around system files directly jump to datetimectl command option below.

How to change timezone in Linux using /etc/localtime file

/etc/localtime is the file read by kernel to determine timezone. If you check your current file, its a soft linkto respective timezone file in /usr/share/zoneinfo directory.
Since our test server’s timezone is UTC, you can see /etc/localtime is soft link to /usr/share/zoneinfo/UTC. To change timezone delete existing /etc/localtime file and create new one with link to desired timezone file.
You can see we have changed timezone to EST by linking new localtime file to appropriate zone file under /usr/share/zoneinfo

How to change timezone using shell variable TZ

Another way to change timezone is to use shell variable TZ. You can do it with single line command as below :
I exported timezone variable TZ for Singapore timezone. Check date command output. Timezone has been set to +08 which means UTC+08:00 i.e. SGT (Singapore Timezone)
But remember this timezone setting will be available for current shell only from where you are exporting this TZ variable.

How to change timezone using /etc/timezone file

Third way to change timezone is use of /etc/timezone file. This is available in some distro like Ubuntu. Content of file will be timezone name you want to set.
You have to export timezone variable TZ as well along with change in /etc/timezone file. And then you can see timezone change in affect.

Change timezone using timedatectl command

You can use timedatectl command to safely change timezone when you dont want to deal with system files and fear of messing them up.
timedatectl command used to display current timezone, list available timezones and change timezone on server. We have this dedicated article on timedatectl to learn this command in detail.
To change timezone of server, use timedatectl with set-timezone argument and its value.
In the output, you can see we have changed timezone of server to WAT timezone.
SHARE

sangeethakumar

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment