Time setting in linux command

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!

Читайте также:  Ubuntu one gnome linux

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 Set Time, Timezone and Synchronize System Clock Using timedatectl Command

The timedatectl command is a new utility for RHEL/CentOS 7/8 and Fedora 30+ based distributions, which comes as a part of the systemd system and service manager, a replacement for old traditional date command used in sysvinit daemon based Linux distributions.

Set System Time, Date and Timezone in Linux

The timedatectl command allows you to query and change the configuration of the system clock and its settings, you can use this command to set or change the current date, time, and timezone or enable automatic system clock synchronization with a remote NTP server.

Читайте также:  Network manager disable linux

In this tutorial, am going to take you through the ways you can manage time on your Linux system by setting the date, time, timezone, and synchronize time with NTP from the terminal using the new timedatectl command.

It is always a good practice to maintain the correct time on your Linux server or system and it can have the following advantages:

  • maintain a timely operation of system tasks since most tasks in Linux are controlled by time.
  • the correct time for logging events and other information on the system and many more.

How to Find and Set Local Timezone in Linux

1. To display the current time and date on your system, use the timedatectl command from the command line as follows:

In the screencast above, RTC time is the hardware clock time.

2. The time on your Linux system is always managed through the timezone set on the system, to view your current timezone, do it as follows:

# timedatectl OR # timedatectl | grep Time

3. To view all available timezones, run the command below:

# timedatectl list-timezones

4. To find the local timezone according to your location, run the following command:

# timedatectl list-timezones | egrep -o "Asia/B.*" # timedatectl list-timezones | egrep -o "Europe/L.*" # timedatectl list-timezones | egrep -o "America/N.*"

5. To set your local timezone in Linux, we will use the set-timezone switch as shown below.

# timedatectl set-timezone "Asia/Kolkata"

It is always recommended to use and set the coordinated universal time, UTC.

# timedatectl set-timezone UTC

You need to type the correct name timezone otherwise you may get errors when changing the timezone, in the following example, the timezone “Asia/Kolkata” is not correct therefore causing the error.

How to Set Time and Date in Linux

6. You can set the date and time on your system, using the timedatectl command as follows:

Set Time in Linux

To set time only, we can use a set-time switch along with the format of time in HH:MM:SS (Hour, Minute, and Seconds).

# timedatectl set-time 15:58:30

You may get the below error when setting the date as shown above:

Failed to set time: NTP unit is active

7. The error says that the NTP service is active. You need to disable it using the below command.

# systemctl disable --now chronyd

Set Date and Time in Linux

8. To set date and time, we can use a set-time switch along with the format of date in YY:MM:DD (Year, Month, Day) and time in HH:MM:SS (Hour, Minute, and Seconds).

# timedatectl set-time '2015-11-20 16:14:50'

How to Find and Set Hardware Clock in Linux

9. To set your hardware clock to coordinated universal time, UTC, use the set-local-rtc boolean-value option as follows:

Читайте также:  Kali linux can install grub

First Find out if your hardware clock is set to local timezone:

Set your hardware clock to local timezone:

# timedatectl set-local-rtc 1

Set your hardware clock to coordinated universal time (UTC):

# timedatectl set-local-rtc 0

Synchronizing Linux System Clock with a Remote NTP Server

NTP stands for Network Time Protocol is an internet protocol, which is used to synchronize the system clock between computers. The timedatectl utility enables you to automatically sync your Linux system clock with a remote group of servers using NTP.

Please note that you must have NTP installed on the system to enable automatic time synchronization with NTP servers.

To start automatic time synchronization with a remote NTP server, type the following command at the terminal.

To disable NTP time synchronization, type the following command at the terminal.

Summary

These are very easy examples described in this tutorial and I hope you will find them helpful for setting various Linux system clocks and timezones. To learn more about this tool, head over to timedatectl man page.

If you have anything to say about this article, feel free to leave a comment for any more information to add. Stay connected to Tecmint.

Источник

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.

Источник

Оцените статью
Adblock
detector