Although many desktop-enabled Ubuntu computers have a handy GUI configuration, this does not help when setting up headless instances (In other words, no screen) as servers. Find out how to set up a time zone from the terminal, using .bashrc.
What is it .bashrc?
the .bashrc
The file is a hidden Bash-shell-specific file that is located at the root of your home directory, In other words, its location is ~/.bashrc
. the ~
(tilde) is a shortcut to your home directory, which has a longer route, as an example /home/roel
. When you use ~
, Bash will automatically replace the tilde with your home directory (In other words, including username).
You can edit this file using a text editor like vim
O nano
. If you want more information about the use vim
, take a look at our post Set a Great Vim Profile Using .vimrc, which also described the vim
use.
If you have no experience with vim
Despite this, or if you are in a hurry, you can use the nano
editor instead. Just run nano ~/.bashrc
to start editing your .bashrc
. If you get an error that nano
not found in your system, just install the same using sudo apt install nano
.
the .bashrc
The file contains per-user system configuration implementations. In other words, if you want to preset something (as an example, a command alias or time zone) to make it available in your Bash terminal session each time you start a, the .bashrc
file is the place to do it!
Change the user's time zone in Bash
Changing the user's time zone in Bash is easy; simply configure the TZ
variable (exporting it) to the desired time zone. As an example:
export TZ=Australia/Perth
date
export TZ=Australia/Darwin
date
Note the time difference between the two locations, as well as the abbreviation of the different time zone.
For a list of time zones, just run timedatectl list-timezones | grep your_country
where your_country
is replaced by your real country, or the country you want to use for your configuration / shell time settings.
Armed with this information, now it will be easy to update our .bashrc
file to match our desired settings.
Setting a user time zone from .bashrc
To set a time zone from your ~/.bashrc
file, first open the file using a text editor (as described above), and then add the next line to the end of the file, changing the time zone to your preferred setting:
export TZ=Australia/Sydney
Now just log out of your current Bash session and reopen it. When you now run the date
command, you should find that your time zone has changed to the value exported to the TZ
variable in your ~/.bashrc
proceedings. You can also write timedatectl
without any option to see a more complete description. As an example, using the TZ
adjustment of Australia/Sydney
, we see:
Ending
In this post, we review how to set up a time zone in Ubuntu or Linux Mint using the .bashrc
proceedings. We also saw how to edit this file and what the file does. Being able to determine a time zone from the command line is especially useful when working on a headless system (In other words, no screen), as is the case with servers. Always enjoy the right time!