post image for subnetting

How to Set Up Static and Dynamic Routing on MikroTik

Routing is the backbone of any network—ensuring that data is forwarded from one network to another in the most efficient manner possible. MikroTik RouterOS provides powerful routing capabilities for both small and large-scale enterprise environments. In this guide, we’ll cover how to configure both static and dynamic routing on MikroTik devices using practical examples and detailed explanations. Whether you’re a network engineer or a systems administrator, this article will help you understand the fundamentals, configuration best practices, and troubleshooting techniques to achieve a robust routing setup.

Table of Contents


1. Understanding Routing in MikroTik

At its core, routing is the process of selecting paths to forward data packets between different networks. MikroTik RouterOS supports both static routing—where routes are manually defined—and dynamic routing—where routes are learned and updated automatically using routing protocols. Understanding the differences and knowing when to use each type is crucial:

  • Static Routing: Best suited for small or stable networks where paths rarely change. It provides complete control over the routing decisions, but requires manual updates when the network topology changes.
  • Dynamic Routing: Ideal for larger or more complex networks. Protocols such as OSPF and BGP automatically exchange routing information between routers, ensuring rapid adaptation to topology changes and supporting load balancing and redundancy.

In MikroTik RouterOS, all routing information is maintained in the routing table (RIB – Routing Information Base) and the FIB (Forwarding Information Base) that the router uses to make packet-forwarding decisions. The choice between static and dynamic routing can affect network performance, scalability, and resiliency.


2. Setting Up Static Routing in MikroTik

Static routes are manually defined routes that specify a fixed path for traffic destined for a particular network. They are simple to implement and offer predictable behavior—making them ideal for small networks, backup routes, or scenarios where full control over routing decisions is required.

2.1. When and Why to Use Static Routing

  • Small Networks or Backup Paths: In networks where the topology is simple or changes infrequently, static routes are often easier to manage than dynamic protocols.
  • Security and Control: By manually configuring routes, you have full control over the traffic flow. This can be especially useful in segmented environments or where sensitive data is involved.
  • Failover Scenarios: Static routes can serve as backup routes when dynamic protocols fail or during maintenance windows.

2.2. How to Configure Static Routes in MikroTik

You can configure static routes using either the command-line interface (CLI) or the graphical interface (WinBox). Below are step-by-step examples for both methods.

Using CLI

To add a simple static route directing traffic for the subnet 192.168.2.0/24 via the gateway 192.168.1.1, enter:

/ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1

You can verify the configuration by printing the routing table:

/ip route print

Using WinBox

  1. Open WinBox and log in to your MikroTik router.
  2. Navigate to IP > Routes.
  3. Click the Add (+) button to open the “New Route” window.
  4. Enter the following details:
    • Destination: 192.168.2.0/24
    • Gateway: 192.168.1.1
  5. Click OK to save the route.

Example scenario: Imagine you have two local networks, 192.168.1.0/24 and 192.168.2.0/24, connected by a MikroTik router. Configuring a static route ensures that devices on the 192.168.1.0/24 network can reach devices on the 192.168.2.0/24 network using the specified gateway.

2.3. Advanced Static Routing Techniques

For more complex scenarios, such as multiple gateways or policy routing, static routing can be extended using additional parameters:

  • Multiple Static Routes: You may need to define more than one static route for backup or load balancing purposes. In these cases, you can set different distance values. A lower distance gives higher priority.
    /ip route add dst-address=10.0.10.0/24 gateway=10.0.0.2 distance=1
    /ip route add dst-address=10.0.10.0/24 gateway=10.0.0.3 distance=2
  • Routing Tables and Policy Routing: You might want to direct traffic from a specific source IP or subnet through a particular gateway. This is done by creating separate routing tables and using routing rules:
    /routing table add name=VPN_Routes fib
    /ip route add dst-address=0.0.0.0/0 gateway=10.10.10.1 routing-table=VPN_Routes
    /routing rule add src-address=192.168.100.0/24 action=lookup-only-in-table table=VPN_Routes
  • Mangle Rules: For even finer control, you can mark packets in the mangle chain and then use those marks to direct traffic. This is useful when multiple subnets require different routing behaviors.
    /ip firewall mangle add chain=prerouting src-address=192.168.100.0/24 action=mark-routing new-routing-mark=VPN_Route

    Then create a route that applies to traffic with the VPN_Route mark:

    /ip route add dst-address=0.0.0.0/0 gateway=10.10.10.1 routing-mark=VPN_Route

3. Setting Up Dynamic Routing in MikroTik

Dynamic routing protocols automatically learn and update routes as network topologies change. MikroTik supports several dynamic routing protocols such as RIP, OSPF, and BGP. In this section, we’ll focus on OSPF (a common IGP for enterprise networks) and BGP (typically used for inter-domain or ISP-level routing).

3.1. Overview of Dynamic Routing Protocols

  • RIP: A distance-vector protocol that uses hop count as its metric. While simple, it is generally suitable only for smaller networks (maximum hop count of 15).
  • OSPF (Open Shortest Path First): A link-state protocol that calculates the best path using the Shortest Path First (SPF) algorithm. OSPF supports hierarchical design by dividing networks into areas and is widely used in enterprise networks.
  • BGP (Border Gateway Protocol): A path-vector protocol used for routing between autonomous systems (AS). BGP is essential for Internet-scale routing and for organizations with multiple public IP blocks.

3.2. Configuring OSPF in MikroTik

OSPF is the most common dynamic routing protocol used in enterprise environments. It uses a link-state algorithm to ensure that all routers have a complete picture of the network topology.

OSPF Configuration via CLI

Below is an example of a basic single-area OSPF configuration. In this example, we assume a backbone area (Area 0) where networks 192.168.1.0/24 and 192.168.2.0/24 are connected to the router.

# Create an OSPF instance with a router ID
/routing ospf instance add name=default router-id=1.1.1.1

# Define the backbone area
/routing ospf area add name=backbone area-id=0.0.0.0

# Advertise the networks in OSPF
/routing ospf network add network=192.168.1.0/24 area=backbone
/routing ospf network add network=192.168.2.0/24 area=backbone

After configuration, you can verify the OSPF neighbors and database with:

/routing ospf neighbor print
/routing ospf lsa print

OSPF Configuration via WinBox

  1. Open WinBox and log in to your MikroTik router.
  2. Go to Routing > OSPF.
  3. In the Instances tab, click the Add (+) button to create a new OSPF instance. Enter a name (for example, default) and a router ID (e.g., 1.1.1.1), then click OK.
  4. Switch to the Areas tab, click Add (+), and configure an area:
    • Area ID: 0.0.0.0 (for the backbone area)
    • Name: backbone
  5. Next, go to the Networks tab and add the networks to be advertised:
    • Click Add (+) and enter the network 192.168.1.0/24 and select area backbone.
    • Click Add (+) again for 192.168.2.0/24.

With OSPF configured, routers within the same area will exchange link-state information and build a consistent routing table using the SPF algorithm.

3.3. Configuring BGP in MikroTik

Border Gateway Protocol is used when you need to exchange routing information with external networks or between multiple autonomous systems. BGP configuration can be more complex, but it offers powerful features for route filtering, policy-based routing, and path control.

BGP Configuration via CLI

Below is an example of a basic BGP configuration. In this scenario, Router A (AS 65001) peers with Router B (AS 65002) over an interface. Adjust the IP addresses and AS numbers to match your environment.

# Create a BGP instance for Router A
/routing bgp instance add name=default as=65001 router-id=1.1.1.1

# Add a BGP peer for Router B
/routing bgp peer add name=peer-to-routerB remote-address=10.0.0.2 remote-as=65002

# Advertise a network (e.g., local LAN)
/routing bgp network add network=192.168.1.0/24

You can verify the BGP session status with:

/routing bgp peer print

BGP Configuration via WinBox

  1. Log in to WinBox and navigate to Routing > BGP.
  2. In the Instances tab, create a new BGP instance by clicking Add (+) and setting:
    • AS Number: 65001
    • Router ID: 1.1.1.1
  3. In the Peers tab, click Add (+) to add a new peer:
    • Name: peer-to-routerB
    • Remote Address: 10.0.0.2
    • Remote AS: 65002
  4. In the Networks tab, add the local network that you wish to advertise, such as 192.168.1.0/24.

BGP is particularly useful when your network spans multiple sites or when you have multiple Internet connections. It also allows you to enforce routing policies and filter routes according to your business requirements.

3.4. Dynamic Routing with Route Redistribution

In some cases, you may need to mix static routes with dynamic routing protocols. For example, you might have a static backup route that should only be used if dynamic routing fails. MikroTik allows you to redistribute connected, static, or even routes learned via one dynamic protocol into another. Here’s an example of redistributing static routes into OSPF:

# Redistribute static routes into OSPF
/routing ospf instance set default redistribute-static=yes

With redistribution enabled, any static route in your routing table will be injected into OSPF and vice versa. Use this feature with caution, as improper redistribution can lead to routing loops or suboptimal routing.


4. Routing Best Practices for MikroTik Networks

Designing a robust routing architecture in MikroTik involves more than just configuring routes. Consider the following best practices:

4.1. Combine Static and Dynamic Routing Effectively

  • Use Static Routes for Simple or Critical Paths: For small networks or for backup routes that require absolute control, use static routes. For example, reserve a static default route for your Internet gateway.
  • Implement Dynamic Routing for Scalability: For complex or larger networks, rely on OSPF or BGP to automatically adjust to topology changes.
  • Redistribution with Caution: If you need to redistribute routes between protocols, always test in a lab environment first.

4.2. Design for High Availability and Load Balancing

  • Use Multiple Gateways: If you have redundant Internet connections, configure multiple default routes with different distances to ensure failover.
    /ip route add dst-address=0.0.0.0/0 gateway=ISP1_IP distance=1
    /ip route add dst-address=0.0.0.0/0 gateway=ISP2_IP distance=2
  • Implement ECMP: For equal-cost multipath (ECMP) routing, ensure that multiple routes with the same destination and cost are installed. MikroTik automatically groups these routes, providing load balancing.
  • Use Routing Tables and Rules: For advanced scenarios (e.g., policy-based routing), create separate routing tables and define routing rules to control traffic flow based on source or destination addresses.
    /routing table add name=VPN_Routes fib
    /ip route add dst-address=0.0.0.0/0 gateway=VPN_Gateway routing-table=VPN_Routes
    /routing rule add src-address=192.168.200.0/24 action=lookup-only-in-table table=VPN_Routes

4.3. Secure and Monitor Your Routing Setup

  • Enable Logging: Use MikroTik’s logging facilities to monitor routing events. For example, log OSPF neighbor changes:
    /system logging add topics=ospf,route action=memory
  • Regularly Check the Routing Table: Periodically review the routing table with /ip route print to ensure that routes are correctly installed.
  • Implement Firewall Filters: Combine routing with firewall rules to control traffic flow and protect your network. For example, if you use mangle rules to mark traffic, ensure that fasttrack rules do not interfere.
  • Document Your Configuration: Maintain detailed documentation of your routing policies, static routes, and dynamic routing configurations.

4.4. Use Loopback Interfaces for Stability

It is best practice to configure a loopback interface on each router and use its IP address as the router ID for OSPF and BGP. This ensures that the router remains reachable even if a physical interface goes down.

# Create a loopback interface and assign a /32 address (for IPv4)
/interface bridge add name=loopback
/ip address add address=10.10.10.10/32 interface=loopback
/routing ospf instance set default router-id=10.10.10.10

5. Troubleshooting Common Routing Issues

Even with careful planning, issues can arise. Here are some common problems and tips on how to troubleshoot them:

5.1. Verifying Route Installation

  • Print the Routing Table: Use /ip route print to see which routes are active. Look for flags such as “A” (active) and “D” (dynamic).
  • Check Interface Status: Ensure that interfaces used for routing (e.g., WAN, LAN, VPN) are up and correctly configured.
  • Examine Neighbor Relationships: For dynamic routing, use /routing ospf neighbor print or /routing bgp peer print to confirm that sessions are established.

5.2. Debugging OSPF

  • Use the OSPF LSA Database: Inspect the OSPF database with /routing ospf lsa print to ensure that link-state advertisements (LSAs) are being exchanged correctly.
  • Monitor OSPF Logs: Increase logging for OSPF events to diagnose issues:
    /system logging add topics=ospf,ospf-discovery action=memory
  • Verify Cost and Priority Settings: Make sure that OSPF interface templates are configured with the proper cost and priority to ensure correct DR/BDR election and route selection.

5.3. Debugging BGP

  • Check BGP Peer Status: Use /routing bgp peer print to see if peers are established and routes are being exchanged.
  • Verify Advertised Networks: Confirm that the networks you expect to be advertised are visible with /routing bgp network print.
  • Use BGP Filters: If certain routes are not being advertised as expected, check your inbound/outbound filters.

5.4. Troubleshooting Static Routes

  • Gateway Reachability: Confirm that the gateway specified in the static route is reachable. Use ping and traceroute from the router.
  • Interface Bindings: Verify that the correct interface is bound to the static route, especially if you have multiple WAN or VPN interfaces.
  • Recursive Routing Issues: If using recursive routes (where the gateway is not directly connected), ensure that there is a route for the next hop.

5.5. Common Pitfalls and How to Avoid Them

  • Mismatched Network Masks: Ensure that the network statements in dynamic routing protocols (like OSPF) match the actual IP addresses on interfaces.
  • Overlapping Routes: Be cautious when redistributing routes between protocols to avoid routing loops.
  • Incorrect Cost/Distance Values: When using multiple routes for load balancing or failover, set the correct distance (static routes) or cost (OSPF) values to prioritize routes properly.
  • FastTrack Interference: When using mangle rules for policy routing, ensure that fasttrack rules do not inadvertently bypass your marked packets.

6. Conclusion

In this guide, we explored how routing is the core of network communications and how MikroTik RouterOS enables both static and dynamic routing configurations to suit diverse network environments. We began by understanding the fundamental differences between static and dynamic routing, walked through detailed configuration examples for both methods using CLI and WinBox, and discussed advanced techniques such as route redistribution, policy routing with separate routing tables, and the use of mangle rules.

We then reviewed best practices for designing robust and secure networks—including using loopback interfaces, prioritizing routes for high availability, and properly monitoring routing performance. Finally, we delved into troubleshooting tips to help diagnose and resolve common routing issues in MikroTik networks.

Whether you’re setting up a small office network or managing a large-scale enterprise environment, understanding and configuring routing on MikroTik devices is essential for achieving efficient and reliable network communications. Experiment with these configurations in your lab environment, and gradually tailor them to your network’s needs. As you grow more comfortable with both static and dynamic routing, you’ll be better prepared to optimize performance, secure your network, and troubleshoot any issues that arise.

Next Steps: Continue your learning journey by exploring advanced routing topics such as MPLS, VRF, and dynamic policy routing on MikroTik. The MikroTik Wiki and community forums are excellent resources for further study.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *