Changing the timezone on your Ubuntu server is a crucial task for ensuring accurate timekeeping, especially when dealing with scheduled tasks, logs, and applications that rely on correct timestamps. This guide provides a detailed walkthrough of how to change the timezone on your Ubuntu server.
Step 1: Check the Current Timezone
Before making any changes, it’s essential to know the current timezone setting on your server. This will help you confirm the change later.
ahmed@server:~# date
Thu Jan 29 02:38:55 EST 2015
ahmed@server:~# more /etc/timezone
US/Eastern
The date
command displays the current date and time, including the timezone abbreviation (EST in this example). The more /etc/timezone
command shows the timezone identifier (US/Eastern).
Step 2: Change Timezone using dpkg-reconfigure
The recommended way to change the timezone on Ubuntu is using the dpkg-reconfigure tzdata
command. This command provides an interactive interface for selecting your desired timezone.
ahmed@server:~# dpkg-reconfigure tzdata
This command will launch a text-based interface.
- Geographic Area: First, you’ll be prompted to select your geographic area (e.g., Asia, Europe, America). Use the arrow keys to navigate and press Enter to select.
- Time Zone: Next, you’ll be presented with a list of time zones within the selected geographic area. Choose the one that corresponds to your desired location (e.g., Kolkata for India Standard Time). Use the arrow keys to navigate and press Enter to select.
After you select the timezone, the system will update the necessary configuration files. You’ll see output similar to this:
Current default time zone: 'Asia/Kolkata'
Local time is now: Thu Jan 29 13:09:54 IST 2015.
Universal Time is now: Thu Jan 29 07:39:54 UTC 2015.
Verify the change by running the date
command again:
ahmed@server:~# date
Thu Jan 29 13:09:57 IST 2015
ahmed@server:~#
The output should now reflect the new timezone (IST in this example).
Step 3: Restart Services Dependent on Time
Changing the timezone doesn’t automatically update all services that rely on the system time. It’s crucial to restart these services to ensure they use the correct timezone. Common services to restart include cron
(for scheduled tasks) and web servers like nginx
or apache2
.
ahmed@server:~# service crond restart
ahmed@server:~# service nginx restart
Alternative using systemctl
: On more recent versions of Ubuntu (using systemd), you can use systemctl
to restart services:
ahmed@server:~# systemctl restart cron
ahmed@server:~# systemctl restart nginx
Why Restart Cron? cron
uses the system’s timezone to schedule tasks. If you don’t restart it after changing the timezone, your cron jobs might run at unexpected times.
Step 4: Verify the Change (Again!)
After restarting the services, it’s a good practice to double-check that everything is working as expected. You can do this by:
- Checking Logs: Examine the logs of the services you restarted (e.g.,
/var/log/syslog
,/var/log/nginx/error.log
) to see if the timestamps are correct. - Running a Test Cron Job: Create a simple cron job that writes the current date and time to a file, and then check the file to verify the timezone.
Additional Notes:
- NTP (Network Time Protocol): Consider using NTP to keep your server’s time synchronized with a reliable time source. This will help prevent time drift and ensure accuracy. You can install and configure NTP using
apt-get install ntp
. - GUI Tools: While
dpkg-reconfigure
is the standard command-line method, some desktop environments provide graphical tools for managing time and timezone settings. However, these are generally not applicable to headless servers. - UTC: Many servers are configured to use UTC (Coordinated Universal Time) to avoid timezone-related issues. If you choose to use UTC, you’ll need to ensure that your applications are configured to handle timezone conversions appropriately.
By following these steps, you can confidently change the timezone on your Ubuntu server and ensure accurate timekeeping for your applications and services. Remember to restart relevant services to apply the changes fully.