Setting timezone from terminal
«which will set to gmt» But is there a way to set time zone with just the offset ie.-1 or +5 etc? I need to do this as I’m writing an application to adjust timeoffset or report logs and the only info I have is the user IP. I can use a webapp to find the location of the IP, but then I need to set offset which would be easy IF I could just get the offset of the location, but if I need to find zone and city it would be a real pain. If anyone knows the answer to how to set system clock with +/-hour would be great.
Please help us to pick up the accepted answer. I think @Mitch’s answer is the best askubuntu.com/a/323163/22308
@NamGVU No, Mitch’s is not the best solution, it’s a «GUI in a terminal» answer. Even the OP said » this wont work as i need to altr timezone from a program without user input i need a command i can feed into terminal not gui solution», and that’s the same objective most would want when looking for a «terminal solution,» a script-friendly one. Collin Anderson’s is better if you know a city, or Ryan’s for plain GMT+-n
8 Answers 8
To change time zone from terminal, just press Ctrl + Alt + T on your keyboard to open Terminal. When it opens, run the command(s) below:
sudo dpkg-reconfigure tzdata
Once open, just follow the screens to change the time zone.
this wont work as i need to altr timezone from a program without user input i need a command i can feed into terminal not gui solution thanks for reply
thx. this helps, byt i’m already changed manually etc/timezone =) like php.net/manual/en/timezones.php . absolutely identical .
Reconfiguring tzdata seems to adjust the hardware clock too so that the displayed time stays the same after the time zone switch. You might want that if your time was correct but time zone was wrong. However you definitely don’t want to touch your hardware clock while traveling. The timedatectl approach seems to change time zone only.
You can also use the new timedatectl to set the time in 14.04.
sudo timedatectl set-timezone America/New_York
To see all available options, you can run ls -R —group-directories-first /usr/share/zoneinfo . (Be careful not to modify or erase any file here.) More info about posix and right prefixes is here.
I realize this thread is a bit dated, but I was looking for a better solution because I needed to automatically set the timezone in a VM after a user downloads it from our website and deploys it. Here’s what I ended up with:
echo "Setting TimeZone. " export tz=`wget -qO - http://geoip.ubuntu.com/lookup | sed -n -e 's/.*\(.*\).*/\1/p'` && timedatectl set-timezone $tz export tz=`timedatectl status| grep Timezone | awk ''` echo "TimeZone set to $tz"
This will query geoip.ubuntu.com from the server once it is started on the new network (my script checks for connectivity first course) and then set the server’s timezone based on the response.
The «wget -q0 -» tells wget to output only the results to stdout which is then piped to the $tz variable.
How to Configure Time Zone and System Clock in Linux
In this tutorial, we will look at how to change your time zone settings in Linux and how to use NTP to sync the Linux system clock to your time zone.
Check Time Zone Settings
Check the current time zone settings.
Fri May 20 13:36:53 UTC 2022
As you can see, my system time zone is set to UTC (Universal Time Coordinated). But I live in China right now. So I need to change time zone settings from UTC to CST (China Standard Time).
Change Time Zone on Debian-based Linux Distros
On Debian-based Linux distros, including Debian, Ubuntu, Linux Mint, Elementary OS, etc, you can change your time zone settings with this command.
sudo dpkg-reconfigure tzdata
It will open up a dialog in the terminal to let your select your geographic area.
Next, select the city or region corresponding to your time zone. I live in China, so I selected Shanghai.
After you hit the OK button, your system clock will be immediately changed to your time zone clock.
A Distro-Agnostic Way to Change Time Zone Settings
This method works on all Linux distributions. First, open a terminal and type this command:
sudo ln -sf /usr/share/zoneinfo/
Don’t press Enter yet, press Tab key instead.
A list of geographic areas will appear, now enter the name of your area after sudo ln -sf /usr/share/zoneinfo/. For example, I entered Asia. Then press Tab key again. A list of City will appear.
Enter the name of the city corresponding to your time zone. For instance, I entered Shanghai. Now press the space key and enter /etc/localtime. So the final command will be
sudo ln -sf /usr/share/zoneinfo/your-geographic-area/your-city /etc/localtime
Press enter to run this command. It will create a symbolic link pointing /etc/localtime file to your specific time zone file. Your system’s clock will be updated immediately, as you can find out by entering date command in the terminal. But users with a desktop environment such as XFCE need to log out and log back in to see the changes on the taskbar.
Real-Time Clock
RTC stands for real-time clock, also known as hardware clock. This clock is powered by CMOS battery of your computer’s motherboard. This clock runs all the time even if you shutdown your computer. Sometimes RTC time is incorrect.
To find out RTC time on your computer, run this command:
Local time: Fri 2022-05-20 21:42:26 +08 Universal time: Fri 2022-05-20 13:42:26 UTC RTC time: Fri 2022-05-20 13:42:26 Time zone: Asia/Singapore (+08, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
As you can see, my local time is now 21:42:26, but RTC time is 13:42:26. RTC time is not in my local time zone and sometimes this can cause problems if the RTC time is inaccurate. RTC time is unreliable.
To make your system read RTC time in UTC standard, execute the following command.
timedatectl set-local-rtc 0
To let your system read RTC time in the local time zone, run this command
timedatectl set-local-rtc 1
It’s recommended your system read RTC time in UTC standard to prevent unexpected behavior.
Synchronize System Clock to Your Time Zone
Sometimes, your system clock can still be incorrect even if you set the correct time zone. To make sure your system clock is synchronized to your local time zone, you need NTP (Network Time Protocol).
NTP’s job is to provide accurate time on your network and there are many services that rely on accurate time to function correctly. NTP synchronizes the clock across your network so that time is as accurate as possible. It’s an application layer protocol and it uses UDP as the transport layer protocol. Network time servers get their time from atomic clocks.
On systemd you can enable NTP synchronization using timedatectl .
sudo timedatectl set-ntp true
The above command will start and enable the systemd-timesyncd service. Now check the status.
Local time: Fri 2022-05-20 21:45:34 +08 Universal time: Fri 2022-05-20 13:45:34 UTC RTC time: Fri 2022-05-20 13:45:34 Time zone: Asia/Singapore (+08, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
You can see that system clock is synchronized and NTP service is enabled. To check the status of timesyncd :
sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2022-05-09 17:47:54 CST; 1h 37min ago Docs: man:systemd-timesyncd.service(8) Main PID: 2243 (systemd-timesyn) Status: "Synchronized to time server 202.112.29.82:123 (0.arch.pool.ntp.org)." Tasks: 2 (limit: 512) CGroup: /system.slice/systemd-timesyncd.service └─2243 /usr/lib/systemd/systemd-timesyncd
Chrony
If the system clock can’t be synced to an NTP server, you can also try chrony , which is a versatile implementation of the Network Time Protocol. Run the following command to install it. (Note: If your system has the systemd-timesyncd pacakge, chrony will remove it from your system.).
- Debian/Ubuntu: sudo apt install chrony
- CentOS/RHEL/Fedora: sudo dnf install chrony
- Arch Linux: sudo pacman -S chrony
- OpenSUSE: sudo zypper install chrony
After it’s installed, start it with:
sudo systemctl enable --now chrony
sudo systemctl status chrony
● chrony.service - chrony, an NTP client/server Loaded: loaded (/lib/systemd/system/chrony.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2022-04-21 23:40:55 CST; 4 weeks 0 days ago Docs: man:chronyd(8) man:chronyc(1) man:chrony.conf(5) Process: 11030 ExecStart=/usr/lib/systemd/scripts/chronyd-starter.sh $DAEMON_OPTS (code=exited, status=0/SUCCESS) Main PID: 11039 (chronyd) Tasks: 2 (limit: 3676) Memory: 1.4M CPU: 168ms CGroup: /system.slice/chrony.service ├─11039 /usr/sbin/chronyd -F 1 └─11040 /usr/sbin/chronyd -F 1 Apr 21 23:40:55 systemd[1]: Starting chrony, an NTP client/server. Apr 21 23:40:55 chronyd[11039]: chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 -DEBUG) Apr 21 23:40:55 chronyd[11039]: Using right/UTC timezone to obtain leap second data Apr 21 23:40:55 chronyd[11039]: Loaded seccomp filter (level 1) Apr 21 23:40:55 systemd[1]: Started chrony, an NTP client/server. Apr 21 23:41:12 chronyd[11039]: Selected source 38.229.62.9 (0.ubuntu.pool.ntp.org) Apr 21 23:41:12 chronyd[11039]: System clock wrong by 2496258.787751 seconds May 20 21:05:31 chronyd[11039]: System clock was stepped by 2496258.787751 seconds May 20 21:05:31 chronyd[11039]: System clock TAI offset set to 37 seconds
As you can see, chrony found the system clock is wrong and automatically sync it with the network time server (0.ubuntu.pool.ntp.org).