Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


Example: Configuring a Routing Policy for AS Path Prepending

This example shows how to configure a routing policy to prepend the AS path on specific routes advertised by BGP.


Before you begin, make sure your router interfaces and protocols are correctly configured. We provide the interface and BGP protocol configuration used in this document.


This example was updated and re-validated on Junos release 22.1R1.


In this example, you create a routing policy called prependpolicy1 and a term called prependterm1. The routing policy prepends AS number 65001 three times to routes that match the,, and prefixes, when the mask length is equal to or longer than the specified mask. The result is a match occurs when the route's mask length is equal to or longer than the specified network mask. The prependpolicy1 policy is applied as an export policy to the BGP routes advertised by R1 in AS 65001 to R2 in AS number 65000. Routes that don't match the specified prefix ranges do not undergo AS path prepending.



In the topology EBGP peering is configured between R1 and R2. Direct interface peering to the subnet addresses is used. R1 belongs to AS number 65001 and is configured to prepend its AS number to a specific set of matching routes when advertised to R2.

By adding AS numbers to the AS path the route becomes less likely to be selected for forwarding. This might be done by the owner of AS 65001 to reduce the amount of ingress traffic it receives from the operator of AS 65000.


In this example we demonstrate AS path prepending through an export policy. You can also use an import policy to match on routes for attribute manipulation. In general its a best practice to only prepend your local AS number to routes. Prepending AS numbers that belong to remote networks can lead to unexpected results.

For details on BGP paths selection see Understanding BGP Path Selection.



CLI Quick Configuration

In this section we focus on the configuration of the R1 device. Refer to the appendix for the complete configurations of all devices used in this example.

To quickly configure this example, copy the following commands, paste them into a text file, remove any line breaks, change any details necessary to match your network configuration, and then copy and paste the commands into the CLI at the [edit] hierarchy level.

In this example we assign three test prefixes to an unused interface on R1. A fourth test prefix is assigned to R1's loopback address. This provides four direct routes that can be advertised into BGP. Our policy uses a combination of protocol direct and route-filter statements to control which prefixes undergo AS path prepending.

Step-by-Step Procedure

The following steps requires you to navigate various levels in the configuration hierarchy. For instructions on how to do that, see Use the CLI Editor in Configuration Mode in the Junos OS CLI User Guide.

To create a routing policy that prepends AS numbers to specific routes:

  1. Configure the peering and loopback interfaces.

  2. Configure the AS number, RID, and the external BGP peer group. You define the prependpolicy1 policy in the next step. The policy is applied as an export policy to affect the routes advertised by R1.

  3. Configure the prependpolicy1 policy. The use of or-longer switch to the route filter statements allows a match when the mask length is equal to or longer than the specified mask. Other options like exact match only when the prefix and mask lengths are equal. The else term demonstrates how a route that does not match the prependterm1 term is advertised without AS path prepending by matching the else term.


    When you enter multiple AS numbers, you must separate each number with a space. Enclose the string of AS numbers in double quotation marks.

  4. Define test routes. In our sample topology we assign prefixes to an unused interface that is operationally up. This provides direct routes for BGP to advertise for testing the operation of the export policy.


Confirm your configuration by entering the show policy-options, show protocols bgp, show routing-options, and show interfaces commands from configuration mode. If the output does not display the intended configuration, repeat the configuration instructions in this example to correct it.

If you are done configuring the R1 device, enter commit from configuration mode.


To confirm that the configuration is working properly, perform these tasks:

Verifying the AS Prepending Policy


Verify that the policy is configured on the device, and that the appropriate routes are specified to prepend with AS numbers.


From operational mode, enter the show policy prependpolicy1 command.

The policy displays the correct match conditions and actions.

Verifying Routing Policy Application and BGP Peering


Verify the routing policy is applied as an export policy to the EBGP peer group. This step also confirms the BGP session to R2 is correctly established.


From operational mode, enter the show bgp neighbor command.

The command output confirms the BGP session is established and that R1 has applied the prependpolicy1 policy as export.

Verify AS Path Prepending


Verify the export policy works as design to prepend AS numbers to matching routes.


From operational mode, enter the show route protocol bgp command on R2. Alternatively, use the show route advertising-protocol bgp at R1 to display details about the routes it advertises to R2.

The routes show the expected AS path prepending. Note that the route only has one instance of AS number 65001. This route does not match the route filter statements in the prependterm1 of the prependpolicy1 policy and so does not undergo any prepending.

R1's view of the BGP routes it advertises to R2 is provided for completeness:

Appendix Full Configurations

The full configuration for R1.

The full configuration for R2.