How to change the date in linux

Set Time/Date/Timezone in Ubuntu Linux

Time is an important aspect in Linux systems especially in critical services such as cron jobs. Having the correct time on the server ensures that the server operates in a healthy environment that consists of distributed systems and maintains accuracy in the workplace.

In this tutorial, we will focus on how to set time/date/time zone and to synchronize the server clock with your Ubuntu Linux machine.

Check Current Time

You can verify the current time and date using the date and the timedatectl commands. These linux commands can be executed straight from the terminal as a regular user or as a superuser. The commands are handy usefulness of the two commands is seen when you want to correct a wrong time from the command line.

Using the date command

Log in as a root user and use the command as follows

Output

You can also use the same command to check a date 2 days ago

Output

Using timedatectl command

Checking on the status of the time on your system as well as the present time settings, use the command timedatectl as shown

Changing Time

We use the timedatectl to change system time using the format HH:MM: SS. HH stands for the hour in 24-hour format, MM stands for minutes and SS for seconds.

Setting the time to 09:08:07 use the command as follows (using the timedatectl)

using date command

Changing time means all the system processes are running on the same clock putting the desktop and server at the same time. From the command line, use date command as follows

Where,
• 10: Hour (hh)
• 13: Minute (mm)
• 13: Second (ss)

To change the locale to either AM or PM use the %p in the following format.

Change Date

Generally, you want your system date and time is set automatically. If for some reason you have to change it manually using date command, we can use this command :

It will set your current date and time of your system into ‘January 25, 2014′ and ’09:17:00 AM’. Please note, that you must have root privilege to do this.

You can use timedatectl to set the time and the date respectively. The accepted format is ‘YYYY-MM-DD’, ‘YYYY’ represents the year, ‘MM’ the month in two digits and ‘DD’ for the day in two digits.

Changing the date to ’15 January 2019′, you should use the following command:

Create custom date format

To create custom date format, use a plus sign (+)

%D format follows Year/Month/Day format.

You can also put the day name if you want. Here are some examples :

Читайте также:  Arduino uno драйвер windows 10 x64

List/Change time zone

Changing the time zone is crucial when you want to ensure that everything synchronizes with the Network Time Protocol. The first thing to do is to list all the region’s time zones using the list-time zones option or grep to make the command easy to understand

The above command will present a scrollable format.

Recommended timezone for servers is UTC as it doesn’t have daylight savings. If you know, the specific time zones set it using the name using the following command

To display timezone execute

Set the Local-rtc

The Real-time clock (RTC) which is also referred to as the hardware clock is independent of the operating system and continues to run even when the server is shut down.

Use the following command

In addition, the following command for the local time

Check/Change CMOS Time

The computer CMOS battery will automatically synchronize time with system clock as long as the CMOS is working correctly.

Use the hwclock command to check the CMOS date as follows

To synchronize the CMOS date with system date use the following format

Conclusion

To have the correct time for your Linux environment is critical because many operations depend on it. Such operations include logging events and cron jobs as well.

If you have any questions or feedback, feel free to leave a comment.

Источник

How To: Change time/date in linux from Command Prompt

Before starting this tutorial, let me clarify one thing that changing the time/date and changing a timezone are two different things. If you are looking for changing the Timezone then refer to this article: Change Timezone in Linux/Unix.

Changing the date and time:

Please note that you can change the time and date by graphical tool also but what I am explaining here is doing all that with command line.

In every system, there are two clocks which comes into action while setting the time, one is hardware clock and the other is linux (OS) clock. The hardware clock determines the system clock on system boot. While the system is running, changes to one of these doesn’t affect the other. Normally you can follow any procedure, like first update one clock and then sync it with the other but it is always advised to first update the hardware clock and then let the linux clock sync it with it at the next reboot. Changing the system clock by using the date program on a running system could cause date discontinuities and consequently problems. Down here, I will be discussing both ways.

For viewing/changing the time, we can use “date” command.

Changing time from “date” command is the easiest part. Simply copy paste the output of the date command and change the values you wanted to and then set it.

But this is something which doesn’t work in all flavors. For ex, flavors like freebsd work in different ways. To change time/date in freebsd is a little bit confusing but very much easy. You just need to use “date” command followed by the month, day, hour, minute, and year all numeric and no spaces.

If you will be using ntpd, you probably don’t need to set either of these clocks (unless the current time is more than 1000s off the real time). Just setup ntpd and let ntpd adjust the time (it will do it in small steps to keep system timestamps reliable).

Читайте также:  Обучение astra linux расширенное администрирование

Redhat Systems: Using the dateconfig tool will update both the system clock and the hardware clock. The dateconfig tool also allows you to setup ntpd, which will keep the system clock in sync with a remote server.

Only change time not date:

To only set the time use like this:

Update Hardware clock:

To sync the hardware clock to the current system clock:

Set Hardware clock manually:

Earlier i told you that better idea is to set the hardware clock and then let the OS clock sync with that.

To know the current time of the hardware clock:

To set the hw clock manually:

Everytime you use the hwclock –set command, it will create or edit the file /etc/adjtime to determine the systematic drift. Once you have some history, you can use the –adjust option to adjust the hardware clock appropriately. Run as a cron job if you want the clock to adjust automatically on a regular schedule. Don’t use the –adjust function when using ntpd since ntpd will turn the “11 minute mode” on, which is best left alone. See the hwclock manpage for more info.

Now you can leave the OS clock to sync it with the hardware clock on the next reboot or else you can do it manually now:

Using NTP (Network Time Protocol):

NTP will connect to the servers to get the atomic time. To use NTP, simply download and install it, and use the ntpdate command to sync your time with the NTP servers.

Now update the hardware clock also.

To keep your system always updated you can create a cronjob which could run on daily basis and put this:

Источник

Date Command in Linux: How to Set, Change, Format and Display Date

Home » SysAdmin » 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.

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

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:

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:

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:

Here are another two formatting examples:

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)
Читайте также:  Яндекс переводчик для сафари mac os

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:

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

Display Future Dates

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

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

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:

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:

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:

Another common use of the date command is in shell scripts. Below we assign the output of date to 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:

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

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.

Источник

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