The date, time, and timezone are important aspects of a Linux system that affect how it operates and interacts with other systems. There are different ways to describe and change these settings in Linux, depending on the distribution and the tools available. Below you will find information on how to display the current date, manually change the system date and time, and set timezones in Linux using the command line and these simple commands.
Display Date in Linux
You can use the date command to display the current date and time:
Using Menu-Based Time Zone Selection
tzselect is a Linux utility that allows users to select a timezone from a list of regions and cities. The utility outputs the timezone description to standard output, which can be used as a value for the TZ environment variable. tzselect does not change the system timezone permanently, it only affects the current shell session. To change the system timezone permanently, other methods such as timedatectl or symlinking can be used, although at the end of this prompt you will see the method used with tzselect to make the change persistent.
Begin by running the following command:
For this example, we select 2 for Americas. Next, select the country.
For this example we will select 10 for Canada.
Then the next screen will display the selection made along with checking whether the information is correct.
Once we confirm the selection you can view the details changed below. This screen also provides information on how to keep the change persistent by appending a line to the .profile file in your home directory.
Changing Time Zone Using timedatectl
timedatectl is a command-line utility that allows you to view and change the system time, date, and timezone. It can also synchronize the system clock with a remote server using the Network Time Protocol (NTP). timedatectl is part of the Systemd system and service manager.
To view all available time zones, use the following command:
Since the list is long, you can also use timedatectl list-timezones | grep EST to search for the value EST. Of course, you can change EST to any other zone you’d like to find, for example Paris, as shown below.
Next, use the following command format: sudo timedatectl set-timezone Region/Location, while changing the Region/Location to your desired result.
For example: Africa/Freetown
Manually Setting a Date and Time in Linux
To manually set a new date and time in Linux, use the following syntax:
date -s=”Date and time information” as shown below.
date -s “2 June 2023 14:05:00”
Changing a Timezone in Linux
The /etc/localtime is a file that configures the system-wide timezone of the local system that is used by applications. It should be an absolute or relative symbolic link pointing to /usr/share/zoneinfo/, followed by a timezone identifier such as “Europe/Berlin” or “Etc/UTC“.
/usr/share/zoneinfo is a directory that contains files with information about timezones. The files are named after each time zone, such as “America/New_York” or “Europe/London“. These files contain information about the timezone rules and daylight saving time (DST) settings for that timezone.
The contents of /etc/localtime can be found below.
The contents of /usr/share/zoneinfo can be found below.
To set the timezone of your choice, use the example below which uses Calcutta (IST) for the local time on the system, and replace it with the timezone of your choice.
*NOTE: Creating this link will create a link named “localtime” in whichever directory you are running this command. Any other utility to change the timezone used after will not work as this link will take precedence over anything else. To resolve this, you can remove the link/file “localtime” when it is required.
ln -sf /usr/share/zoneinfo/Asia/Calcutta localtime
- How to Check the Apache Version on a Linux Dedicated cPanel Server
- How to Change a Password in Linux
- How to Check if Your Linux Server is Under DDoS Attack
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.