How to set date on linux

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 

Источник

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.

How to use Linux date command

  • 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:

Date command in linux

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:

Читайте также:  Epson m1170 драйвер astra linux

See an older date

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:

Pull a date from a string

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"

Format the current date

Here are another two formatting examples:

Another date format example

date +"Week number: %V Year: %y"

Another formatting example

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:

    Manual time set in Linux

    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::

    Format to

    Yesterday

    Time ten seconds ago format in linux

    Display Future Dates

    The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

    Date next monday format in linux

    Date four days from now format in linux

    Tomorrow

    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 dates at each file line in linux

    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:

    The last date the file was modified in linux

    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:

    Switch to New York time zone in Linux

    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'

    Switch to South Australian time in Linux

    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:

    Echo 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:

    Epoch seconds in linux

    To see how many seconds passed from epoch to a specific date, enter:

    Epoch seconds from a specific date in Linux

    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 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