Of course! You are absolutely right on all points.
- NTP (Network Time Protocol) runs on UDP port 123.
- Since it's UDP, it's "connectionless," which makes checking it with tools like
nc a bit different than for a TCP service.
Here is a complete guide on how to check the connection, from the simple nc method you asked about to the more reliable, standard RHEL tools.
1. How to Check with nc (Netcat)
You can use nc to send a UDP packet to the server on port 123. This is a basic test to see if the port is open and reachable, but it doesn't fully validate the NTP service itself.
The Command:
nc -u -v <ntp_server_ip_or_hostname> 123
-u: Specifies UDP mode.
-v: Verbose mode, which gives you feedback.
How to Interpret the Output:
- Press Enter and type something: After running the command, your cursor will be blinking. Type some text (e.g., "test") and press
Enter.
- Wait for a response:
- If the command exits immediately or says "Connection refused", it's likely a local firewall on your RHEL server blocking the outgoing packet, or a network firewall immediately rejecting it.
- If the command hangs and you get no response, it means your packet was sent, but you received nothing back. This is the most common scenario and could mean:
- The remote server's firewall is dropping the packet.
- Your firewall is blocking the return packet.
- The NTP server is configured to ignore non-NTP-compliant packets (which is what
nc sends).
- If you get a "succeeded!" message and/or see some garbled binary output, it means you successfully sent a packet and received a reply. This is a strong indicator that the path is open.
Example of a successful test:
$ nc -u -v time.google.com 123
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to 216.239.35.0:123.
test <-- I typed 'test' and pressed Enter
^C <-- I had to press Ctrl+C to exit, but I didn't get an error, which is a good sign.
Key Takeaway for nc: It's a decent "is the network path open?" check, but it doesn't confirm the NTP service is working correctly. For that, you need a proper NTP client.
2. The Recommended RHEL Method: chrony
On modern RHEL systems (7, 8, 9), chrony is the default NTP daemon. It comes with a powerful command-line tool, chronyc, which is the best way to check the status.
Method A: The Best One-Off Test (like ntpdate)
If you want to test a server that is not yet in your configuration file, use this command. It performs a quick, single-shot query without changing your system clock.
sudo chronyd -Q 'server <ntp_server_ip_or_hostname> iburst'
chronyd -Q: Runs chronyd in a special one-off query mode.
server ... iburst: Specifies the server to test. The iburst option sends a quick burst of packets to speed up the initial synchronization test.
Successful Output:
2023-10-27T10:30:01Z chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG)
2023-10-27T10:30:01Z Initial frequency -1.234 ppm
2023-10-27T10:30:05Z System clock wrong by 0.000123 seconds (step)
2023-10-27T10:30:05Z chronyd exiting
The key is that the command completes successfully and often shows you the time offset.
Failed Output (e.g., firewall block):
The command will hang for a while and then time out with an error.
2023-10-27T10:31:01Z chronyd version 4.2 starting (...)
2023-10-27T10:32:15Z No suitable source for synchronisation
2023-10-27T10:32:15Z chronyd exiting
Method B: Checking Configured Servers
If the NTP server is already configured in /etc/chrony.conf, use this command to see its live status.
chronyc sources
Output Explained:
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* time.google.com 1 6 377 24 -125us[ -135us] +/- 5.4ms
^? ntp.example.com 0 8 0 - +0ns[ +0ns] +/- 0ns
^ (Mode): ^ indicates a server.
* (State): This is the most important part. * means it's the current, synchronized source.
? (State): Indicates a source that is unreachable or whose packets are failing validity tests. This means there is a connection problem.
x (State): A "false ticker" that chrony has determined is providing bad time.
Reach: This is a bitmask showing the results of the last 8 polls. 377 (octal) is 11111111 in binary, meaning all 8 were successful. 0 means all 8 failed. This is a very reliable indicator of a working connection.
3. The Old Method: ntpdate (Deprecated)
On older systems (RHEL 6 and earlier), or if you have it installed for legacy reasons, ntpdate is a simple tool.
# ntpdate might not be installed. Install with: sudo dnf install ntpdate
ntpdate -q <ntp_server_ip_or_hostname>
-q: Query only. It will not change the system clock.
Successful Output:
server 216.239.35.0, stratum 1, offset -0.000123, delay 0.01234
27 Oct 10:35:01 ntpdate[12345]: adjust time server ...
Failed Output:
27 Oct 10:36:01 ntpdate[12346]: no server suitable for synchronization found
4. Troubleshooting: Check the Firewall
If all checks fail, the most common culprit is the local firewall on your RHEL server.
# Check if the NTP client service is allowed
sudo firewall-cmd --list-services
# If 'ntp' is not in the list, you may need to add it, though outgoing
# connections are often allowed by default. This is more for allowing
# INCOMING requests if your server were also an NTP server.
# However, some strict firewalls require an explicit rule.
sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload
Summary / TL;DR
| Method | Use Case | Pros & Cons |
|---|
nc -u -v <ip> 123 | Quick & dirty check for network path/port reachability. | Con: Doesn't validate NTP protocol. Can give false sense of failure/success. |
chronyd -Q 'server <ip> iburst' | Best for a one-off test of an unconfigured server. | Pro: Uses the actual NTP protocol. Gives a definitive pass/fail. |
chronyc sources | Best for checking configured servers that are running. | Pro: Shows live sync status, reachability history, and stratum. |
ntpdate -q <ip> | For older systems or as a legacy fallback. | Pro: Simple. Con: Deprecated, may not be installed. |