Whilst many Desktop enabled Ubuntu computers have a handy GUI configuration cool, this does not help when configuring headless (i.e. screenless) instances like servers. Find out how to setup a timezone from the terminal, using .bashrc.
What Is .bashrc?
The .bashrc
file is a hidden Bash-shell specific file sitting in the root of you home directory, i.e. it’s location is ~/.bashrc
. The ~
(tilde) is a shortcut to your home directory, which has a longer path, like for example /home/roel
. When using ~
, Bash will automatically replace the tilde with your home directory (i.e. including the user name).
You can edit this file by using a text editor like vim
or nano
. If you would like to learn more about using vim
, have a look at our Define a Great Vim Profile Using .vimrc article which also described basic vim
usage.
If you are not experienced with vim
yet, or an in a hurry, you can use the nano
editor instead. Simply execute nano ~/.bashrc
to get started with editing your .bashrc
. If you get an error that nano
was not found on your system, simply install the same by using sudo apt install nano
.
The .bashrc
file contains per-user system configuration implementations. In other words, if you would like to pre-configure something (like for example, an command alias, or a timezone) to be available in your Bash terminal session every time you start one, the .bashrc
file is the place to do so!
Changing the User Timezone in Bash
Changing the user timezone in Bash is easy; simply set the TZ
variable (by exporting it) to your desired timezone. For example:
export TZ=Australia/Perth date export TZ=Australia/Darwin date
Note the difference in time between the two locations, as well as the different timezone abbreviation.
To get a list of time zones, simply execute timedatectl list-timezones | grep your_country
where your_country
is replaced by your actual country, or the country you want to use for your shell time setting/configuration.
Armed with this information, it will now be easy to update our .bashrc
file to match our desired setup.
Configuring a User Timezone from .bashrc
To setup a timezone from your ~/.bashrc
file, first open the file using a text editor (as described above), and subsequently add the following line to the end of the file, changing the timezone to your preferred setting:
export TZ=Australia/Sydney
Now simply exit your current Bash session and re-open it. When you now execute the date
command, you should find that your timezone has change to the value exported to the TZ
variable in your ~/.bashrc
file. You can also type timedatectl
without any options to see a fuller overview. For example, using the TZ
setting of Australia/Sydney
, we see:
Wrapping up
In this article, we reviewed how to setup a timezone in Ubuntu or Linux Mint by using the .bashrc
file. We also looked at how to edit this file and what the file does. Being able to set a timezone from the command line is especially handy when you are working on a headless (i.e. screenless) system, like is the case with servers. Enjoy the correct time, every time!