Set linux date to current

Date Command in Linux

The «date» command in Linux is a simple but powerful tool used to display the current date and time, as well as set the system date and time. This command is extremely useful for troubleshooting and system administration tasks, and is a vital tool in understanding any Linux user. In this article, we’ll discuss the basic usage of the date command, as well as some of its more advanced options and features. We’ll also cover some examples of how the date command can be used in real-world scenarios.

Basic usage of date command

The date command is one of the most basic commands in Linux. To display the current date and time, simply type «date» at the command prompt and press enter. The output will display the current date and time in the format «Day Month Date Time TimeZone Year». For instance −

$ date Tue Jan 25 14:20:34 EST 2022

Formatting the Output

One of the most useful features of the date command is the ability to specify a particular format for date and time output. This can be done using the «+» option followed by a format specifier. For example, to display the date in the «YYYY-MM-DD» format, use the command −

There are many format specifiers that can be used with the date command, each of which will display the date and time in a different format. Some of the more commonly used format specifiers include −

  • %Y − displays the year with century as a decimal number
  • %m − displays the month as a decimal number (01-12)
  • %d − displays the day of the month as a decimal number (01-31)
  • %H − displays the hour (24-hour clock) as a decimal number (00-23)
  • %M − displays the minute as a decimal number (00-59)
  • %S − displays the second as a decimal number (00-59)

Setting the Date and Time

The date command can also be used to set the system date and time. To do this, you need to log in as the root user. Use the following command to set the date and time.

$ sudo date -s "25 JAN 2022 14:20:34"

You can also set the date and time with the «-u» option to set it to Coordinated Universal Time (UTC) instead of local time.

$ sudo date -us "25 JAN 2022 14:20:34"

Working with Timezones

Another important feature of the date command is the ability to work with different time zones. By default, the date command will display the date and time in your local time zone, but you can use the «-u» option to display the date and time in UTC, or you can use the «-Z» to specify a different time zone.

$ date -u Tue Jan 25 14:20:34 UTC 2022 $ date -Z EST Tue Jan 25 09:20:34 EST 2022

Advance usage of date command

There are many options and advanced features of the date command that allow you to customize the output and behavior. Some of the most useful are −

  • The «-d» option, which allows you to specify a date in the format «YYYY-MM-DD«
  • The «-r» option, which displays the date and time of the specified file in the format «YYYY-MM-DD HH:MM:SS«
  • The «-R» option, which displays the date and time of the specified file in the format «Day, DD Mon YYYY HH:MM:SS«
  • The «-I» option, which displays the date and time in ISO 8601 format, which is a standardized format that is commonly used in computer systems and networks.
Читайте также:  Linux var lib usr

Examples of date command

The following examples demonstrate some of the options and advanced features of the date command −

To display the date in «YYYY-MM-DD» format −

To view the date and time of a file named «example.txt» −

$ date -r example.txt 2022-01-25 14:20:34

To set the date and time to Coordinated Universal Time (UTC) instead of local time

$ sudo date -us "25 JAN 2022 14:20:34"

To display the date and time in ISO 8601 format

Real World Scenarios

The date command can be used in a variety of real-world scenarios. Some examples include −

  • Checking the date and time on a remote server using SSH
  • Synchronizing the date and time on multiple servers using NTP
  • Creating timestamps for log files or backups
  • Parsing and analyzing log files for troubleshooting and performance monitoring

Conclusion

The date command is a powerful and versatile tool that is essential for any Linux user. Whether you need to check the current date and time, set the system date and time, or view the date and time for a specific file, the date command is the tool for the job. With its many options and features, it’s a powerful tool that can be used for a variety of tasks. With a good understanding of the date command, you will be able to perform many useful tasks, making the Linux operating system even more powerful and efficient. It’s a simple command to use, but it can be used in many different ways and scenarios. Therefore, it’s always a good idea to master the date command to make your Linux experience more efficient and productive.

Источник

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:

Читайте также:  Linux echo text file

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.

Источник

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

Читайте также:  Посмотреть какая файловая система linux

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 

Источник

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