LinuxCommandLibrary

nm-online

Check network connectivity status

TLDR

Find out whether the network is connected and print the result to stdout

$ nm-online
copy

Wait n seconds for a connection (30 by default)
$ nm-online --timeout [n]
copy

SYNOPSIS

nm-online [OPTIONS]
The most common usage involves specifying a timeout and optionally checking for full connectivity:
nm-online [-t SECONDS] [-q] [-s]

PARAMETERS

-t, --timeout=SECONDS
    Specifies the maximum number of seconds to wait for network connectivity. If the network is not online within this period, the command exits with a non-zero status. A value of 0 means to wait indefinitely.

-q, --quiet
    Suppresses all standard output messages, only returning an exit status.

-s, --check-connectivity
    Causes nm-online to wait until NetworkManager reports full Internet connectivity, as opposed to just an active connection. This involves NetworkManager's configured connectivity check mechanism (e.g., reaching a public URL).

-h, --help
    Shows a help message and exits.

-v, --version
    Shows version information and exits.

DESCRIPTION

nm-online is a command-line utility provided by NetworkManager that waits for an active network connection. It blocks execution until NetworkManager reports a successful network connection, or until a specified timeout period expires. This command is particularly useful in startup scripts, systemd service units, or any automated process where network availability is a prerequisite for subsequent operations.

It checks for different levels of connectivity: by default, it waits for a basic active connection; with the --check-connectivity option, it waits for full Internet connectivity as determined by NetworkManager's configured connectivity checks. Upon success, it exits with a zero status, indicating that the network is ready.

CAVEATS

nm-online relies entirely on NetworkManager's state and configuration. If NetworkManager is not running, or is misconfigured, nm-online will not function as expected. The --check-connectivity option's effectiveness depends on NetworkManager having a properly configured connectivity check URI; if not configured, it might not accurately reflect Internet reachability.

It does not guarantee application-level connectivity (e.g., DNS resolution might still fail, or a specific remote service might be down, even if the network is "online").

EXIT STATUS

nm-online returns the following exit codes:
0: The network connection is active and online (or connectivity check succeeded if --check-connectivity was used).
1: A timeout occurred before the network became online.
2: An error occurred (e.g., NetworkManager not running or D-Bus communication failed).

DEFAULT BEHAVIOR

By default, if nm-online detects that a connection is already active when it is started, it exits immediately with a success status (0). It does not wait unless an active connection is not found initially.

HISTORY

nm-online has been an integral part of the NetworkManager project since its early days, evolving with NetworkManager itself. Its primary purpose has always been to provide a reliable method for scripts and services to synchronize with the network's readiness state. As NetworkManager gained more sophisticated connectivity checking capabilities, nm-online also incorporated options like --check-connectivity to reflect these advancements, ensuring that services could wait for a higher assurance of network availability.

SEE ALSO

Copied to clipboard