How to change date, time, timezone on centos 6

When I was tweaking one of my linux server, I found out that the time of my server isn’t match with my local time. It’s hard to read logs with different time zone from your server, so I decided to change the timezone of my server to the my current timezone. I believe it was set with EST time. It would be easier for me to manage my server if it has the same time and date. I will show you how to change date and time on centos 6 operating system in this article. This article will properly work with other Linux distributions like Ubuntu, Linux Mint, Fedora or Redhat since the way Linux date, time, and timezone work all the same in Linux server.

There are a few different way to change date, time, and timezone on centos 6 via the command line terminal. Linux system normally keep their timezone in /etc/localtime. To check or show the current date, and time of your Linux system, you can use date command. Date command can print, or set date and time of your linux system.

Display Date and Time in Linux

# date
and its output

 

Change Timezone in Linux

Fist, we are going to get a list of timezone that came with Linux. # cd /usr/share/zoneinfo/ # ls

There are countries, or continent with different timezone, for example when you list all file and directories in zoneinfo directory as I did above, there are timezone for countries with only one timezone, and directories for countries and continent with several timezones. For example my timezone is America Pacific, I will need to go to America directory to get my timezone.

# cd /usr/share/zoneinfo/America/# ls

I will use Toronto for Pacific time, so I will need to copy/set Toronto file as my localtime on my server (this will work with any linux system I believe).

# cp /usr/share/zoneinfo/America/Toronto /etc/localtime

Change Date and Time in Linux

Now your Linux system should have the timezone that you want. If the date or time is not correct, you can set date and time with date command.

# date MMDDhhmmYYYY

  • MM: two digit month number
  • DD: two digit date
  • hh: two digit hour (24 hour system)
  • mm: two digit minute
  • YYYY: four digit of year

You can also sync your system time and date with ntp (Network Time Protocol) over a network or internet. Normally ntp should be installed to your Linux system as default, in case your system does not have ntp, you can easily install ntp package with a simple command.

To install ntp on Ubuntu, Linux or debian based distribitions
# apt-get update# apt-get install ntp

To install ntp on Fedora, Centos or redhat based distribitions
# yum install ntp

After you set your timezone correcly, you should be able to synce your date and time with ntp command
# ntpdate pool.ntp.org

Note: if you have a VPS (virtual private server) and not a real dedicated server, you will properly won’t be able to adjust time on your VPS. When you are on VPS, you do not have the right to modify or update time and date, only the VPS owner or provider have the rights to adjust time and date on that VPS node.

You will properly get one of this error if you are on a VPS.

  • Can’t adjust the time of day: Operation not permitted
  • rdate: could not set system time: Operation not permitted
  • ntpdate: Can’t adjust the time of day: Operation not permitted
這篇文章有幫助嗎? 0 Users Found This Useful (3 Votes)