NTP: Difference between revisions

imported>Tobias.bora
NTP and DHCP
Koalp (talk | contribs)
m Add link to chrony article
 
(7 intermediate revisions by 3 users not shown)
Line 20: Line 20:
* for '''ntpd''', use <code>services.ntp.enable = true;</code>
* for '''ntpd''', use <code>services.ntp.enable = true;</code>
* for '''openntpd''', use <code>services.openntpd.enable = true;</code>
* for '''openntpd''', use <code>services.openntpd.enable = true;</code>
* for '''chrony''', use <code>services.chrony.enable = true;</code>
* for '''[[chrony]]''', use <code>services.chrony.enable = true;</code>


== Set NTP from DHCP servers ==
== Set NTP from DHCP servers ==


Most of the time the default NTP servers will be reachable. However, sometimes the NTP servers can be blocked by the firewall (for instance most universities block NTP for security reasons). This will be visible in the <code>systemd-timesyncd.service</code>:
Most of the time the default NTP servers will be reachable. However, sometimes the NTP servers can be blocked by the firewall (for instance most universities block NTP for security reasons). This will be visible in the <code>systemd-timesyncd.service</code>:
<syntaxHighlight>
<syntaxHighlight lang=console>
$ sudo systemctl status systemd-timesyncd.service
$ sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
● systemd-timesyncd.service - Network Time Synchronization
Line 34: Line 34:


You can also test a specific server as follows:
You can also test a specific server as follows:
<syntaxHighlight>
<syntaxHighlight lang=console>
$ nix-shell -p ntp
$ nix-shell -p ntp
$ ntpdate -q 0.nixos.pool.ntp.org
$ ntpdate -q 0.nixos.pool.ntp.org
Line 41: Line 41:


However, a replacement NTP is usually provided in that case via the DHCP protocol. You can see it for instance if you use Network Manager:
However, a replacement NTP is usually provided in that case via the DHCP protocol. You can see it for instance if you use Network Manager:
<syntaxHighlight>
<syntaxHighlight lang=console>
$ nmcli connection show 'name-of-the-current-connection' | grep ntp
$ nmcli connection show 'name-of-the-current-connection' | grep ntp
DHCP4.OPTION[8]:                        ntp_servers = 192.168.x.y
DHCP4.OPTION[8]:                        ntp_servers = 192.168.x.y
Line 51: Line 51:


You might also be able to get the actual hostname of the DHCP (if it exists) using:
You might also be able to get the actual hostname of the DHCP (if it exists) using:
<syntaxHighlight>
<syntaxHighlight lang=console>
$ sudo nmap -sP 192.168.x.y
$ sudo nmap -sP 192.168.x.y
</syntaxHighlight>
</syntaxHighlight>
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> (this last option requires further configuration), and <code>networking.useNetworkd</code> is apparently experimental now (2022) according to its documentation.
<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:
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>
systemd.network.config = {
  dhcpV4Config = { UseNTP = false; };
  dhcpV6Config = { UseNTP = false; };
}
</syntaxHighlight>
but I have not tested it.
</li>
<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 110: Line 118:


To apply the change, switch to your new configuration and make sure to restart Network Manager:
To apply the change, switch to your new configuration and make sure to restart Network Manager:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=console>
$ sudo systemctl restart NetworkManager.service
$ sudo systemctl restart NetworkManager.service
# Check if the NTP server is used (may need to wait a few minutes)
</syntaxHighlight>
Check if the NTP server is used (may need to wait a few minutes):
<syntaxHighlight lang=console>
$ sudo systemctl status systemd-timesyncd.service
$ sudo systemctl status systemd-timesyncd.service
</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>
[[Category:Server]]
[[Category:Cookbook]]