- How to change ubuntu’s server date and time via command line?
- 7 Answers 7
- How can I get the current date and time in the terminal and set a custom command in the terminal for it? [closed]
- Date Command in Linux: How to Set, Change, Format and Display Date
- Linux date Command Syntax
- How to Use date Command in Linux
- Linux date Command Format Options
- Set or Change Date in Linux
- Display Past Dates
- Display Future Dates
- Display the Date String at Line of File
- Display Last Modified Timestamp of a Date File
- Override a Time Zone
- Use date with Other Commands
- Use Unix Epoch Time (Epoch Converter)
- How to set the current time in Linux
- Method 1 — Using the timedatectl command
- Method 2 — Set Current Date and Time Using NTPD
- Method 3 — Setting System Date and Time using the date command
- Sync Hardware Clock with System Date
- Summary
How to change ubuntu’s server date and time via command line?
The Ubuntu server’s current date and time is different from the time zone date and time. I have tried using:
sudo date "30 Sep 2015 4:43:42"
to change it but it did not change the date and time, just printed on terminal the date and time I changed, but when I executed:
The date and time is still the old one. What is the correct way to change date and time of Ubuntu Server?
7 Answers 7
You can set the system date with this command:
sudo date --set="2015-09-30 10:05:59.990"
Then when using date , it should be showed correctly.
Now you should also the set hardware clock in the BIOS of the system, that the setting persists over a reboot (dureing the startup the system time is set to the value of the hardware clock). Do that with hwclock :
This gets the system clocks (sys) value and sets the hardware clock (hc). Check it with the hwclock command. Both hwclock and date should now show the same date and time.
To set your timezone, you can use this command:
sudo dpkg-reconfigure tzdata
BTW: If you use a this machine as a server, I strongly recommend using an NTP-Client to sync the time over network. So you can guarantee that all your servers have the exactly same time set. This will sync the time while the machine runs. If you have applications which are dependent of synced time over server, I recommend the NTP-Daemon. The longer it runs in the background, the more precise is the time.
How can I get the current date and time in the terminal and set a custom command in the terminal for it? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have to check the time in a Linux terminal. What is the command for getting date and time in a Linux terminal? Is there a way in which we can set a custom function?
This question seems on topic as it relates to a specific computing task on a specific operating system (which is just a software layer itself). As, @drstevens said, this is the first result in Google and it was informative for me needs. If it were off-topic, it would read something like «Do operating systems tell time?» or «What do you guys think about operating systems that tell time.» Stack Overflow is a great resource for beginners and it seems like this was closed as for being too beginner of a question. It should instead just be listed as a beginner question.
StackExchange is the place for answers. This is a good question. Perhaps the question should be migrated, but it should not be closed, in my opinion. This question appeared first in my Google results when I searched for «linux check time».
Date Command in Linux: How to Set, Change, Format and Display Date
Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.
Read on to learn how to use the date command in Linux.
- A system running Linux
- A user account with root privileges
- Access to a terminal window/command line
Linux date Command Syntax
The syntax for the date command is:
How to Use date Command in Linux
To show the current system time and date, type in the date command:
The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.
The -d option allows users to operate on a specific date. For example, we can type in the following command:
You can use the —date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:
Note: Learn how can you create a script using the printf command to display the current date.
Linux date Command Format Options
To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.
Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:
date +"Year: %Y, Month: %m, Day: %d"
Here are another two formatting examples:
date +"Week number: %V Year: %y"
These are the most common formatting characters for the date command:
-
- %D – Display date as mm/dd/yy
- %Y – Year (e.g., 2020)
- %m – Month (01-12)
- %B – Long month name (e.g., November)
- %b – Short month name (e.g., Nov)
- %d – Day of month (e.g., 01)
- %j – Day of year (001-366)
- %u – Day of week (1-7)
- %A – Full weekday name (e.g., Friday)
- %a – Short weekday name (e.g., Fri)
- %H – Hour (00-23)
- %I – Hour (01-12)
- %M – Minute (00-59)
- %S – Second (00-60)
To see all formatting options, run date —help or the man command man date in your terminal.
Set or Change Date in Linux
To change the system clock manually, use the set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:
Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.
Display Past Dates
Use the —date option to display past dates in Linux. The date command accepts values such as «tomorrow» , «Friday» , «last Friday» , «next Friday» , «next week» , and similar. So, use the following strings to print past dates::
Display Future Dates
The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:
Display the Date String at Line of File
The —file option prints the date string present at each line of the file. Unlike the —date option, —file can present multiple date strings at each line.
This is the syntax for the —file command:
Here we use the cat command to add dates to a file and then print them with the date command:
Display Last Modified Timestamp of a Date File
When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:
Override a Time Zone
By default, the date command uses the time zone defined in /etc/localtime . To use a different time zone in the environment, set the TZ variable to the desired time zone.
For example, to switch to New York time, enter:
Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.
The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:
date -d 'TZ="Australia/Sydney" 04:30 next Monday'
Use date with Other Commands
You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:
mysqldump database_name > database_name-$(date +%Y%m%d).sql
Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:
Use Unix Epoch Time (Epoch Converter)
You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.
To show the number of seconds from the epoch to the current day, use the %s format control:
To see how many seconds passed from epoch to a specific date, enter:
You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.
How to set the current time in Linux
This tutorial will discuss various methods of setting the current date and time from your Linux terminal session.
As a Linux user, you will come across instances where you need to configure the current date and time of a Linux system without access to a Graphical interface. Whether you are configuring a remote machine via SSH or just too lazy to exit the terminal, learning how to set the date and tiem from the terminal can be useful.
Method 1 — Using the timedatectl command
The timedatectl command enables you to query and change the system clock and settings. It also allows you to configure time synchronization services.
We can use this command to set our current date and time. Start by checking whether your system is configured to your desired timezone.
Local time: Sun 2022-11-27 20:04:06 PST Universal time: Mon 2022-11-28 04:04:06 UTC RTC time: Mon 2022-11-28 04:04:06 Time zone: US/Pacific (PST, -0800) System clock synchronized: no NTP service: active RTC in local TZ: no
In the example above, the current timezone is set to US/Pacific .
If your timezone is incorrect, you can change it by using the set-timezone command.
Start by listing all the available timezones in your machine.
timedatectl list-timezones | grep
For example, to fetch the Los Angeles Timezone:
timedatectl list-timezones | grep Angeles
Finally, run the set-timezones command to update your system’s timezone:
sudo timedatectl set-timezone
sudo timedatectl set-timezone America/Los_Angeles
You can then verify the changes have been applied with the command:
Local time: Sun 2022-11-27 17:09:25 PST Universal time: Mon 2022-11-28 01:09:25 UTC RTC time: Mon 2022-11-28 01:09:25 Time zone: America/Los_Angeles (PST, -0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
Once the correct timezone is configured, you need to enable the NTP service to synchronize your time. Run the command below to enable date and time synchronization:
sudo timedatectl set-ntp true
To check your current date and time, run:
Sun 27 Nov 2022 05:13:41 PM PST
Method 2 — Set Current Date and Time Using NTPD
The ntpd program is an operating system daemon which sets and maintains the system time of day in synchronism with Internet standard time servers
The nap protocol provides accurate date and time information from the internet as defined by your timezone. To set date and time using ntp, install the ntp daemon with the command:
sudo apt-get install ntp [Debian, Ubuntu] sudo yum install ntp [CentOS, RHEL] sudo pacman -S ntp [Arch, Manjaro]
Once installed, run the command to check the status of the daemon:
● ntp.service - Network Time Service Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2022-11-27 17:16:43 PST; 11s ago Docs: man:ntpd(8) Main PID: 1253 (ntpd) Tasks: 2 (limit: 1060) Memory: 1.4M CGroup: /system.slice/ntp.service └─1253 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 115:121 Nov 27 17:16:44 debian ntpd[1253]: Soliciting pool server 162.159.200.123 Nov 27 17:16:45 debian ntpd[1253]: Soliciting pool server 41.175.51.165 Nov 27 17:16:45 debian ntpd[1253]: Soliciting pool server 162.159.200.1 Nov 27 17:16:46 debian ntpd[1253]: Soliciting pool server 160.119.216.197 Nov 27 17:16:46 debian ntpd[1253]: Soliciting pool server 160.119.216.202 Nov 27 17:16:46 debian ntpd[1253]: Soliciting pool server 160.119.216.206 Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 2606:4700:f1::1 Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 64:ff9b::a29f:c801 Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 64:ff9b::a29f:c87b Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 64:ff9b::29af:33a5
Finally, activate time and date synchronization with the comand:
Local time: Sun 2022-11-27 17:21:37 PST Universal time: Mon 2022-11-28 01:21:37 UTC RTC time: Mon 2022-11-28 01:21:38 Time zone: America/Los_Angeles (PST, -0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
Method 3 — Setting System Date and Time using the date command
To set the date and time for your system using the date command, disable ntp with the command:
sudo timedatectl set-ntp false
Specify the date in the format: YYYY-MM-DD HH:MM:SS
$ sudo date -s '2022-12-27 17:25:58' Tue 27 Dec 2022 05:25:58 PM PST
Sync Hardware Clock with System Date
To synchronize your hardware clock with your current date and time, run the command:
Summary
In this tutorial, you learned various methods of setting up your current date and time from the comfort of your Linux terminal.
If you enjoy our content, please consider buying us a coffee to support our work:
A highly-skilled software engineer with a passion for teaching and sharing knowledge. From development to hardware, get articles with high technical accuracy, clarity, and an engaging writing style.