大网数据广告活动
服务器运维问答

list all route tables

· 52CE 我爱测

I need to know how to list the IDs of all route tables. For example, I can run: ip rule add fwmark 2 table 104 ip route add dev eth0 default via 192.168.3.7 table 104 A call to ip rule list shows: 0: from all lookup local 32765: …

问题原文

原帖链接 · nic · CC BY-SA 3.0

I need to know how to list the IDs of all route tables. For example, I can run:

ip rule add fwmark 2 table 104
ip route add dev eth0 default via 192.168.3.7 table 104

A call to ip rule list shows:

0:  from all lookup local 
32765:  from all fwmark 0x2 lookup 104 
32766:  from all lookup main 
32767:  from all lookup default

And a call to ip route show table 104 shows:

default via 192.168.3.7 dev eth0

If I then call ip rule del table 104, a subsequent call to ip rule list shows:

0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default

However, a call to ip route show table 104 still shows:

default via 192.168.3.7 dev eth0

I know that I can flush the table using ip route flush table 104. I'd like to be able to flush all tables that are not local, main, and default. Thus I want to be able to list the existing tables.

I've seen people use cat /etc/iproute2/rt_tables, but that only produces:

#
# reserved values
#
255 local
254 main
253 default
0   unspec
#
# local
#
#1  inr.ruhep

What can I do to get all the table names that currently exist? Thanks in advance!

社区高分解答

原帖链接 · larsks · CC BY-SA 3.0

What can I do to get all the table names that currently exist?

The file /etc/iproute2/rt_tables is the only source of table names on the system. Internally, routing tables have integer identifiers.

The rest of your question is a bit confusing. If a table is not referenced by a rule, then it is effectively "deleted" because it has no impact on the routing of the system. So you can list all the active routing tables like this:

ip rule list | awk '/lookup/ {print $NF}'

That looks for all lookup action in the routing rules and prints the target.

It looks like routing table are identified by a 64 bit integer. If you really, really wanted to find all routing tables with rules, even the inactive ones that have no impact on your system, you could simple iterate a loop from 1 to 2^64:

seq 0 $(echo '2 64 ^ p' | dc) | xargs -iTABLE sh -c 'echo === TABLE ===; ip route show table TABLE'

...but this will take forever and ever, because 64 bits covers a lot of space.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-08-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。