How to configure the time zone on Linux

Having a correct time zone configured on your Linux data center servers could mean the difference between software running properly or not.

How to configure the time zone on Linux Having a correct time zone configured on your Linux data center servers could mean the difference between software running properly or not.

As an IT admin, you know the importance of time. But we're not talking about being on time for client appointments or for corporate meetings, where you have to justify every single penny you've spent on data center hardware. We're talking about actual time, as in the ticking and tocking of seconds that count off the moments until entropy sweeps us from this mortal coil.

Or maybe not something so bleak.

SEE: Hiring kit: Database administrator (TechRepublic Premium)

When referring to "time" in this instance, I'm actually speaking to the time on your servers. If you've ever run into a situation where you've seen server software refuse to work properly, one of the last things you might have checked is the time on the server running the service. Believe it or not, having the correct time on a server is crucial for certain applications and services. Without having time configured properly, you'll receive errors that give you absolutely no indication as to what is causing the problem.

And that is why it is so important for you to have configured your timezone properly on your Ubuntu Servers within the data center. Without that timezone properly configured, your servers will be out of sync and will complain in numerous ways.

Let's find out how to properly set the time zone on Linux. I'll be demonstrating on Ubuntu Server 18.04, but the process should work on nearly every distribution. I'm not only going to show you how to set the system time zone, but also the timezone for PHP (because anytime you use PHP, the time zone must also be configured correctly for that subsystem). 

System time zone

Your system time zone should have been set during the initial installation. However, that could have been skipped, your company could have moved, or someone might have accidentally changed the time zone. 

First, let's find out what time zone is set on the server. To do that, issue the command:

timedatectl status | grep "Time zone"

This should report the currently set time zone (Figure A).

tza.jpg

Figure A: My time zone is set incorrectly!

If you find your time zone not set correctly, fret not. To set the timezone, issue the command:

sudo dpkg-reconfigure tzdata

In the resulting window (Figure B), make sure to select the Geographic area first, and then the city or region.

tzb.jpg

Figure B: Setting the geographic area for the timezone.

Once you've selected the city or region, tab down to OK to set the time zone. Issue the command:

timedatectl status | grep "Time zone"

You should now see your time zone is set properly (Figure C).

tzc.jpg

Figure C: The server time zone is correct now.

PHP time zone

Chances are, you've installed PHP on your server. Why? Because so many server applications depend on it. If you don't configure the time zone correctly for PHP, you'll find those services unable to properly function. 

So how do you set the time zone for PHP? Let's assume you've installed PHP 7.2 on your server that runs Apache. That being the case, open the php.conf file for editing with the command:

sudo nano /etc/php/7.2/apache2/php.ini

Type the [Ctrl]+W key combination, type timezone, and hit Enter. You should then see the line:

date.timezone =

This line needs to be configured in the form:

date.timezone = GEOGRAPHIC_REGION/CITY

Where GEOGRAPHIC_REGION is your Country (or region) and CITY is the city in which you are located. In some instances, where you live in a city with the same name as cities in other states, you'll have to use the format GEOGRAPHIC_REGION/STATE/CITY. For example, if you're in Louisville, KY, the time zone line would be:

date.timezone = America/Kentucky/Louisville

Save and close that file.

PHP is now properly configured for your time zone. Restart Apache (so the change will take effect) with the command:

sudo systemctl restart apache2

Must do

You might not think setting the time zone on your data center servers is all that important. Think again. You'd be surprised at how crucial this setting is for so many applications and services. Make sure to go around to each of your Linux data center servers and check the time zone setting. If you find any of them not set properly, reconfigure them immediately.

Also see

linuxhero.jpg

Image: Jack Wallen