Jump to content

NTP: Difference between revisions

87 bytes added ,  23 November 2022
no edit summary
imported>Tobias.bora
mNo edit summary
imported>Tobias.bora
No edit summary
Line 57: Line 57:
Then, you can of course manually add the DHCP server as explained above… but it's not practical when you often go to new places, or if the NTP server changes over time. For laptop it may not be a big issue (your internal clock will be used in that case until you reach a new network without firewalls) but for some devices like raspberry pi that can't store the time when device is turned off, or for servers that will always be behind the firewall, it can be interesting to automatically configure the NTP server using DHCP.
Then, you can of course manually add the DHCP server as explained above… but it's not practical when you often go to new places, or if the NTP server changes over time. For laptop it may not be a big issue (your internal clock will be used in that case until you reach a new network without firewalls) but for some devices like raspberry pi that can't store the time when device is turned off, or for servers that will always be behind the firewall, it can be interesting to automatically configure the NTP server using DHCP.


The procedure depends on how you configure internet. By default (if you have not installed network manager…) the [https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/tasks/network-interfaces-scripted.nix module <code>network-interfaces-scripted</code>] will be used… unfortunately as far as I know this script cannot deal with NTP.
The procedure depends on how you configure internet:
 
<ol>
If you use <code>systemd-networkd</code>, then it should automatically use the right NTP… However if you want the configuration to be automatic you want to use <code>networking.useNetworkd = true;</code> instead of <code>systemd.network.enable</code> (<code>useNetworkd</code> will automatically configure <code>systemd.network</code> to provide a good default experience as alone <code>systemd.network</code> does basically nothing). However, <code>networking.useNetworkd</code> is apparently experimental now (2022) according to its documentation, so use it at your own risks. As far as I understand you can also '''disable''' the default behavior using:
<li> '''By default''': (if you have not installed network manager…) the [https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/tasks/network-interfaces-scripted.nix module <code>network-interfaces-scripted</code>] will be used… unfortunately as far as I know this script cannot deal with NTP. </li>
<li> '''networkd''': If you use <code>systemd-networkd</code>, then it should automatically use the right NTP… However if you want the configuration to be automatic you want to use <code>networking.useNetworkd = true;</code> instead of <code>systemd.network.enable</code> (<code>useNetworkd</code> will automatically configure <code>systemd.network</code> to provide a good default experience as alone <code>systemd.network</code> does basically nothing). However, <code>networking.useNetworkd</code> is apparently experimental now (2022) according to its documentation, so use it at your own risks. As far as I understand you can also '''disable''' the default behavior using:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
systemd.network.config = {
systemd.network.config = {
Line 67: Line 68:
</syntaxHighlight>
</syntaxHighlight>
but I have not tested it.
but I have not tested it.
 
</li>
If you use Network Manager, then Network Manager cannot (in 2022) automatically configure the NTP as-it… but you can use this trick to fake it, by automatically creating a new connection-specific NTP configuration every time the connection changes and restarting <code>systemd-timesyncd.service</code>:
<li> '''Network Manager''' If you use Network Manager, then Network Manager cannot (in 2022) automatically configure the NTP as-it… but you can use this trick to fake it, by automatically creating a new connection-specific NTP configuration every time the connection changes and restarting <code>systemd-timesyncd.service</code>:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
Line 123: Line 124:
</syntaxHighlight>
</syntaxHighlight>
You might need to wait a one or two minutes as <code>systemd-timesyncd</code> may try to load other ntp servers, timeout, and then try the new NTP server.
You might need to wait a one or two minutes as <code>systemd-timesyncd</code> may try to load other ntp servers, timeout, and then try the new NTP server.
</li>
</ol>
Anonymous user