Set time date linux command

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.

Источник

11 Useful Linux date command examples (How to set date and time in Linux)

CyberITHub

In this article, I will take you through 11 Useful Linux date command examples. date is an important Linux command frequently used to set System date and time. You might have encountered few cluster based set up where each and every node needs to be in date and time sync with each other to be able to run successfully. Once it goes out of date or time sync beyond few seconds or minutes, cluster goes down. Hence it is very important to set the date and time correctly in the System. In this session we will look at how to set date and time in Linux using date command examples.

Читайте также:  Start server minecraft linux

Syntax

date [OPTION]. [+FORMAT]
date [-u|—utc|—universal] [MMDDhhmm[[CC]YY][.ss]]

11 Useful Linux date command examples (How to set date and time in Linux) 1

Linux date command Examples

Example 1: How to check current date and time using date command

If you want to check current date and time of your System then you need to simple run date command as shown below.

[root@localhost ~]# date Fri May 15 19:16:12 EDT 2020

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.

Example 2: How to check future date using date command

If you want to check date of some future day then you can check it by using —date option.

[root@localhost ~]# date --date="next fri" Fri May 22 00:00:00 EDT 2020

To check Next Tueday date you can use below command.

[root@localhost ~]# date --date="next tue" Tue May 19 00:00:00 EDT 2020

To check Next Sunday date you can use below command.

[root@localhost ~]# date --date="next sun" Sun May 24 00:00:00 EDT 2020

To check tomorrow date you can use below command.

[root@localhost ~]# date --date="tomorrow" Mon May 18 23:14:17 EDT 2020

To check yesterday date you can use below command.

[root@localhost ~]# date --date="yesterday" Sat May 16 23:14:22 EDT 2020

To check after 2 Years date you can use below command.

[root@localhost ~]# date --date="2 Year" Tue May 17 23:14:29 EDT 2022

To check after 2 months date you can use below command.

[root@localhost ~]# date --date="2 months" Fri Jul 17 23:14:48 EDT 2020

—date : display time described by STRING, not ‘now’. More info on date command Man page.

Example 3: How to check version of date command

If you want to check the version of date command then you need to use —version option as shown below. As you can see from below output current version is 8.22

[root@localhost ~]# date --version date (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie.

—version : output version information and exit. More info on date command Man page.

Example 4: How to Check current UTC Time using date command

You can check the current Coordinated Universal Time by using date -u command.

[root@localhost ~]# date -u Fri May 15 23:25:48 UTC 2020

-u : print or set Coordinated Universal Time (UTC)

Читайте также:  Посмотреть содержимое файла линукс команда

Example 5: How to Set date and time in Linux

If you want to change both data and time then you can use —set option with date command to change that as shown below.

[root@localhost ~]# date --set="2020-05-17 11:12:13" Sun May 17 11:12:13 EDT 2020

—set : set time described by STRING

Example 6: How to Set only time in Linux using date command

If you want to only change the time then you can use the same —set option as shown in above example and set the time only instead of setting both date and time.

[root@localhost ~]# date --set="14:12:13" Sun May 17 14:12:13 EDT 2020

Example 7: How to Set only date in Linux using date command

If you want to change only date but not the time then you need to first check the current time and then set accordingly. For example, if you check the current date and time you can see that current date is 16th May 2020 and current time is 13:16:04 EDT .

[root@localhost ~]# date Sat May 16 13:16:04 EDT 2020

So, if you want to change the date only say to 17 May,2020 in this example keeping the time same then you need to use below date command.

[root@localhost ~]# date --set="2020-05-17 13:16:04" Sun May 17 13:16:04 EDT 2020

Example 8: How to Check the Last Modification date timestamp of a file

If you want to check the last modification date timestamp of a file then you need to use -r option with date command as shown below. In this example we are checking the last modification date timestamp of a file file1.txt using date -r file1.txt command.

[root@localhost ~]# date -r file1.txt Tue Apr 28 00:55:56 EDT 2020

-r : display the last modification time of FILE

Example 9: How to Display date in RFC Format

If you want to display date in RFC format then you need to use date -R command as shown below.

[root@localhost ~]# date -R Sun, 17 May 2020 14:13:05 -0400

-R : output date and time in RFC 2822 format

Example 10: Using Format Specifiers with date command

There are lot of format specifiers available to use with date command as per the needs. You can check and find its usages below.

[root@localhost ~]# date "+%a" Sun

%a locale’s abbreviated weekday name (e.g., Sun)

[root@localhost ~]# date "+%A" Sunday

%A locale’s full weekday name (e.g., Sunday)

[root@localhost ~]# date "+%b" May

%b locale’s abbreviated month name (e.g., Jan)

[root@localhost ~]# date "+%B" May

%B locale’s full month name (e.g., January)

[root@localhost ~]# date "+%c" Sun 17 May 2020 04:50:02 PM EDT

%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)

%C century; like %Y, except omit last two digits (e.g., 20)

%d day of month (e.g., 01)

[root@localhost ~]# date "+%D" 05/17/20

%D date; same as %m/%d/%y

Читайте также:  Загрузка графической оболочки astra linux

%e day of month, space padded; same as %_d

[root@localhost ~]# date "+%F" 2020-05-17

%F full date; same as %Y-%m-%d

%g last two digits of year of ISO week number (see %G)

[root@localhost ~]# date "+%G" 2020

%G year of ISO week number (see %V); normally useful only with %V

[root@localhost ~]# date "+%h" May

%h same as %b

%H hour (00..23)

%I hour (01..12)

[root@localhost ~]# date "+%j" 138

%j day of year (001..366)

%k hour, space padded ( 0..23); same as %_H

%l hour, space padded ( 1..12); same as %_I

%m month (01..12)

%M minute (00..59)

%n a newline

[root@localhost ~]# date "+%N" 046197179

%N nanoseconds (000000000..999999999)

%p locale’s equivalent of either AM or PM; blank if not known

%P like %p, but lower case

[root@localhost ~]# date "+%r" 09:15:07 PM

%r locale’s 12-hour clock time (e.g., 11:11:04 PM)

[root@localhost ~]# date "+%R" 21:15

%R 24-hour hour and minute; same as %H:%M

[root@localhost ~]# date "+%s" 1589764531

%s seconds since 1970-01-01 00:00:00 UTC

%S second (00..60)

[root@localhost ~]# date "+%T" 21:16:07

%T time; same as %H:%M:%S

%u day of week (1..7); 1 is Monday

%U week number of year, with Sunday as first day of week (00..53)

%V ISO week number, with Monday as first day of week (01..53)

%w day of week (0..6); 0 is Sunday

%W week number of year, with Monday as first day of week (00..53)

[root@localhost ~]# date "+%x" 05/17/2020

%x locale’s date representation (e.g., 12/31/99)

%y last two digits of year (00..99)

[root@localhost ~]# date "+%Y" 2020
[root@localhost ~]# date "+%z" -0400

%z +hhmm numeric time zone (e.g., -0400)

Example 11: Check Other date command options

If you want to check all the other options available with date command you can use date —help command as shown below.

[root@localhost ~]# date --help Usage: date [OPTION]. [+FORMAT] or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date. Mandatory arguments to long options are mandatory for short options too. -d, --date=STRING display time described by STRING, not 'now' -f, --file=DATEFILE like --date once for each line of DATEFILE -I[TIMESPEC], --iso-8601[=TIMESPEC] output date/time in ISO 8601 format. TIMESPEC='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated precision. -r, --reference=FILE display the last modification time of FILE -R, --rfc-2822 output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600 --rfc-3339=TIMESPEC output date and time in RFC 3339 format. TIMESPEC='date', 'seconds', or 'ns' for date and time to the indicated precision. Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00 -s, --set=STRING set time described by STRING -u, --utc, --universal print or set Coordinated Universal Time (UTC) --help display this help and exit --version output version information and exit

—help : display this help and exit

Recommended Posts:-

Источник

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