You are viewing an old revision of this post, from October 16, 2014 @ 11:01:33. See below for differences between this version and the current revision.

Change Timezone in CentOS

You just got your new CentOS dedicated server, and you notice that times in your logs aren’t quite right. You check the time from the command line (run date), and find that the timezone is set to US Eastern or some other timezone. How do you get this changed?

Unfortunately, this is not an easy thing to figure out. Fortunately though, it’s not hard to do with the right directions.

 

Please note that you have to have root access to be able to make the changes detailed below.

There are a series of time zone files located at /usr/share/zoneinfo. Select the appropriate named timezone for your location. For my location, Oklahoma, USA, I actually have two that I can select from: America/Chicago or US/Central. Make note of the appropriate folder and file for your timezone.

The active timezone used on your system is in the /etc/localtime file. The default will vary depending on your server host but often seems to be EST or EDT (depending on the time of year you are checking). We simply need to replace this file with the file we selected in the previous step.

Now, I say replace, but I actually recommend creating a link to the pertinent file rather than actually making a copy. Let me explain the reasoning for this quickly.

A crucial part of the timezone shift calculations is the daylight savings calculations. Many people don’t realize this, but the daylight savings days were changed in 2007. When this change happened, all servers needed to be updated with new zoneinfo files.

If your server has an automated process to update these files when daylight savings calculations change, your/usr/share/zoneinfo files will be updated but the /etc/localtime file will not be. So, if you simply made a copy of the file, you’ll have to know when these updates come out and manually copy the file over. If you create a link, everything will take care of itself.

Enough of the banter. On with the show.

First, make a backup of the existing localtime file. It’s always good practice to make backups of original config files.

[gaarai@server ~]$ sudo mv /etc/localtime /etc/localtime.bak

Next, create the link:

[gaarai@server ~]$ sudo ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime

Make sure to replace America/Chicago with the directory (if your zone has one) and filename of the timezone you wish to use.

Now you just need to test your change. Run date from the command line, and ensure that the appropriate time, date, and timezone are reported.

Revisions

Revision Differences

October 16, 2014 @ 11:01:33Current Revision
Content
Unchanged: <p>Unchanged: <p>
Unchanged: You just got your new CentOS dedicated server, and you notice that times in your logs aren&rsquo;t quite right. You check the time from the command line (run&nbsp;<code> date</code>), and find that the timezone is set to US Eastern or some other timezone. How do you get this changed?Unchanged: You just got your new CentOS dedicated server, and you notice that times in your logs aren&rsquo;t quite right. You check the time from the command line (run&nbsp;<code> date</code>), and find that the timezone is set to US Eastern or some other timezone. How do you get this changed?
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Unfortunately, this is not an easy thing to figure out. Fortunately though, it&rsquo;s not hard to do with the right directions.Unchanged: Unfortunately, this is not an easy thing to figure out. Fortunately though, it&rsquo;s not hard to do with the right directions.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: <em style="margin: 1.5em 0px;padding: 0px;border: 0px;vertical-align: baseline;background: transparent">Please note that you have to have root access to be able to make the changes detailed below.</em>Unchanged: <em style="margin: 1.5em 0px;padding: 0px;border: 0px;vertical-align: baseline;background: transparent">Please note that you have to have root access to be able to make the changes detailed below.</em>
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: There are a series of time zone files located at&nbsp;<code> /usr/share/zoneinfo</code>. Select the appropriate named timezone for your location. For my location, Oklahoma, USA, I actually have two that I can select from:&nbsp;<code> America/Chicago< /code>&nbsp; or&nbsp;<code> US/Central</code>. Make note of the appropriate folder and file for your timezone.Unchanged: There are a series of time zone files located at&nbsp;<code> /usr/share/zoneinfo</code>. Select the appropriate named timezone for your location. For my location, Oklahoma, USA, I actually have two that I can select from:&nbsp;<code> America/Chicago< /code>&nbsp; or&nbsp;<code> US/Central</code>. Make note of the appropriate folder and file for your timezone.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: The active timezone used on your system is in the&nbsp;<code> /etc/localtime< /code>&nbsp;file. The default will vary depending on your server host but often seems to be EST or EDT (depending on the time of year you are checking). We simply need to replace this file with the file we selected in the previous step.Unchanged: The active timezone used on your system is in the&nbsp;<code> /etc/localtime< /code>&nbsp;file. The default will vary depending on your server host but often seems to be EST or EDT (depending on the time of year you are checking). We simply need to replace this file with the file we selected in the previous step.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Now, I say replace, but I actually recommend creating a link to the pertinent file rather than actually making a copy. Let me explain the reasoning for this quickly.Unchanged: Now, I say replace, but I actually recommend creating a link to the pertinent file rather than actually making a copy. Let me explain the reasoning for this quickly.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: A crucial part of the timezone shift calculations is the daylight savings calculations. Many people don&rsquo;t realize this, but the daylight savings days were changed in 2007. When this change happened, all servers needed to be updated with new zoneinfo files.Unchanged: A crucial part of the timezone shift calculations is the daylight savings calculations. Many people don&rsquo;t realize this, but the daylight savings days were changed in 2007. When this change happened, all servers needed to be updated with new zoneinfo files.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: If your server has an automated process to update these files when daylight savings calculations change, your<code>/usr/ share/zoneinfo< /code>&nbsp;files will be updated but the&nbsp;<code> /etc/localtime< /code>&nbsp;file will not be. So, if you simply made a copy of the file, you&rsquo;ll have to know when these updates come out and manually copy the file over. If you create a link, everything will take care of itself.Unchanged: If your server has an automated process to update these files when daylight savings calculations change, your<code>/usr/ share/zoneinfo< /code>&nbsp;files will be updated but the&nbsp;<code> /etc/localtime< /code>&nbsp;file will not be. So, if you simply made a copy of the file, you&rsquo;ll have to know when these updates come out and manually copy the file over. If you create a link, everything will take care of itself.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Enough of the banter. On with the show.Unchanged: Enough of the banter. On with the show.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: First, make a backup of the existing localtime file. It&rsquo;s always good practice to make backups of original config files.Unchanged: First, make a backup of the existing localtime file. It&rsquo;s always good practice to make backups of original config files.
Unchanged: </p>Unchanged: </p>
Unchanged: <pre class="terminal">Unchanged: <pre class="terminal">
Unchanged: <span class="regular- prompt">[<span class="user"> gaarai@server</span> <span class="path"> ~</span>]$</span> <span class="user-input">sudo mv /etc/localtime /etc/localtime.bak< /span></pre>Unchanged: <span class="regular- prompt">[<span class="user"> gaarai@server</span> <span class="path"> ~</span>]$</span> <span class="user-input">sudo mv /etc/localtime /etc/localtime.bak< /span></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: Next, create the link:Unchanged: Next, create the link:
Unchanged: </p>Unchanged: </p>
Unchanged: <pre class="terminal">Unchanged: <pre class="terminal">
Deleted: <span class="regular- prompt">[<span class="user"> gaarai@server</span> <span class="path"> ~</span>]$</span> <span class="user-input">sudo ln -fs /usr/share/zoneinfo/ <strong>America/ Chicago</strong> /etc/localtime< /span></pre> Added: <span class="regular- prompt">[<span class="user"> gaarai@server</span> <span class="path"> ~</span>]$</span> <span class="user-input">sudo ln -sf /usr/share/zoneinfo/ <strong>America/ Chicago</strong> /etc/localtime< /span></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: Make sure to replace&nbsp; <code>America/ Chicago</code>&nbsp;with the directory (if your zone has one) and filename of the timezone you wish to use.Unchanged: Make sure to replace&nbsp; <code>America/ Chicago</code>&nbsp;with the directory (if your zone has one) and filename of the timezone you wish to use.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: Now you just need to test your change. Run&nbsp;<code> date</code>&nbsp;from the command line, and ensure that the appropriate time, date, and timezone are reported.Unchanged: Now you just need to test your change. Run&nbsp;<code> date</code>&nbsp;from the command line, and ensure that the appropriate time, date, and timezone are reported.
Unchanged: </p>Unchanged: </p>

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply