Changing date and time 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 :

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

Читайте также:  Кодек heif hevc для windows 10

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 Set/Change Time, Date And Timezone In Linux?

If you missed to set the proper timezone, date and time while installing the OS. Don’t worry it can be changed at any point of time.

If we bought the server from some providers and they might have set the timezone based on their location.

Say for example, if you buy a server from USA then they will build a system based on their GEO location.

If so, don’t worry you can simply change the timezone and date formats as per your requirement using the below commands on your Linux Box.

In this article, i have covered all the possible methods with all Major Linux Distribution such as CentOS, Redhat (RHEL), Fedora, Ubuntu, Debian, Mint & openSUSE systems.

Also, we have updated the information based on the system manager such as SysVinit and systemd. Timezone is controlled by /etc/localtime file.

The below files are belongs to timezone on Linux.

  • /usr/share/zoneinfo: This directory contains timezone files.
  • /etc/localtime: This file is symlink with timezone file.
  • /etc/timezone: This file is holding timezone name on debian based systems.
  • /etc/sysconfig/clock: This file is holding timezone name on RHEL based systems.

1) How To Check Current Date, Time And Timezone Information In Linux?

Use the following commands to check the current date, time and timezone information in Linux systems.

To check current system date and time.

To display current system date and time with UTC format.

Run the following command to display Hardware Clock (RTC).

To check the timezone on SysVinit systems.

We can sell all the details together in the single command for systemd systems. See the details below.

2) How To Set/Change Date And Time In SysVinit Systems?

Use the following commands to set or change the time and date as you wish in Linux SysVinit systems. If you requires locale’s then you can set accordingly.

Common Syntax:

To set new date and time in one shot, use the following format.

You can double confirm this by running date command once again.

To set only time, use the following format.

To set only date, use the following format.

To Set time with locale’s.

To set the hardware clock to local time.

To set the hardware clock to UTC time.

3) How To Set/Change Date And Time In systemd Systems?

Use the following commands to set or change the time and date as you wish in Linux systemd systems.

Common Syntax:

To change the new date and time in one shot, use the following format.

To set only time, use the following format.

To set only date, use the following format.

Use the following format to set RTC time.

4) How To Change The TimeZone In systemd Systems?

For systemd system, use the timedatectl command to change the timezone. Navigate to the following url to change the timezone in Linux.

If you would like to restart the timedatectl service, use the following command.

Run the following command to verify the new timezone.

5) How To Change The TimeZone In RHEL 6/CentOS 6 Systems?

For RHEL/CentOS systems, use the following command to change the timezone.

Run the following command to check the new timezone in RHEL/CentOS systems.

6) How To Automatically Adjust Your Linux System Clock?

If you would like to sync your Linux system clock to remote NTP server then you have configure it.

Читайте также:  Как узнать тип оперативной памяти компьютера windows 10 ddr

To do so, navigate the following urls because we had written an detailed articles about these in the past.

Источник

Linux Set Date and Time From a Command Prompt

H ow can I set the system date and time from the command prompt (bash shell)? I don’t have GUI installed and I am login over ssh session. How can I set date under Linux operating systems?

Use the date command to display the current date and time or set the system date / time over ssh session. You can also run the date command from X terminal as root user.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 2m

This is useful if the Linux server time and/or date is wrong, and you need to set it to new values from the shell prompt.

You must login as root user to use date command.

Linux Display Current Date and Time

Just type the date command:
$ date
Sample outputs:

Linux Display The Hardware Clock (RTC)

Type the following hwclock command to read the Hardware Clock and display the time on screen:
# hwclock -r
OR
# hwclock —show
$ sudo hwclock —show —verbose

OR show it in Coordinated Universal time (UTC):
# hwclock —show —utc
Sample outputs:

Linux Set Date Command Example

Use the following syntax to set new data and time:
date —set=»STRING»
For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:
# date -s «2 OCT 2006 18:00:00»
OR
# date —set=»2 OCT 2006 18:00:00″
You can also simplify format using following syntax:
# date +%Y%m%d -s «20081128»

Linux Set Time Examples

To set time use the following syntax:
# date +%T -s «10:13:13»
Where,

Use %p locale’s equivalent of either AM or PM, enter:
# date +%T%p -s «6:10:30AM»
# date +%T%p -s «12:10:30PM»

How do I set the Hardware Clock to the current System Time?

Use the following syntax:
# hwclock —systohc
OR
# hwclock -w

A note about systemd based Linux system

With systemd based system you need to use the timedatectl command to set or view the current date and time. Most modern distro such as RHEL/CentOS v.7.x+, Fedora Linux, Debian, Ubuntu, Arch Linux and other systemd based system need to the timedatectl utility. Please note that the above command should work on modern system too.

timedatectl: Display the current date and time

Type the following command:
$ timedatectl

Fig.01: Systemd Linux timedatecetl command to display the current date and time

How do I change the current date using the timedatectl command?

To change the current date, type the following command as root user:
# timedatectl set-time YYYY-MM-DD
OR
$ sudo timedatectl set-time YYYY-MM-DD
For example set the current date to 2015-12-01 (1st, Dec, 2015):
# timedatectl set-time ‘2015-12-01’
# timedatectl
Sample outputs:

To change both the date and time, use the following syntax:
# timedatectl set-time YYYY-MM-DD HH:MM:SS
Where,

  1. HH : An hour.
  2. MM : A minute.
  3. SS : A second, all typed in two-digit form.
  4. YYYY: A four-digit year.
  5. MM : A two-digit month.
  6. DD: A two-digit day of the month.

For example, set the date ’23rd Nov 2015′ and time to ‘8:10:40 am’, enter:
# timedatectl set-time ‘2015-11-23 08:10:40’
# date

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How do I set the current time only?

The syntax is:
# timedatectl set-time HH:MM:SS
# timedatectl set-time ’10:42:43′
# date
Sample outputs:

How do I set the time zone using timedatectl command?

To see list all available time zones, enter:
$ timedatectl list-timezones
$ timedatectl list-timezones | more
$ timedatectl list-timezones | grep -i asia
$ timedatectl list-timezones | grep America/New
To set the time zone to ‘Asia/Kolkata’, enter:
# timedatectl set-timezone ‘Asia/Kolkata’
Verify it:
# timedatectl

How do I synchronizing the system clock with a remote server using NTP?

Simply type the following command:
# timedatectl set-ntp yes
Verify it:
$ timedatectl
Sample outputs:

Conclusion

Linux users can use date command to print or set the system date and time. Systemd based Linux users can use timedatectl to control the system time and date.

  • You can also set new timzone using this mini-howto.
  • Man pages – timedatectl(8)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Change the Linux Date and Time: Simple Commands

Telling the time on Linux is more complicated than it might seem at first glance. To start with, the time command on Linux doesn’t tell the time:

Because time is a timer for measuring how long a process runs. For example, how long does it take to recursively list all the files in a directory?

Date for Time

It may sound odd, but you must use the date command to see the time on Linux:

The date command is fundamental to understanding time options on Linux. For example, the panel clock in Xfce4 supports using the standard date options to customize the date and time display. Figure 1 shows what mine looks like.

This comes from these FORMAT options for the date command: %r %n%a %b %d, %Y , which you can easily test for yourself:

man date details all the formatting options. Note how you can use ordinary spacing and punctuation to control the appearance. You customize date and time displays to suit your own whims, and in consistent, script-friendly ways.

man date lists a good set of options, but it does not tell you everything. To get the complete story of date you need the GNU coreutils manual. And that is where we learn about the magic strings that let us ask for dates next week, last year, day of week, and many more. Like the date three Tuesdays from now, five months from now, eight years ago:

You can quickly check the time in a different time zone:

UTC (Coordinated Universal Time) is the universal standard for time worldwide. When you know your UTC offset you always know what time it is, because date will tell you:

If you live in one of those demented regions that uses Daylight Savings Time, date -R will always tell you the correct offset no matter what time of year it is.

Cal For Dates

When you just want to see some dates, think of our good old friend cal :

cal -3 displays three months: last month, this month, and next month. cal YYYY displays a specific year, like cal 1962 .

ncal is included on most Linux systems, and it is an oldtimer designed to fit nicely on a 25×80 terminal:

ncal -b switches to our customary horizontal display. ncal will show an arbitrary number of months in the past or future. For example ncal -bB 6 displays the current month plus six months previous, and ncal -bA 6 display the current month plus the next six months.

You can see any month in any year, for example March 1950, with ncal -bm March 1950 . This works for future months, too.

Those Wacky ls Timestamps

The way the ls outputs the date and time is a continual source of vexation because it varies on the different Linux distributions. This is how it looks on my Linux Mint system:

Files dated within the last six months display the time instead of the year, and older files display the year and not the time. The Mint time style is called iso , and this is the the GNU default. long-iso is my preference, and it looks like this:

I like long-iso because it’s easy to sort– year, month, day, single-digit months and days are padded to two digits, and it uses a 24-hour clock. This is all defined in ISO 8601. On Linux it’s controlled by the TIME_STYLE environment variable, so you can override the default system-wide in /etc/profile , or in your personal .profile or .bashrc by adding a line like export TIME_STYLE=long-iso , then logging out and back in.

You might want to create a custom timestamp. This uses the same options as the date command, and you can test this on the command line before making it permanent in a configuration file:

The GNU manual spells all this out in plain English. You can experiment to your heart’s content, and then log out and log back in to reset to your system default.

And that is just the beginning of telling time on Linux. My dream is a lifestyle that doesn’t need clocks at all, but I haven’t figured out how to do that in Linux.

Источник

Читайте также:  Ja2 не запускается windows 10
Оцените статью