AS prepending is used to artificially increase the length of the AS Path attribute. It is typically applied to outbound route advertisements to influence how traffic enters our network.
On Junos, AS Prepending is configured using a routing policy. We will see how to do this in the following examples.
Lab Environment
The lab is set up as per the below diagram. This is the same as the local preference example.

- vMX-1 and vMX-2 belong to AS 65002.
- vMX-3 belongs to AS 65001.
- iBGP is already configured between vMX-1 and vMX-2
- eBGP is already configured between vMX-2 and vMX-3.
- vMX-1 is advertising 172.16.101.0/24, 172.16.102.0/24, and 172.16.103.0/24 to vMX-2
- vMX-3 is advertising 172.17.201.0/24, 172.17.202.0/24, and 172.17.203.0/24 to vMX-2
Lab Baseline
On vMX-2 we have six BGP routes. Three routes are from vMX-1 and three from vMX-2. In the following examples, we will change the AS Path length for the routes vMX-2 advertises to vMX-3.
On vMX-3 we have three routes, learned from vMX-2. The AS path for all of these routes is 65002.
lab@vMX-3> show route protocol bgp
inet.0: 14 destinations, 14 routes (14 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
172.16.101.0/24 *[BGP/170] 00:24:55, localpref 100
AS path: 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.102.0/24 *[BGP/170] 00:24:55, localpref 100
AS path: 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.103.0/24 *[BGP/170] 00:24:55, localpref 100
AS path: 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
lab@vMX-3>
Example 1 – Prepending All Routes
In this first example, we will prepend all routes vMX-2 advertises to vMX-3 with an additional 2 AS entries, for a total path length of three.
As is normal practice, we use our own AS number when prepending.
The first step is to create a policy to do the prepending.
lab@vMX-2> edit
Entering configuration mode
[edit]
lab@vMX-2# set policy-options policy-statement my-prepend-policy term prepend-routes then as-path-prepend "65002 65002"
[edit]
lab@vMX-2# show policy-options policy-statement my-prepend-policy
term prepend-routes {
then as-path-prepend "65002 65002";
}
[edit]
lab@vMX-2#
There are a couple of things to note about this policy. The term prepend-routes does not have a match statement. This means it will match on all routes. It does not have a terminating action either and relies on the routes to be accepted by the default BGP policy.
Next, we need to apply the policy to our BGP peer. In this case, we will set the export policy at the group level.
[edit]
lab@vMX-2# set protocols bgp group my-external-group export my-prepend-policy
[edit]
lab@vMX-2# show protocols bgp
group my-external-group {
type external;
authentication-key "$9$7mdw2ZUH5Qn4aQn/CB17-V"; ## SECRET-DATA
export my-prepend-policy;
peer-as 65001;
neighbor 192.168.2.1;
}
group my-internal-group {
type internal;
local-address 172.16.1.2;
authentication-key "$9$GSjkmz39O1hfT1hSr8LGDi"; ## SECRET-DATA
export fix-nhs;
neighbor 172.16.1.1;
}
[edit]
lab@vMX-2# commit
commit complete
[edit]
lab@vMX-2#
Example 1 – Validation
We can validate the prepending by looking at the route table on vMX-3.
vMX-3
lab@vMX-3> show route protocol bgp
inet.0: 14 destinations, 14 routes (14 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
172.16.101.0/24 *[BGP/170] 00:01:27, localpref 100
AS path: 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.102.0/24 *[BGP/170] 00:01:27, localpref 100
AS path: 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.103.0/24 *[BGP/170] 00:01:27, localpref 100
AS path: 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
lab@vMX-3>
The prepending has worked, we can see the change in the AS Path length.
We can also check this on vMX-2 by looking at the routes we advertise to vMX-3.
lab@vMX-2> show route advertising-protocol bgp 192.168.2.1
inet.0: 20 destinations, 20 routes (20 active, 0 holddown, 0 hidden)
Prefix Nexthop MED Lclpref AS path
* 172.16.101.0/24 Self 65002 65002 [65002] I
* 172.16.102.0/24 Self 65002 65002 [65002] I
* 172.16.103.0/24 Self 65002 65002 [65002] I
lab@vMX-2>
In the AS path, you can see the first two are due to the prepending. The third in the square brackets represents the default behavior of adding an AS path when advertising the route over an eBGP session.
Example 2 – Prepending Specific Routes
We don’t necessarily want to prepend all routes. Sometimes we only want to prepend a few specific routes. We can match a route using a route filter as shown in this example. We will modify the existing policy to set prepends on the 172.16.102.0/24 route only.
lab@vMX-2> edit
Entering configuration mode
[edit]
lab@vMX-2# show policy-options policy-statement my-prepend-policy
term prepend-routes {
then as-path-prepend "65002 65002";
}
[edit]
lab@vMX-2# set policy-options policy-statement my-prepend-policy term prepend-routes from route-filter 172.16.102.0/24 exact
[edit]
lab@vMX-2# show policy-options policy-statement my-prepend-policy
term prepend-routes {
from {
route-filter 172.16.102.0/24 exact;
}
then as-path-prepend "65002 65002";
}
[edit]
lab@vMX-2# commit
commit complete
[edit]
lab@vMX-2#
Since the policy now has a match statement the prepending will only affect the 172.16.102.0/24 route.
Example 2 – Validation
We can validate this behavior like we did before, by looking at the routes on vMX-3 and the route advertisements on vMX-2.
vMX-3
lab@vMX-3> show route protocol bgp
inet.0: 14 destinations, 14 routes (14 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
172.16.101.0/24 *[BGP/170] 00:01:39, localpref 100
AS path: 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.102.0/24 *[BGP/170] 00:10:03, localpref 100
AS path: 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.103.0/24 *[BGP/170] 00:01:39, localpref 100
AS path: 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
lab@vMX-3>
Only the 172.16.102.0/24 route has the prepends on it.
vMX-2
lab@vMX-2> show route advertising-protocol bgp 192.168.2.1
inet.0: 20 destinations, 20 routes (20 active, 0 holddown, 0 hidden)
Prefix Nexthop MED Lclpref AS path
* 172.16.101.0/24 Self I
* 172.16.102.0/24 Self 65002 65002 [65002] I
* 172.16.103.0/24 Self I
lab@vMX-2>
The output on vMX-2 is a little different this time around. The 172.16.102.0/24 route looks the same but the others are different. Because we are not doing any prepending on these routes it doesn’t show the AS in square brackets for the non-prepended routes.
Example 3 – Effects Of Prepending In Multiple Terms Or Policies
What happens if we set prepends in multiple terms or policies? What do you think the following policy on vMX-2 will do to the routes advertised to vMX-3?
[edit]
lab@vMX-2# show policy-options policy-statement my-prepend-policy
term prepend-all-routes {
then as-path-prepend "65002 65002";
}
term prepend-specific-route {
from {
route-filter 172.16.102.0/24 exact;
}
then as-path-prepend "65002 65002";
}
[edit]
lab@vMX-2#
Think about this before reading on to see the results.
Example 3 – Validation
We see the following results on the routers.
vMX-3
lab@vMX-3> show route protocol bgp
inet.0: 14 destinations, 14 routes (14 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
172.16.101.0/24 *[BGP/170] 00:02:54, localpref 100
AS path: 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.102.0/24 *[BGP/170] 00:02:54, localpref 100
AS path: 65002 65002 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
172.16.103.0/24 *[BGP/170] 00:02:54, localpref 100
AS path: 65002 65002 65002 I, validation-state: unverified
> to 192.168.2.2 via ge-0/0/1.0
inet6.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)
lab@vMX-3>
vMX-2
lab@vMX-2> show route advertising-protocol bgp 192.168.2.1
inet.0: 20 destinations, 20 routes (20 active, 0 holddown, 0 hidden)
Prefix Nexthop MED Lclpref AS path
* 172.16.101.0/24 Self 65002 65002 [65002] I
* 172.16.102.0/24 Self 65002 65002 65002 65002 [65002] I
* 172.16.103.0/24 Self 65002 65002 [65002] I
lab@vMX-2>
Did you guess correctly that the 172.16.102.0/24 route would be prepended four times?
The reason this occurs is that our policy is not using any terminating actions.
As the routes are processed through the policy they are evaluated against the first term, which matches all routes and sets two prepends. Because there is no terminating action the routes are further evaluated by the next term.
The second term specifically matches the 172.16.102.0/24 route and adds another two prepends. Again, there is no terminating action so the routes continue to be evaluated.
Once the routes have been evaluated by all the terms in the policy, they start to be evaluated by the next policy. In this case, the next policy is the Junos default BGP policy which accepts all BGP routes.
Summary
Setting AS prepending on Junos is done via routing policy. As always, care must be taken to understand how a routing policy will impact routes to ensure you get the right outcome.
Now you know how to set prepending, you may like to read about why AS prepending doesn’t always work.
If you are new to BGP on Junos, have a look at this basic BGP configuration example.
great website, thank you for doing this.