Konfiguration av datum och tid
Moderna operativsystem skiljer mellan följande två typer av klockor:
-
En realtidsklocka (RTC, Real-Time Clock), vanligen benämnd som en hårdvaruklocka, (vanligtvis en integrerad krets på moderkortet) som är helt oberoende av det aktuella tillståndet för operativsystemet och går även när datorn är avstängd.
-
En systemklocka även känd som programvaruklocka, som hanteras utav kärnan och dess initiala värde baseras på realtidsklockan. När väl systemet startats och systemklockan initierats är programvaruklockan helt oberoende av realtidsklockan.
Systemtiden hålls alltid i Coordinated Universal Time (UTC) och konverteras i program till lokaltiden vid behov. Lokaltid är den faktiska tiden i ens aktuella tidszon, och tar hänsyn till sommartid (DST). Realtidsklockan kan antingen använda UTC eller lokaltid. UTC rekommenderas.
Fedora 32 erbjuder tre kommandoradsverktyg som kan användas för att konfigurera och visa information om systemets datum och tid: verktyget timedatectl, vilket är nytt i Fedora 32 och är en del av systemd
; det traditionella kommandot date och verktyget hwclock för att komma åt hårdvaruklockan.
Att använda kommandot timedatectl
Verktyget timedatectl distribueras som en del av system- och tjänstehanteraren systemd
och gör att man kan granska och ändra konfigurationen av systemklockan. Man kan använda detta verktyg för att ändra det aktuella datumet och den aktuella tiden, ställa in tidszonen eller aktivera automatisk synkronisering av systemklockan med en fjärrserver.
För information om hur man visar det aktuella datumet och den aktuella tiden, se även Att använda kommandot date.
Att visa det aktuella datumet och den aktuella tiden
För att visa aktuellt datum och tid tillsammans med detaljerad information om konfigurationen av systemet och hårdvaruklockan, kör kommandot timedatectl utan några ytterligare kommandoradsflaggor:
timedatectl
Detta visar den lokala och universella tiden, den nu valda tidszonen, statusen för konfiguration av nätverkstidsprotokollet (NTP
) ocy ytterligare information relaterat till sommartid.
Följande är en exempelutmatning från kommandot timedatectl på ett system som inte använder NTP
för att synkronisera systemklockan med en fjärrserver:
~]$ timedatectl Local time: Mon 2013-09-16 19:30:24 CEST Universal time: Mon 2013-09-16 17:30:24 UTC Timezone: Europe/Prague (CEST, +0200) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: yes Last DST change: DST began at Sun 2013-03-31 01:59:59 CET Sun 2013-03-31 03:00:00 CEST Next DST change: DST ends (the clock jumps one hour backwards) at Sun 2013-10-27 02:59:59 CEST Sun 2013-10-27 02:00:00 CET
Att ändra aktuell tid
För att ändra den aktuella tiden, skriv följande vid en skalprompt som root
:
timedatectl [option] set-time
HH:MM:SS
Ersätt HH med en timme, MM med en minut och SS med en sekund, allt skrivet med två siffror.
Detta kommando uppdaterar både systemtiden och hårdvaruklockan. Restultatet är snarlikt att använda de båda kommandona date --set och hwclock --systohhc.
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.
För att ändra aktuell tid till 23:26, kör följande kommando som root
:
~]# timedatectl set-time 23:26:00
Som standard konfigureras systemet till att använda UTC. För att konfigurera systemet till att hålla klockan i den lokala tiden, kör kommandot timedatectl med argumentet set-local-rtc
som root
:
timedatectl set-local-rtc
boolean
För att konfigurera systemet till att hålla klockan i den lokala tiden, ersätt boolean med yes
(eller, alternativt, y
, true
, t
eller 1
). För att konfigurera systemet till att använda UTC, ersätt boolean med no
(eller, alternativt, n
, false
, f
eller 0
). Standardalternativet är no
.
Att byta aktuellt datum
För att ändra det aktuella datumet, skriv följande vid en skalprompt som root
:
timedatectl set-time
ÅÅÅÅ-MM-DD
Ersätt ÅÅÅÅ med året med fyra siffror, MM med en tvåsiffrig månad och DD med en tvåsiffrig dag i månaden.
Observera att genom att ändra datumet utan att ange aktuell tid resulterar i att ställa tiden till 00.00.00.
För att ändra aktuellt datum till den 2 juni 2013 och behålla den aktuella tiden (23.26), kör följande kommando som root
:
~]# timedatectl set-time "2013-06-02 23:26:00"
Att ändra tidszonen
För att räkna upp alla tillgängliga tidszoner, skriv följande vid en skalprompt:
timedatectl list-timezones
För att ändra den använda tidszonen, skriv som root
:
timedatectl set-timezone tidszon
Ersätt tidszon med någon av de värden som räknades upp av kommandot timedatectl list-timezones.
För att identifiera vilken tidszon som är närmast till den aktuella platsen, använd kommandot timedatectl med kommandoradsflaggan list-timezones
. Till exempel, för att räkna upp alla tillgängliga tidszoner i Europa, skriv:
~]# timedatectl list-timezones | grep Europe Europe/Amsterdam Europe/Andorra Europe/Athens Europe/Belgrade Europe/Berlin Europe/Bratislava …
För att ändra tidszon till Europe/Stockholm
, skriv som root
:
~]# timedatectl set-timezone Europe/Stockholm
Att synkronisera systemklockan med en fjärrserver
Till skillnad mot de manuella justeringarna som beskrivs i det föregående avsnittet gör kommandot timedatectl även att man kan aktivera automatisk synkronisering av systemklockan med en grupp av fjärrservrar med protokollet NTP
. Att aktivera NTP aktiverar tjänsten chronyd
eller ntpd
, beroende på vilken av dem som är installerad.
Tjänsten NTP
kan aktiveras och avaktiveras med ett kommando enligt följande:
timedatectl set-ntp
boolean
För att aktivera att systemet synkroniserar systemklockan med en fjärr-NTP
-server, ersätt boolean med yes
(standardflaggan). För att avaktivera denna funktion, ersätt boolean mes no
.
För att aktivera automatisk synkronisering av systemklockan med en fjärrserver, skriv:
~]# timedatectl set-ntp yes
Kommandot kommer misslyckas om ingen NTP
-tjänst är installerad. Se Att installera chrony för mer information.
Att använda kommandot date
Verktyget date är tillgängligt på alla Linuxsystem och gör att man kan visa och konfigurera aktuellt datum och aktuell tid. Det används ofta i skript för att visa detaljerad information om systemklockan i ett anpassat format.
För information om hur man ändrar tidszonen eller aktiverar automatisk synkronisering av systemklockan med en fjärrserver, se Att använda kommandot timedatectl.
Att visa det aktuella datumet och den aktuella tiden
För att visa aktuellt datum och aktuell tid, kör kommandot date utan några ytterligare kommandoradsflaggor:
date
Detta visar veckodagen följt av aktuellt datum, lokaltid, förkortad tidszon och år.
Som standard visar kommandot date lokaltiden. För att visa tiden i UTC, kör kommandot med kommandoradsflaggan --utc
eller -u
:
date --utc
Man kan också anpassa formatet på den visade informationen genom att ge flaggan [option]`+"format"`på kommandoraden:
date +"format"
Replace format with one or more supported control sequences as illustrated in Displaying the Current Date and Time. See Commonly Used Control Sequences for a list of the most frequently used formatting options, or the date
(1) manual page for a complete list of these options.
Control Sequence | Description |
---|---|
|
The hour in the HH format (for example, |
|
The minute in the MM format (for example, |
|
The second in the SS format (for example, |
|
The day of the month in the DD format (for example, |
|
The month in the MM format (for example, |
|
The year in the YYYY format (for example, |
|
The time zone abbreviation (for example, |
|
The full date in the YYYY-MM-DD format (for example, |
|
The full time in the HH:MM:SS format (for example, 17:30:24). This option is equal to |
To display the current date and local time, type the following at a shell prompt:
~]$ date
Mon Sep 16 17:30:24 CEST 2013
To display the current date and time in UTC, type the following at a shell prompt:
~]$ date --utc
Mon Sep 16 15:30:34 UTC 2013
To customize the output of the date command, type:
~]$ date +"%Y-%m-%d %H:%M" 2013-09-16 17:30
Att ändra aktuell tid
To change the current time, run the date command with the --set
or -s
option as root
:
date --set
HH:MM:SS
Ersätt HH med en timme, MM med en minut och SS med en sekund, allt skrivet med två siffror.
By default, the date command sets the system clock to the local time. To set the system clock in UTC, run the command with the --utc
or -u
command line option:
date--set
HH:MM:SS--utc
För att ändra aktuell tid till 23:26, kör följande kommando som root
:
~]# date --set 23:26:00
Att byta aktuellt datum
To change the current date, run the date command with the --set
or -s
option as root
:
date --set
YYYY-MM-DD
Ersätt ÅÅÅÅ med året med fyra siffror, MM med en tvåsiffrig månad och DD med en tvåsiffrig dag i månaden.
Observera att genom att ändra datumet utan att ange aktuell tid resulterar i att ställa tiden till 00.00.00.
För att ändra aktuellt datum till den 2 juni 2013 och behålla den aktuella tiden (23.26), kör följande kommando som root
:
~]# date --set 2013-06-02 23:26:00
Using the hwclock Command
hwclock
is a utility for accessing the hardware clock, also referred to as the Real Time Clock (RTC). The hardware clock is independent of the operating system you use and works even when the machine is shut down. This utility is used for displaying the time from the hardware clock. hwclock
also contains facilities for compensating for systematic drift in the hardware clock.
The hardware clock stores the values of: year, month, day, hour, minute, and second. It is not able to store the time standard, local time or Coordinated Universal Time (UTC), nor set the Daylight Saving Time (DST).
The hwclock
utility saves its settings in the /etc/adjtime
file, which is created with the first change you make, for example, when you set the time manually or synchronize the hardware clock with the system time.
In Fedora 6, the hwclock command was run automatically on every system shutdown or reboot, but it is not in Fedora 32. When the system clock is synchronized by the Network Time Protocol (NTP) or Precision Time Protocol (PTP), the kernel automatically synchronizes the hardware clock to the system clock every 11 minutes. |
For details about NTP, see Configuring NTP Using the chrony Suite and Configuring NTP Using ntpd. For information about PTP, see Configuring PTP Using ptp4l. For information about setting the hardware clock after executing ntpdate, see Configuring the Hardware Clock Update.
Att visa det aktuella datumet och den aktuella tiden
Running hwclock with no command line options as the root
user returns the date and time in local time to standard output.
hwclock
Note that using the --utc
or --localtime
options with the hwclock command does not mean you are displaying the hardware clock time in UTC or local time. These options are used for setting the hardware clock to keep time in either of them. The time is always displayed in local time. Additionally, using the hwclock --utc or hwclock --local commands does not change the record in the /etc/adjtime
file. This command can be useful when you know that the setting saved in /etc/adjtime
is incorrect but you do not want to change the setting. On the other hand, you may receive misleading information if you use the command an incorrect way. See the hwclock
(8) manual page for more details.
To display the current date and the current local time from the hardware clock, run as root
:
~]# hwclock Tue 15 Apr 2014 04:23:46 PM CEST -0.329272 seconds
CEST is a time zone abbreviation and stands for Central European Summer Time.
For information on how to change the time zone, see Changing the Time Zone.
Setting the Date and Time
Besides displaying the date and time, you can manually set the hardware clock to a specific time.
When you need to change the hardware clock date and time, you can do so by appending the --set
and --date
options along with your specification:
hwclock --set --date "dd mmm yyyy HH:MM"
Replace dd with a day (a two-digit number), mmm with a month (a three-letter abbreviation), yyyy with a year (a four-digit number), HH with an hour (a two-digit number), MM with a minute (a two-digit number).
At the same time, you can also set the hardware clock to keep the time in either UTC or local time by adding the --utc
or --localtime
options, respectively. In this case, UTC
or LOCAL
is recorded in the /etc/adjtime
file.
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 "21 Oct 2014 21:17" --utc
Synchronizing the Date and Time
You can synchronize the hardware clock and the current system time in both directions.
-
Either you can set the hardware clock to the current system time by using this command:
hwclock --systohc
Note that if you use NTP, the hardware clock is automatically synchronized to the system clock every 11 minutes, and this command is useful only at boot time to get a reasonable initial system time.
-
Or, you can set the system time from the hardware clock by using the following command:
hwclock --hctosys
When you synchronize the hardware clock and the system time, you can also specify whether you want to keep the hardware clock in local time or UTC by adding the --utc
or --localtime
option. Similarly to using --set
, UTC
or LOCAL
is recorded in the /etc/adjtime
file.
The hwclock --systohc --utc command is functionally similar to timedatectl set-local-rtc false and the hwclock --systohc --local command is an alternative to timedatectl set-local-rtc true.
To set the hardware clock to the current system time and keep the hardware clock in local time, run the following command as root
:
~]# hwclock --systohc --localtime
To avoid problems with time zone and DST switching, it is recommended to keep the hardware clock in UTC. The shown Synchronizing the Hardware Clock with System Time is useful, for example, in case of a multi boot with a Windows system, which assumes the hardware clock runs in local time by default, and all other systems need to accommodate to it by using local time as well. It may also be needed with a virtual machine; if the virtual hardware clock provided by the host is running in local time, the guest system needs to be configured to use local time, too.
Additional Resources
For more information on how to configure the date and time in Fedora 32, see the resources listed below.
-
timedatectl
(1) — The manual page for the timedatectl command line utility documents how to use this tool to query and change the system clock and its settings. -
date
(1) — The manual page for the date command provides a complete list of supported command line options. -
hwclock
(8) — The manual page for the hwclock command provides a complete list of supported command line options.
-
System Locale and Keyboard Configuration documents how to configure the keyboard layout.