Time is an important aspect in Linux systems especially in critical services such as cron jobs. Having the correct time on the server ensures that the server operates in a healthy environment that consists of distributed systems and maintains accuracy in the workplace.
In this tutorial, we will focus on how to set time/date/time zone and to synchronize the server clock with your Ubuntu Linux machine.
Check Current Time
You can verify the current time and date using the date
and the timedatectl
commands. These linux commands can be executed straight from the terminal as a regular user or as a superuser. The commands are handy usefulness of the two commands is seen when you want to correct a wrong time from the command line.
Using the date command
Log in as a root user and use the command as follows
$ date
Output
You can also use the same command to check a date 2 days ago
$ date --date="2 days ago"
Output
Using timedatectl command
Checking on the status of the time on your system as well as the present time settings, use the command timedatectl as shown
# timedatectl
or
# timedatectl status
Changing Time
We use the timedatectl
to change system time using the format HH:MM: SS. HH stands for the hour in 24-hour format, MM stands for minutes and SS for seconds.
Setting the time to 09:08:07 use the command as follows (using the timedatectl)
# timedatectl set-time 09:08:07
using date command
Changing time means all the system processes are running on the same clock putting the desktop and server at the same time. From the command line, use date command as follows
# date +%T -s "10:13:13"
Where,
• 10: Hour (hh)
• 13: Minute (mm)
• 13: Second (ss)
To change the locale to either AM or PM use the %p in the following format.
# date +%T%p -s "6:10:30AM"
# date +%T%p -s "12:10:30PM"
Change Date
Generally, you want your system date and time is set automatically. If for some reason you have to change it manually using date command, we can use this command :
# date --set=”20140125 09:17:00”
It will set your current date and time of your system into 'January 25, 2014' and '09:17:00 AM'. Please note, that you must have root privilege to do this.
You can use timedatectl to set the time and the date respectively. The accepted format is 'YYYY-MM-DD', 'YYYY' represents the year, 'MM' the month in two digits and 'DD' for the day in two digits.
Changing the date to '15 January 2019', you should use the following command:
# timedatectl set-time 20190115
Create custom date format
To create custom date format, use a plus sign (+)
$ date +”Day : %d Month : %m Year : %Y” Day: 05 Month: 12 Year: 2013
$ date +%D 12/05/13
%D format follows Year/Month/Day format.
You can also put the day name if you want. Here are some examples :
$ date +”%a %b %d %y” Fri 06 Dec 2013
$ date +”%A %B %d %Y” Friday December 06 2013
$ date +”%A %B %d %Y %T” Friday December 06 2013 00:30:37
$ date +”%A %B-%d-%Y %c” Friday December-06-2013 12:30:37 AM WIB
List/Change time zone
Changing the time zone is crucial when you want to ensure that everything synchronizes with the Network Time Protocol. The first thing to do is to list all the region’s time zones using the list-time zones option or grep to make the command easy to understand
# timedatectl list-timezones
The above command will present a scrollable format.
Recommended timezone for servers is UTC as it doesn't have daylight savings. If you know, the specific time zones set it using the name using the following command
# timedatectl set-timezone America/Los_Angeles
To display timezone execute
# timedatectl | grep "Time"
Set the Local-rtc
The Real-time clock (RTC) which is also referred to as the hardware clock is independent of the operating system and continues to run even when the server is shut down.
Use the following command
# timedatectl set-local-rtc 0
In addition, the following command for the local time
# timedatectl set-local-rtc 1
Check/Change CMOS Time
The computer CMOS battery will automatically synchronize time with system clock as long as the CMOS is working correctly.
Use the hwclock
command to check the CMOS date as follows
# hwclock
To synchronize the CMOS date with system date use the following format
# hwclock –systohc
Conclusion
To have the correct time for your Linux environment is critical because many operations depend on it. Such operations include logging events and cron jobs as well.
If you have any questions or feedback, feel free to leave a comment.
Read Also:
- Tuptime - Tool to Display Uptime History of Linux System
- How to Synchronize Time using NTP Server in Ubuntu
- How to Take Linux System Snapshots with Timeshift
The time and date has disappeared from my desktop. Normally, it appears to the upper far right hand side of my screen. Your instructions are confusing and I can't complete them to display the time and date automatically on start-up and through out my computer session. Help!
Article was focusing on setting date from command line. By the way which linux distro you use ? Was it working before ?
Is date command work from terminal ?
Doesn't work correctly on Debian. I think somehow the system is still using network time, even though I disabled it and can't find a running NTP client. I was trying to mess around with the date, but this happens:
sudo date -s "$( date --rfc-3339="seconds" | sed s/2016/2017/ )"; date; sleep 1; date
Thu 12 Jan 14:43:59 CET 2017
Thu 12 Jan 14:43:59 CET 2017
Tue 12 Jan 14:44:00 CET 2016
After a single second, the date gets corrected to the real date.
Great! Ty
Came in here to point out "corn job" at the end of the article.
Thanks for that. The typo is fixed now.
Ridiculous to have to use command line to make clock adjustments; I'll stay with a distro that allows it from the tray.