- Linux: set date through command line
- 7 Answers 7
- Easy
- direct
- How to Change Date, Time, and Time Zone in Linux Mint 20
- Change date in Linux from the command line
- Check the updated date and time from command line
- Change time in Linux from the command line
- Change date and time with one command on Ubuntu
- Set hardware clock in Linux
- Change the time zone through the command line
- List of available time zones
- Change the date, time, and time zone through GUI
- Conclusion
- How to change ubuntu’s server date and time via command line?
- 7 Answers 7
Linux: set date through command line
You can use e.g. date —set=’-2 years’ to set the clock back two years, leaving all other elements identical. You can change month and day of month the same way. I haven’t checked what happens if that calculation results in a datetime that doesn’t actually exist, e.g. during a DST switchover, but the behaviour ought to be identical to the usual «set both date and time to concrete values» behaviour.
Assuming you’re trying to set the date to the current time, you could do sudo ntpd -gq to have the system update automatically using the ntp service.
7 Answers 7
Run that as root or under sudo . Changing only one of the year/month/day is more of a challenge and will involve repeating bits of the current date. There are also GUI date tools built in to the major desktop environments, usually accessed through the clock.
To change only part of the time, you can use command substitution in the date string:
will change the date, but keep the time. See man date for formatting details to construct other combinations: the individual components are %Y , %m , %d , %H , %M , and %S .
There’s no option to do that. You can use date -s «2014-12-25 $(date +%H:%M:%S)» to change the date and reuse the current time, though.
@MichaelHomer @SHW it is possible to change only the date with a command like date -s 2018-01-01 . Someone shared this in an answer also: superuser.com/questions/870068/…
System time
You can use date to set the system date. The GNU implementation of date (as found on most non-embedded Linux-based systems) accepts many different formats to set the time, here a few examples:
date -s 'next year' date -s 'last year'
date -s 'last month' date -s 'next month'
date -s 'next day' date -s 'tomorrow' date -s 'last day' date -s 'yesterday' date -s 'friday'
date -s '2009-02-13 11:31:30' #that's a magical timestamp
Hardware time
Now the system time is set, but you may want to sync it with the hardware clock:
Use —show to print the hardware time:
You can set the hardware clock to the current system time:
Or the system time to the hardware clock
Bonus points for illustrating the non-obvious ways you can set the date with examples and for pointing out the difference between the «system time» and the «hardware clock». Very helpful answer!
I think you confused hctosys and systohc. hctosys means «Set system time from hardware clock» and systohc means «Set hardware clock from system time».
The command to to change the system date is date .
There are two ways to call the date command(in Linux):
date [OPTION]. [+FORMAT] date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Easy
The easiest way is to use date -s as it allows the use of simple relative dates
$ date -s yesterday; date date: cannot set date: Operation not permitted Sat Jan 5 07:21:07 EST 2019 Sun Jan 6 07:21:07 EST 2019
The date did not change because it was executed with a limited user $ . If you actually want the date changed, use root ( # ) or sudo:
$ sudo date -s yesterday; date Sat Jan 5 07:21:07 EST 2019 Sat Jan 5 07:21:07 EST 2019
So, changing any part of a relative date is as easy as naming it:
$ date -s "5 years ago" Mon Jan 6 08:26:26 EST 2014 $ date -s "+6 months" Sat Jul 6 08:28:39 EDT 2019 $ date -s "+3 hours -13 minutes" Sun Jan 6 11:16:59 AST 2019
Absolute dates are a bit more complex as they need more detail:
Or, you can use the date command twice:
replace any of the % by a valid value and the date will be set (only as root).
$ date -s "$(date +'%Y-11-%d %H:%M:%S')" Wed Nov 6 08:37:15 EST 2019
direct
The second date call form is used to directly change the system date.
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Will set the date to the 23th of November at 08h and 12min.
Try date as a limited user to see what it would do (without changing anything):
$ date 11230812 date: cannot set date: Operation not permitted Sat Nov 23 08:12:00 EST 2019
Or, if you actually want to change the date, as root:
# date 11230812 # date Sat Nov 23 08:12:00 EST 2019
Note that services like NTP or chrony will be affected. And, if restarted will reset the date back to the real one.
$ date 1123081222 date: cannot set date: Operation not permitted Wed Nov 23 08:12:00 EST 2022
Or a CCYY to set year and century:
$ date 112308121982 date: cannot set date: Operation not permitted Tue Nov 23 08:12:00 EST 1982
How to Change Date, Time, and Time Zone in Linux Mint 20
A time zone is identified based on the geographic region with the same standard time and date. Typically, the date, time, and time zone are set during the installation of an operational system. Users need to change the time zone for performing a few tasks in Linux platforms. Some jobs in Linux use time zone like cron jobs use it for execution or in logs timestamps. In this tutorial, we will go through different ways of changing the date, time, and time zone in Linux Mint 20 and Ubuntu 20.04.
To have the correct date and time in any operating system is very important since many operations depend on the date and time feature. To set a date, time in Linux Mint, open up the terminal either using the Ctrl+Shift+T shortcut or access it via ApplicationsTerminal.
Change date in Linux from the command line
In Linux platforms, the server and the clock of the system need to be exactly on time. You need to have sudo rights privilege to make such changes in your computer system. To proceed, type the following command in the terminal window:
The output will appear as shown in the figure below and the date will be set based on the input value.
Check the updated date and time from command line
To check the updated time from the command line, simply type
The output will display the updated time field.
timedatectl is a utility of Linux that allows users to view the system’s time and date through the terminal. To view the current time, date and time zone, use:
Change time in Linux from the command line
Users can also change the time from the command line by using
Here we have set the time to 15:14:00 and once we press the output will appear as shown below. The time will be updated accordingly.
Change date and time with one command on Ubuntu
Linux allows users to update date and time through one single command as well. To update date and time through the one common command, type the following:
11: Hour (hh)
14: Minute (mm)
00: Second (ss)
As soon as you hit the key, the output will appear indicating that the date and time are updated accordingly.
Set hardware clock in Linux
A hardware clock runs within the hardware of your computer system even if there is no power supply still it continues to function. To check out the hardware clock use the following command:
The output will appear as shown in the figure below, displaying the time of the hardware clock.
Now let’s check out the method that is used to set the hardware clock to the local time in a Linux system. For that, you will need to type:
$ hwclock --set --date="YYYY-MM-DD 16:45:05" --localtime
The hardware clock will be adjusted according to the local time.
Change the time zone through the command line
In order to change the time zone. First, let’s view the time zone of the system using timedatectl.
The system time zone in a Linux Mint system is configured by symlinking which is a /etc/localtime file. It is further configured to the binary time zone identifier that exists in the /usr/share/zoneinfo directory.
Another way by which, users can check the available time zone by using:
The output will look like this:
List of available time zones
To change the time zone, at first you need to find out and know the name of that time zone which you plan to use. Typically, the time zone in any system follows the “Region/City” format. Let’s view all available options.long name of the time zone you want to use:
$ timedatectl list-timezones
The output will show a list similar to the one displayed below:
Now, you can use any of the option from the list to update the time zone. Use the following command:
$ sudo timedatectl set-timezone
Here, in the example the time zone used by us is America/New_York. You can adjust the time zone as per your requirements following the same method.
Change the date, time, and time zone through GUI
To change the date, time and time zone through the UI method in Linux Mint 20, simply access the date and time shortcut on the bottom right of the screen and click on it. A calendar menu will open up, click on the Date and Time Settings option available at the bottom of the calendar.
You will see the Manually set date and Time option. Click on the date option against the Manually set date and time field.
A Select a Date modal will appear, you need to select a date then press the OK button to confirm the selection.
To change the time, select the Time option against the Manually set date and time in the Date & Time menu.
A modal will appear as shown in the figure below, update the time then select OK to reflect changes.
To update the time zone, click on Region in date and time modal.
You will see a list of Regions available, select the region first. It will show possible cities for that selected region. You need to select a city to proceed, once done select OK to make changes.
Conclusion
The methods discussed in this tutorial, tell the ways to change the date, time, and time zone in Linux Mint 20. Users can change them either through the command line or through the UI based on their preference. Both methods are easy to implement and explained in detail in this tutorial with examples.
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.