Knowledgebase
Sarail Net > Support Ticket > Knowledgebase

Search help:


How to set time,date timezone settings on CentOS from Command Line

Solution

How to set time, date Timezone in Linux from Command Line

Here's what you need to know when you want to change the timezone on a CentOS or Red Hat Enterprise Linux server.

1. Change your timezone

# mv /etc/localtime /etc/localtime.backup
# ln -s /usr/share/zoneinfo/America/Chihuahua/etc/localtime

You need to substitute the lines in bold with your actual current timezone. That will change the timezone in the current session and should prevail upon reboots. To be absolutely certain, edit the file /etc/sysconfig/clock as well. That's the file that will be read on boot the determine the timezone.

# cat /etc/sysconfig/clock
ZONE="America/Chihuahua"
UTC=true
ARC=false

Same story: replace the zone-info with your actual zone.

Most of your running applications will read the timezone information upon startup, so if you change this you will have to restart your applications if you want them to see the new timezone.

2. Change your current time on the server

If your clock is off on the server, first check if the timezone (explained above) is correct. If it is, you should doublecheck your NTP (Network Time Protocol) daemon settings.

First, check if the ntpd is installed.

# rpm -qa | grep ntp
ntp-4.2.2p1-9.el5.centos.2.1

If that doesn't list anything, install ntpd first.

# yum install ntp

And check if it's started automatically upon boot.

# chkconfig --list ntpd
ntpd            0:off   1:off   2:off   3:off   4:off   5:off   6:off

If that's off on runlevel 3, edit it so it will start when you reboot the system.

# chkconfig ntpd on
# chkconfig --list ntpd
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

To start with, update the time manually  by calling the ntp daemon, then start the service so it updates automatically.

# ntpdate be.pool.ntp.org
8 Nov 16:58:12 ntpdate[3635]: step time server 193.110.251.51 offset 1.045684 sec
# /etc/init.d/ntpd start
Starting ntpd:                                             [  OK  ]

If you receive the following error when running ntpdate:

# ntpdate be.pool.ntp.org
8 Nov 16:58:05 ntpdate[3547]: the NTP socket is in use, exiting

Then stop the ntpd first, then try the above steps again (/etc/init.d/ntpd stop).

Once you're certain the clock is correct, you can sync the settings back to your hardware clock.

# hwclock --systohc
# hwclock --show

 

That should be it to set your clock correctly, assuming your firewall allows NTP connections to remote servers. If you're running on OpenVZ or Virtuozzo, ask your administrator to change the clock for you as that is controlled by your host.

 
Was this article helpful? yes / no
Related articles How to Install Apache, MySQL, and PHP on CentOS 6.10
Article details
Article ID: 12
Category: Knowledgebase
Rating (Votes): Article rated 3.4/5.0 (15)

 
« Go back

 
Powered by Help Desk Software HESK, in partnership with SysAid Technologies