Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


longest-match (Static Routes)


Hierarchy Level


Specify the static route on the device to resolve and determine the packet’s next-hop interface using the Longest Match Routing Rule (most specific entry), sometimes referred to as the longest prefix match or maximum prefix length match. The Longest Match Routing Rule is an algorithm used by IP routers to select an entry from a routing table. The router uses the longest (prefix) match to determine the egress (outbound) interface and the address of the next device to which to send a packet. Typically, the static route prefers the directly connected subnet route for resolving the next hop rather than performing a longest prefix match with any other available routes.


(Required) You must include the resolve next-hop option to specify the longest-match statement. Next-hop options define additional information about static routes that are included with the route when it is installed in the routing table. You alter the default next-hop resolution behavior using the resolve next-hop option.

The router implements the Longest Match Routing Rule as follows:

  1. The router receives a packet.

  2. While processing the header, the router compares the destination IP address, bit-by-bit, with the entries in the routing table.

The entry that has the longest number of network bits that match the IP destination address is always the best match (or best path) as shown in the following example:

Longest Match Example

  • The router receives a packet with a destination IP address of

  • The routing table contains the following possible matches:




To determine the longest match, it’s easiest to convert the IP addresses in Table 1 to binary and compare them.

Table 1: Converted IP Addresses


Converted Binary Address (destination IP address)


11000000.10101000.00000001.00100000 (<–Best match)




When determining the next-hop interface for customer deployments, setting the longest-match statement results in traffic loss.

Required Privilege Level

routing—To view this statement in the configuration.

routing-control—To add this statement to the configuration.

Release Information

Statement introduced in Junos OS Release 15.1.