CentOS 6 Linux Server Cookbook
上QQ阅读APP看书,第一时间看更新

Changing the time zone and updating the hardware clock

In this recipe we will introduce the concept of TZDATA in order that we can learn how to change a server's time zone and update the hardware clock.

Over the lifetime of your server, the need to change the time and date for one or more users may not happen very often but when it does the process of managing this modification can seem to be quite complicated. You may want to make this change for any number of reasons, you can even use this approach to implement an independent time zone solution for different users, but regardless as to the reason why, it is the purpose of this recipe to illustrate a series of best practices that can be applied to your server and provide you with the solution you need.

Getting ready

To complete this recipe, you will require a working installation of the CentOS 6 operating system with root privileges, a console-based text editor of your choice, and a connection to the Internet to facilitate the download of additional packages.

How to do it...

TZDATA is a compilation of the world's time zone information and we will start this recipe by showing you how to use the tzselect utility and applying the new time zone setting to a user of your choice.

  1. To begin, log in as root and type the following command in order to install the necessary tzdata package:
    yum install tzdata
    
  2. Now start the tzselect utility by typing:
    tzselect
    

    Note

    Remember, you can press Ctrl + C to interrupt the tzselect utility at any time.

  3. Having initiated the tzselect utility, you will be asked a series of questions that begins as follows:

    Please identify a location so that time zone rules can be set correctly.

  4. From the values shown on screen, choose a numeric value to confirm your selection and press the Return key to proceed.
  5. You will now be asked the following:

    Please select a country.

  6. This question refers to the preferred time zone, so in a similar manner, choose a numeric value to make your selection and press the Return key to proceed. Based on the information provided, the utility will then respond by asking you to confirm the final details. As an example, for a user located in London (UK), the response will be as follows:

    The following information has been given:

    Britain (UK)

    Therefore TZ='Europe/London' will be used.

    Local time is now:Wed May 30 22:21:20 BST 2012.

    Universal Time is now:Wed May 30 21:21:20 UTC 2012.

    Is the above information OK?

    1) Yes

    2) No

  7. Again, choose a numeric value to confirm your settings and press the Return key to proceed. By completing this action, you will now finalize the purpose of the tzselect utility, and it will respond in the following way:

    You can make this change permanent for yourself by appending the line

    TZ='Europe/London'; export TZ

    to the file '.profile' in your home directory; then log out and log in again.

    Here is that TZ value again, this time on standard output so that you

    can use the /usr/bin/tzselect command in shell scripts:

    Europe/London

  8. With reference to the preceding example shown, return to your console and make a note of the following value:
    TZ='XXXXXX/XXXXXXX'; export TZ 
    

    Note

    For example, if the TZ value given was Los Angeles (USA), then you would record: TZ='America/Los_Angeles'; export TZ. However, if the TZ value given was London (UK), then you would record: TZ='Europe/London'; export TZ.

  9. Now open the appropriate user profile with your favorite text editor as follows:
    vi /home/username/.profile
    

    Note

    If you cannot find this file, or this file does not exist, as an alternative you can use /home/username/.bash_profile.

  10. Scroll to the bottom of the file and substitute the TZ values shown with those values you obtained from the tzselect utility like this:
    TZ0
    'Europe/Moscow'; export TZ
    

    Note

    On the whole, most people will use the location-based method to confirm any preferred time zone settings, but there may be an occasion when you would prefer to use the Posix Time Format. If this is the case, then simply modify the previous command by substituting the following value, POSIX-format-here with a relative value, as follows:

    TZ='POSIX-fomat-here'; export TZ

    The value chosen should be a number based on the difference in time between you current location and GMT such as GMT-5.

  11. When finished, save and close your file. The relevant user can now re-login and use the date command to confirm their new time zone settings at any time:
    date
    
  12. Repeat these given steps for any remaining users on the server.

How it works...

As we have seen, the tzselect utility can be used to tell you the time (anywhere in the world) by simply looking at your system clock and comparing it with Coordinated Universal Time (UTC). Its purpose is not to complete the task of updating your server's time zone, but it does provide the necessary information that will facilitate such changes for one or more users.

So what have we learned from this experience?

Having started by installing the relevant package, you were then guided to start the tzselect utility and use the supplied TZ value to update a user's profile.

Updating a user's profile was simply a matter of amending the appropriate file for each user. In most cases you would update the file found at /home/username/.profile. However, if this file was not available, then an alternative option located at /home/username/.bash_profile can be used.

For example, a revised bash profile will look like this:

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
TZ='Europe/Moscow'; export TZ

You were also shown how to use the Posix Time Format, and if you did indeed decide to use this variation, then your revised bash profile will look like this:

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
TZ='GMT-5'; export TZ

Remember, in the context of this recipe, the measurement of time is session based, so having made the preceding modifications, a user would then be required to re-login before they will notice any changes. They can do this by typing the following command:

date

Moreover, it is important to note that a change to a specific profile will not affect another user and none of these changes will affect the server as a whole. So with this in mind, this recipe can be used to enable multiple users to maintain different clocks.

So having completed this recipe, we can say that we now know how to obtain the required TZ value and provide a unique time zone setting for each user. As previously stated, it may not be a feature that you use on a regular basis, but it does provide a flexible solution in as much that you are now able you to configure a user account independently of the system as a whole and deliver a fully localized server environment regardless of how many users you serve or where they are located.

There's more...

Setting the time for your users is only one aspect of managing time on a server and it should not be forgotten that your server will run two clocks—a hardware-based clock that is supported by the battery on the motherboard and a secondary clock that is maintained by the operating system. Where the former is used to set the system clock during the boot process, the latter will be the clock that is used to keep a track of time.

Both of these time values can be run independently of each other (local time versus UTC) and on occasion this may even prove to be useful when considering a dual-boot situation. However, and in the case of most servers, it is common practice to ensure that both the hardware clock and system clock use the same reference in order to account for daylight.

As the root user you can use the following syntax to set a new date and time for your server's operating system by simply replacing MMDDhhmmYYYY.ss (Month, Day, Hour, Minute, Year, and seconds) with the correct values:

date MMDDhhmmYYYY.ss

For example, to change the time and date value to May 17, 13:21:22, 2012, you can use the following syntax:

date 051713212012.22

Note

As an alternative approach you can simplify this procedure by changing these values independently. You can change the date by typing:

date --set="YYYYMMDD"

You can change the time by typing:

date +%T -s "HH:MM:SS"

When ready, you should now synchronize the system with your hardware clock by typing:

hwclock–systohc

Finally, reboot your server to finalize the changes made:

reboot

Note

You should be aware that changing time values on a regular basis can affect other aspects of your server and as a result, you may need to ensure that any date and time-based service or application is not affected inadvertently.

On a successful reboot, you can confirm the status of your hardware clock with the following command:

hwclock–show

Similarly you can view the current operating system date and time by typing the following command:

date

Linking time and location

As many of the applications and features running on your server will use the current clock to determine the correct time, having completed the preceding steps it is often a good idea to ensure if the correct time zone information is linked to your server's local time settings.

To do this simply, log in as root, and remove the old location values by typing:

rm /etc/localtime

Confirm the request and then by referring to the list of files located in /usr/share/zoneinfo, create the following symbolic link by replacing XXX with a value more representative of your location:

ln -sf /usr/share/zoneinfo/XXX /etc/localtime

Note

For example, if you were to type, ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime, this would be correct for servers located in or near London, while the command ln -sf/usr/share/zoneinfo/Europe/Moscow /etc/localtime would be correct for servers located in or around Moscow.

Finally, you should reboot your server to allow any changes to take immediate effect:

reboot