A Quick Introduction to Linux Policy Routing

A Quick Introduction to Linux Policy Routing

29 May 2013

In this post, I’m going to introduce you to policy routing as implemented in recent versions of Ubuntu Linux (and possibly other Linux distributions as well, but I’ll be using Ubuntu 12.04 LTS). Policy routing actually allows us a great deal of flexibility in how we direct traffic out of a Linux host; I’ll discuss a rather practical application of this configuration in a future blog post. For now, though, let’s just focus on how to configure policy routing.

There are a couple parts involved in policy routing:

  • Policy routing tables: Linux comes with three by default: local (which cannot be modified or deleted), main, and default. Somewhat unintuitively, routes added to the system without a routing table specified go to the main table, not the default table.
  • Policy routing rules: Again, Linux comes with three rules, one for each of the default routing tables.

In order for us to leverage policy routing for our purposes, we need to do three things:

  1. We need to create a custom policy routing table.
  2. We need to create one or more custom policy routing rules.
  3. We need to populate the custom policy routing table with routes.

Let’s look at each of these steps separately.

Creating a Custom Policy Routing Table

The first step is to create a custom policy routing table. Each table is represented by an entry in the file /etc/iproute2/rt_tables, so creating a new table is generally accomplished using a command like this:

echo 200 custom >> /etc/iproute2/rt_tables

This creates the table with the ID 200 and the name “custom”. You’ll reference this name later as you create the rules and populate the table with routes, so make note of it. Because this entry is contained in the rt_tables file, it will be persistent across reboots.

Creating Policy Routing Rules

The next step is to create the policy routing rules that will tell the system which table to use to determine the correct route. In this particular case, I’m going to use the source address (i.e., the originating address for the traffic) as the determining factor in the rule. This is a common application of policy routing, and for that reason it’s often referred to as source routing.

To create the policy routing rule, use this command:

ip rule add from <source address> lookup <table name>

Let’s say that we wanted to create a rule that told the system to use the “custom” table we created earlier for all traffic originating from the source address 192.168.30.200. The command would look like this:

ip rule add from 192.168.30.200 lookup custom

You can see all the policy routing rules that are currently in effect using this command:

ip rule list

As I mentioned in the beginning of this article, there are default rules that govern the use of the local, main, and default tables (these are the built-in tables). Once you’ve added your rule, you should see it listed there as well.

There is a problem here, though: rules created this way are ephemeral and will disappear when the system is restarted (or when the networking is restarted). To make the rules persist, add a line like this to /etc/network/interfaces:

post-up ip rule add from 192.168.30.200 lookup custom

You’d want to place this line in the configuration stanza that configures the interface with the address 192.168.30.200. With this line in place, the rule should persist across reboots or across network restarts.

Populating the Routing Table

Once we have the custom policy routing table created and a rule defined that directs the system to use it, we need to populate the table with the correct routes. The generic command to do this is the ip route add command, but with a specific table parameter added.

Using our previous example, let’s say we wanted to add a default route that was specific to traffic originating from 192.168.30.200. We’ve already created a custom policy routing table, and we have a rule that directs the system to use that table for traffic originating from that address. To add a new default route specifically for that interface, you’d use this command:

ip route add default via 192.168.30.1 dev eth1 table custom

Naturally, you’d want to substitute the correct default gateway for 192.168.30.1 and the correct interface for eth1 in the above command, but this should give you the right idea. Of course, you don’t have to use default routes; you could install specific routes into the custom policy routing table as well. This also works on VLAN sub-interfaces, so you could create per-VLAN routing tables:

ip route add default via 192.168.30.1 dev eth0.30 table vlan30

This command installs a default route for the 192.168.30.x interface on VLAN 30, using a table named “vlan30” (note that the table needs to created before you can add routes to it, as far as I can tell).

As with the policy routing tables, routes added this way are not persistent, so you’ll want to make them persistent by adding a line like this to your/etc/network/interfaces configuration file:

post-up ip route add default via 192.168.30.1 dev eth1 table custom

This will ensure that the appropriate routes are added to the appropriate policy routing table when the corresponding network interface is brought up.

Summary

There’s a great deal more functionality possible in policy routing, but this at least gives you the basics you need to understand how it works. In a future post, I’ll provide a specific use case where this functionality could be put to work. In the meantime, feel free to share any corrections, clarifications, questions, or thoughts in the comments below.

Tags: CLI · Linux · Networking · Ubuntu Previous Post: VLAN Trunking to Guest Domains with Open vSwitchNext Post: A Use Case for Policy Routing with KVM and Open vSwitch

Be social and share this post!

时间: 2024-08-28 15:52:55

A Quick Introduction to Linux Policy Routing的相关文章

Introduction to Linux

Part 1: Introduction 1. Linux Evolution and Popular Operating Systems The definition of the word Linux depends on the context in which it is used. Linux means the kernel of the system, which is the central controller of everything that happens on the

文件(file)1————An introduction to Linux filesystems

这里讲的主要是关于文件,目录以及一部分磁盘的相关知识.主要依据是操作系统第4章 文件管理(参考书:王道的书)和鸟哥的私房菜的第567章.  在系统运行时,计算机以进程为基本单位进行资源的调度和分配:而在用户进行的输入.输出中,则以文件为基本单位. 1.什么是文件? 从比喻的角度来讲,操作系统是图书管理员对于书的管理,则文件就是图书馆的书. 从底层向上的角度来讲,数据项:用于描述一个对象的某种属性值,如姓名,日期或证件号(注意这三个例子中的一个就是数据项)   --->记录:一组相关数据项的集合,

A quick introduction to Source Insight for seamless development platform between Linux and Windows

前言 Source Insight是一个面向项目开发的程序编辑器和代码浏览器,它拥有内置的对C/C++, C#和Java等程序的分析.能分析源代码并在工作的同时动态维护它自己的符号数据库,并自动显示有用的上下文信息.越是大规模的代码,越能显示出SI的强大之处.Linux服务器开发领域,很多时候代码往往是从Linux主机上copy一份到Windows,然后SI为其创建项目.代码在Windows上修改好后,用WinSCP上传到Linux主机替换源代码,挥着使用Beyond Compare对比合并代码

A quick introduction to HTML

w3c reference : https://www.w3.org/TR/2014/REC-html5-20141028/introduction.html#writing-secure-applications-with-html HTML user agents (e.g. Web browsers) then parse this markup, turning it into a DOM (Document Object Model) tree. A DOM tree is an in

Beginners Level Course:Introduction to Linux

欢迎来到"开启Linux之旅:入门教程".如果你没有接触过Linux并且想知道怎么使用在今天这个成长速度最快的操作系统,所有你需要做的是跟随接下来的课程,很快你就能高效的使用Linux. 开启Linux之旅:入门教程是一个自学教程.是这个课程变得与众不同的是在课程的任何地方你都可以添加笔记或者评论.在屏幕右侧的评论栏可以做到这些.这些评论可以是公开的,也可以是不公开的,并且可以是你喜欢的任何形式.可以自由的使用它们提问.回答其他人的问题.粘贴更新或者提出解决问题的不同方法. Linux

Linux 基于策略的路由

Linux 基于策略的路由(Linux Policy Routing) Linux 有传统的基于数据包目的地址的路由算法,和新的基于策略的路由算法新算法优点:支持多个路由表,支持按数据报属性(源地址.目的地址.协议.端口.数据包大小.内容等)选择不同路由表 # 查看规则命令,后面可跟其它参数,默认为 show(list) 显示全部ip rule 系统默认有3条记录0: from all lookup local32766: from all lookup main32767: from all

Linux Ethernet Bonding Driver HOWTO 英文原版

Linux Ethernet Bonding Driver HOWTO Latest update: 12 November 2007 Initial release : Thomas Davis <tadavis at lbl.gov> Corrections, HA extensions : 2000/10/03-15 : - Willy Tarreau <willy at meta-x.org> - Constantine Gavrilov <const-g at xp

马哥2016全新Linux+Python高端运维班-Linux 网络基础管理,包管理,bash脚本练习

本周作业内容: 1.请描述网桥.集线器.二层交换机.三层交换机.路由器的功能.使用场景与区别.     集线器:又称"HUB"主要功能是对接收到的信号进行再生整形放大,以扩大网络的传输距离,同时把所有节点集中在以它为中心的节点上.它工作于OSI(开放系统互联参考模型)参考模型第一层,即"物理层".     二层交换机:工作于OSI模块的第2层(数据链路层),故称为二层交换机.可以识别数据包中的MAC地址信息,根据MAC地址进行转发,并将这些MAC地址与对应的端口记录

Linux入门之CentOS7内核编译三部曲(1)

Linux入门之CentOS7内核编译三部曲(1) 我们知道,一个Linux系统的主要组成是由liunx内核核心和一些支持模块组合而成的.但是在某些场合中,需要某项功能,而当前内核的核心或者模块不支持此功能,那么就需要对内核进行一个升级或者重新编译内核添加相应的功能,以此提供了对此功能的支持. 编译前的准备 认识kernel 所为kernel,就是一种操作系统的核心,当然也是一个文件,而这种核心提供了对一些硬件的支持,一般来说其中包含了一些对常见硬件核心驱动的核心代码.启动系统时会通过加载MBR