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:
We need to create a custom policy routing table.
We need to create one or more custom policy routing rules.
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!
A Quick Introduction to Linux Policy Routing的更多相关文章
- zt-Simple source policy routing
原文地址: http://tldp.org/HOWTO/Adv-Routing-HOWTO/lartc.rpdb.simple.html Linux Advanced Routing & Tr ...
- [转帖]Introduction to Linux monitoring and alerting
Introduction to Linux monitoring and alerting https://www.redhat.com/sysadmin/linux-monitoring-and-a ...
- Introduction to Linux Threads
Introduction to Linux Threads A thread of execution is often regarded as the smallest unit of proces ...
- An Introduction to Laravel Policy
An Introduction to Laravel Policy 30 Dec 2018 . Laravel. 7.6K views If you heard about Laravel Polic ...
- A quick introduction to Source Insight for seamless development platform between Linux and Windows
前言 Source Insight是一个面向项目开发的程序编辑器和代码浏览器,它拥有内置的对C/C++, C#和Java等程序的分析.能分析源代码并在工作的同时动态维护它自己的符号数据库,并自动显示有 ...
- Quick Introduction to SQL Server Profiler
Introduction to Profiler SQL Server Profiler — or just Profiler — is a tool that can help monitor al ...
- A quick introduction to HTML
w3c reference : https://www.w3.org/TR/2014/REC-html5-20141028/introduction.html#writing-secure-appli ...
- A quick introduction to Google test
视频参考:Google C++ Testing GTest GMock Framework 为什么要使用 Google C++ Testing Framework? 使用这个框架有许多好理由.本文讨论 ...
- Queueing in the Linux Network Stack !!!!!!!!!!!!!!!
https://www.coverfire.com/articles/queueing-in-the-linux-network-stack/ Queueing in the Linux Networ ...
随机推荐
- jstorm集群部署
jstorm集群部署下载 Install JStorm Take jstorm-0.9.6.zip as an example unzip jstorm-0.9.6.1.zip vi ~/.bashr ...
- android Bundle savedInstanceState用途
经常会出现用户按到home键,退出了界面,或者安卓系统意外回收了应用的进程,这种情况下,使用Bundle savedInstanceState就可以用户再次打开应用的时候恢复的原来的状态 (以下转自: ...
- JQuery记住用户名和密码的具体实现
代码如下: //初始化页面时验证是否记住了密码 $(document).ready(function() { if ($.cookie("rmbUser") == "tr ...
- 利用oneproxy部署mysql数据库的读写分离
实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验说明:本实验共有4台主机,IP分配如拓扑 实验软件:mariadb-10.0.20 oneproxy-rhel ...
- [zz]如何在C语言程序中处理汉字
学习过C语言的人也许有时会遇到这样一个问题:如何用变量存储汉字以及对这些变量进行操作.目前许多C语言参考书中都没涉及到这个问题,程序中多为处理英文变量和英文字符串,涉及到汉字的情况也大都是在print ...
- [No00007D]2016-面经[上]
面试常见问题: 题一:"请你自我介绍一下" 思路:1.这是面试的必考题目.2.介绍内容要与个人简历相一致.3.表述方式上尽量口语化.4.要切中要害,不谈无关.无用的内容.5.条理要 ...
- 转: 在 Vim 中优雅地查找和替换 (写的很好,排版也是相当的赞)
http://harttle.com/2016/08/08/vim-search-in-file.html
- 原生js实现jquery库中选择器的功能(jquery库封装一)
今天是2017.1.1,新的一天,新的一年,新的一年里继续夯实基础知识,在工作中多些项目,多思考,多总结,前端是不断更新,在更新的过程中也是发现乐趣和挑战自我的过程,希望年轻的我和年轻的javascr ...
- codevs 2894 保留小数
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给你一个实数,小数点后不超过50位,请你保留到小数点后n位(四舍五入!!!) 输 ...
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 增加安全性增加内网的电脑才可以调用的限制
大型业务应用系统很容易受到各种攻击,每加上一道防护就可以减少80%-90%的攻击.我们的核心接口组件里也做了一些限制,有些接口方法只有内网的用户才可以调用,防止外网的用户调用,这样安全性会有很大的提升 ...