Documentation for a newer release is available. View Latest

Configuring the Date and Time

Modern operating systems distinguish between the following two types of clocks:

  • A real-time clock (RTC), commonly referred to as a hardware clock, (typically an integrated circuit on the system board) that is completely independent of the current state of the operating system and runs even when the computer is shut down.

  • A system clock, also known as a software clock, that is maintained by the kernel and its initial value is based on the real-time clock. Once the system is booted and the system clock is initialized, the system clock is completely independent of the real-time clock.

The system time is always kept in Coordinated Universal Time (UTC) and converted in applications to local time as needed. Local time is the actual time in your current time zone, taking into account daylight saving time (DST). The real-time clock can use either UTC or local time. UTC is recommended.

Fedora 31 offers three command line tools that can be used to configure and display information about the system date and time: the timedatectl utility, which is new in Fedora 31 and is part of systemd; the traditional date command; and the hwclock utility for accessing the hardware clock.

Using the timedatectl Command

The timedatectl utility is distributed as part of the systemd system and service manager and allows you to review and change the configuration of the system clock. You can use this tool to change the current date and time, set the time zone, or enable automatic synchronization of the system clock with a remote server.

For information on how to display the current date and time in a custom format, see also Using the date Command.

Displaying the Current Date and Time

To display the current date and time along with detailed information about the configuration of the system and hardware clock, run the timedatectl command with no additional command line options:

timedatectl

This displays the local and universal time, the currently used time zone, the status of the Network Time Protocol (NTP) configuration, and additional information related to DST.

Example 1. Displaying the Current Date and Time

The following is an example output of the timedatectl command on a system that does not use NTP to synchronize the system clock with a remote server:

$ timedatectl
               Local time: tis 2022-07-12 10:30:56 CEST
           Universal time: tis 2022-07-12 08:30:56 UTC
                 RTC time: tis 2022-07-12 08:30:56
                Time zone: Europe/Stockholm (CEST, +0200)
System clock synchronized: yes
              NTP service: inactive
          RTC in local TZ: no

Changing the Current Time

To change the current time, type the following at a shell prompt as root:

timedatectl set-time HH:MM:SS

Replace HH with an hour, MM with a minute, and SS with a second, all typed in two-digit form.

This command updates both the system time and the hardware clock. The result it is similar to using both the date --set and hwclock --systohc commands.

Kommandot kommer misslyckas om en `NTP`tjänst är aktiverad. Se Att synkronisera systemklockan med en fjärrserver för att tillfälligt avaktivera tjänsten.

Example 2. Changing the Current Time

To change the current time to 11:26 p.m., run the following command as root:

~]# timedatectl set-time 23:26:00

By default, the system is configured to use UTC. To configure your system to maintain the clock in the local time, run the timedatectl command with the set-local-rtc option as root:

timedatectl set-local-rtc boolean

To configure your system to maintain the clock in the local time, replace boolean with yes (or, alternatively, y, true, t, or 1). To configure the system to use UTC, replace boolean with no (or, alternatively, n, false, f, or 0). The default option is no.

Changing the Current Date

To change the current date, type the following at a shell prompt as root:

timedatectl set-time YYYY-MM-DD

Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month.

Note that changing the date without specifying the current time results in setting the time to 00:00:00.

Example 3. Changing the Current Date

To change the current date to 2 June 2013 and keep the current time (11:26 p.m.), run the following command as root:

~]# timedatectl set-time "2013-06-02 23:26:00"

Changing the Time Zone

För att räkna upp alla tillgängliga tidszoner, skriv följande vid en skalprompt:

timedatectl list-timezones

To change the currently used time zone, type as root:

timedatectl set-timezone tidszon

Ersätt tidszon med någon av de värden som räknades upp av kommandot timedatectl list-timezones.

Example 4. Changing the Time Zone

To identify which time zone is closest to your present location, use the timedatectl command with the list-timezones command line option. For example, to list all available time zones in Europe, type:

~]# timedatectl list-timezones | grep Europe
Europe/Amsterdam
Europe/Andorra
Europe/Athens
Europe/Belgrade
Europe/Berlin
Europe/Bratislava

To change the time zone to Europe/Prague, type as root:

~]# timedatectl set-timezone Europe/Prague

Synchronizing the System Clock with a Remote Server

As opposed to the manual adjustments described in the previous sections, the timedatectl command also allows you to enable automatic synchronization of your system clock with a group of remote servers using the NTP protocol. Enabling NTP enables the chronyd or ntpd service, depending on which of them is installed.

The NTP service can be enabled and disabled using a command as follows:

timedatectl set-ntp boolean

To enable your system to synchronize the system clock with a remote NTP server, replace boolean with yes (the default option). To disable this feature, replace boolean with no.

Example 5. Synchronizing the System Clock with a Remote Server

To enable automatic synchronization of the system clock with a remote server, type:

~]# timedatectl set-ntp yes

The command will fail if an NTP service is not installed. See Installing chrony for more information.

Using the date Command

The date utility is available on all Linux systems and allows you to display and configure the current date and time. It is frequently used in scripts to display detailed information about the system clock in a custom format.

For information on how to change the time zone or enable automatic synchronization of the system clock with a remote server, see Using the timedatectl Command.

Displaying the Current Date and Time

To display the current date and time, run the date command with no additional command line options:

date

This displays the day of the week followed by the current date, local time, abbreviated time zone, and year.

By default, the date command displays the local time. To display the time in UTC, run the command with the --utc or -u command line option:

date --utc

You can also customize the format of the displayed information by providing the +"format" option on the command line:

date +"format"

Ersätt format med en eller flera av de stödda styrsekvenserna så som det illustreras i Att visa det aktuella datumet och den aktuella tiden. Se Vanligen använda styrsekvenser för en lista över de vanligen oftast använda alternativen, eller manualsidan date(1) för en fullständig lista över dessa alternativ.

Table 1. Vanligen använda styrsekvenser
Styrsekvens Beskrivning

%H

Timme i formatet HH (till exampel, 17).

%M

Minut i formatet MM (till exampel, 30).

%S

Sekund i formatet SS (till exampel, 24).

%d

Dag i månaden i formatet DD (till exampel, 16).

%m

Månad i formatet MM (till exampel, 09).

%Y

År i formatet ÅÅÅÅ (till exampel, 2013).

%Z

Tidszonsförkortning (till exampel, CEST).

%F

Fullständigt datum i formatet ÅÅÅÅ-MM-DD (till exampel, 2022-07-14). Detta alternativ är samma som %Y-%m-%d.

%T

Fullständig tid i formatet HH:MM:SS (till exampel, 17:30:24). Detta alternativ är samma som %H:%M:%S

Example 6. Displaying the Current Date and Time

För att visa aktuellt datum och tid lokalt, skriv följande vid en skalprompt:

~]$ date
tor 14 jul 2022 20:59:56 CEST

För att visa aktuellt datum och tid i UTC, skriv följande vid en skalprompt:

~]$ date --utc
tor 14 jul 2022 18:59:56 UTC

För att anpassa utdata från kommandot date, skriv:

~]$ date +"%Y-%m-%d %H:%M"
2022-07-14 20:59

Changing the Current Time

För att ändra aktuell tid, kör kommandot date med flaggan --set eller -s som root:

date --set HH:MM:SS

Replace HH with an hour, MM with a minute, and SS with a second, all typed in two-digit form.

Som standard ställer kommandot date klockan till den lokala tiden. För att ställa klockan i i UTC, kör kommandot med kommandoradsflaggan --utc eller -u:

date --set HH:MM:SS --utc
Example 7. Changing the Current Time

To change the current time to 11:26 p.m., run the following command as root:

~]# date --set 20:59:56

Changing the Current Date

För att ändra aktuell datum, kör kommandot date med flaggan --set eller -s som root:

date --set ÅÅÅÅ-MM-DD

Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month.

Note that changing the date without specifying the current time results in setting the time to 00:00:00.

Example 8. Changing the Current Date

To change the current date to 2 June 2013 and keep the current time (11:26 p.m.), run the following command as root:

~]# date --set 2022-07-14 20:59:56

Att använda kommandot hwclock

hwclock är ett verktyg för att komma åt hårdvaruklockan, även känd som realtidsklockan (Real Time Clock, RTC). Hårdvaruklockan är oberoende av operativsystemen man använder och fungerar även när maskinen är avstängd. Detta verktyg används för att visa tiden från hårdvaruklockan. hwclock har också funktioner för att kompensera för systematisk avdrift i hårdvaruklockan.

Hårdvaruklockan lagrar värdena på: år, månad, dag, timme, minut och sekund. Den kan inte lagra tidsstandarden, lokaltid eller Coordinated Universal Time (UTC), eller ange sommartiden (Daylight Saving Time, DST).

Verktyget hwclock sparar sina inställningar i filen /etc/adjtime, vilken skapas när man gör sin första ändring, till exempel, när man ställer tiden manuellt eller synkroniserar hårdvaruklockan och systemtiden.

I Fedora 6 kördes kommandot hwclock automatiskt vid varje avstängning eller uppstart av systemet, men det görs inte i Fedora 31. När systemklockan synkroniseras av nätverkstidsprotokollet (Network Time Protocol, NTP) eller precisionstidsprotokollet (Precision Time Protocol, PTP) synkroniserar kärnan automatiskt hårdvaruklockan med systemklockan var 11:e minut.

För detaljer om NTP, se Att konfigurera NTP med sviten chrony och Att konfigurera NTP med ntpd. För information om PTP, se Att konfigurera PTP med ptp4l. För information om att ställa in hårdvaruklockan efter att ha kört ntpdate, se Att konfigurera uppdatering av hårdvaruklockan.

Displaying the Current Date and Time

Att köra hwclock utan några kommandoradsflaggor som användaren root returnerar datum och tid i lokal tid till standard ut.

hwclock

Observera att flaggorna --utc eller hwclock betyder inte att man visar hårdvaruklockans tid i UTC eller lokal tid. Dessa flaggor använd för att ställa in hårdvaruklockan till att ha tiden i endera av dem. Tiden visas alltid i lokal tid. Dessutom, att använda kommandona hwclock --utc eller hwclock --local ändrar inte den lagrade inställning i filen /etc/adjtime. Detta kommando kan vara användbart när man vet att den sparade inställningen i /etc/adjtime är felaktig men man inte vill ändra inställningen. Å andra sidan kan man få missledande information om man använder kommandot på fel sätt. Se manualsidan hwclock(8) för fler detaljer.

Example 9. Displaying the Current Date and Time

För att visa aktuellt datum och tid i den lokala tiden från hårdvaruklockan, kör som root:

~]# hwclock
2022-07-16 10:35:20.843111+02:00

CEST är en tidszonsförkortning och står för Central European Summer Time.

För information om hur man ändrar tidszonen, se Att ändra tidszonen.

Att ställa in datum och tid

Förutom att visa datum och tid kan man manuellt ställa hårdvaruklockan till en specifik tid.

När man behöver ändra hårdvaruklockans datum och tid kan man göra det genom att lägga till flaggorna --set och --date tillsammans med sin speicifikation:

hwclock --set --date "ÅÅÅÅ-mm-dd HH:MM"

Ersätt dd med en dag (ett tvåsiffrigt tal), mm med en månad (ett tvåsiffrigt tal), ÅÅÅÅ med ett år (ett fyrsiffrigt tal), HH med en timma (ett tvåsiffrigt tal), MM med en minut (ett tvåsiffrigt tal).

På samma gång kan man även ställa in hårdvaruklockan till att hålla tiden i antingen UTC eller lokal tid genom att lägga till flaggan --utc respektive --localtime. I så fall sparas UTC eller LOCAL i filen /etc/adjtime.

Example 10. Att ställa hårdvaruklockan till ett specifikt datum och tid

If you want to set the date and time to a specific value, for example, to "21:17, October 21, 2014", and keep the hardware clock in UTC, run the command as root in the following format:

~]# hwclock --set --date "2022-07-18 23:20" --utc

Att synkronisera datum och tid

Man kan synkronisera hårdvaruklockan och den aktuella systemtiden i båda riktningar.

  • Antingen kan man ställa hårdvaruklockan till den aktuella systemtiden genom att använda detta kommando:

    hwclock --systohc

    Observera att om man använder NTP synkroniserar hårdvaruklockan automatiskt med systemklockan var 11 minut, och detta kommando är endast användbart vid uppstart för att få en rimlig initial systemtid.

  • Eller så kan man ställa systemtiden från hårdvaruklockan genom att använda följande kommando:

    hwclock --hctosys

När man synkroniserar hårdvaruklockan och systemtiden kan man också specificera huruvida man vill hålla hårdvaruklockan i lokal tid eller UTC genom atat lägga till flaggan --utc eller --localtime. Likson när man använder --set kommer UTC ellerr LOCAL sparas i filen /etc/adjtime.

Kommandot hwclock --systohc --utc är funktionellt likvärdigt med kommandot timedatectl set-local-rtc false ock kommandot hwclock --systohc --local är ett alternativ till timedatectl set-local-rtc true.

Example 11. Att synkronisera hårdvaruklockan med systemtiden

För att ställa hårdvaruklockan till den aktuella systemtiden och hålla hårdvaruklockan i lokal tid, kör följande kommando som root:

~]# hwclock --systohc --localtime

För att undvika problem med byte av tidszon och sommartid rekommenderas det att hålla hårdvaruklockan till UTC. Det visade sättet Att synkronisera hårdvaruklockan med systemtiden är användbart, till exempel, i lägen med flervalsstart med ett Windows-system, vilket antar att hårdvaruklockan kör i lokal tid som standard, och då även alla andra system behöver anpassa sig till det genom att använda lokal tid. Det kan även behövas med en virtuell maskin; om den virtuella hårdvaruklockan som tillhandahålls av värden kör i lokal tid behöver gästsystem konfigureras att också använda lokal tid.

Ytterligare resurser

För mer information om hur man konfigurerar datum och tid i Fedora 31, se resurserna listade nedan.

Installerad dokumentation
  • timedatectl(1) — Manualsidan för kommandoradsverktyget timedatectl dokumenterar hur detta verktyg kan användas för att fråga om och ändra systemklockan och dess inställningar.

  • date(1) — Manualsidan för kommandot date ger en fullständig lista över kommandoradsargument som stödjs.

  • hwclock(8) — Manualsidan för kommandot date ger en fullständig lista över kommandoradsargument som stödjs.

Se även