CentOS 7 管理 IP 网络

  • 原创
  • Madman
  • /
  • /
  • 0
  • 8377 次阅读

Synopsis: CentOS 7 中用 ip 或 nmcli 命令配置网络,如果服务器有多个网络接口,并且想指定多个默认网关时,需要添加多张路由表,并使用策略路由规则(Policy-routing)来实现

1. 配置 IP 网络

Redhat 7 Network Guide - Configuring IP Networking

  • ifconfig (net-tools),已废弃
  • ip (iproute2)
  • nmcli
  • nmtui
  • GNOME GUI

Note that the ip utility replaces the ifconfig utility because the net-tools package (which provides ifconfig) does not support InfiniBand addresses.

通过 nmcli 命令指定的配置项,在系统重启后会继续生效。而 ip 命令配置的 IP 信息只临时生效,重启系统后失效,要永久生效的话,需要手动修改 /etc/sysconfig/network-scripts/ifcfg-xxx

2. 配置静态路由和默认网关

Redhat 7 Network Guide - Configuring Static Routes and the Default Gateway

默认网关 适用于未发往本地网络、且未在路由表中指定首选路由的任何流量

要永久生效的话,请使用 nmcli 命令配置路由,或者手动修改 /etc/sysconfig/network-scripts/route-xxx

3. 策略路由 Policy-routing

Policy-routing also known as source-routing, is a mechanism for more flexible routing configurations. Routing decisions are commonly made based on the destination IP address of a package. Policy-routing allows more flexibility to select routes based on other routing properties, such as source IP address, source port, protocol type.

3.1 安装 NetworkManager-config-routing-rules

CentOS 默认使用 NetworkManager 来管理网络,所以需要安装一个插件包来支持 Policy-routing

# yum install NetworkManager-config-routing-rules

3.2 启动 NetworkManager-dispatcher 服务

# systemctl enable NetworkManager-dispatcher.service
# systemctl start NetworkManager-dispatcher.service

3.3 新增路由表

Routing tables stores route information about networks. They are identified by either numeric values or names, which can be configured in the /etc/iproute2/rt_tables file. The default table is identified with 254.

添加一张新的路由表,ID 需要小于 253
# echo '200 s3' >> /etc/iproute2/rt_tables

3.4 添加策略路由规则

帮助文档: man 8 ip-rule

Using policy-routing, you also need rules. Rules are used to select a routing table, based on certain properties of packets

/etc/sysconfig/network-scripts/ 目录下添加 rule-xxx,其中 xxx 是网络接口名称,比如 s3addr(VLAN 名称),假设此 VLAN 创建在聚合设备 team1.5 上,那么 /etc/sysconfig/network-scripts/rule-s3addr 的内容如下:

from 172.18.0.11 table s3

表示 源地址 为 172.18.0.11 的数据包(即服务器回复客户端的数据包)将使用 s3 这张路由表(默认是 main 路由表)

临时生效也可以执行:

# ip rule add from 172.18.0.11 table s3

3.5 为新路由表指定默认网关

帮助文档: man 8 ip-route

/etc/sysconfig/network-scripts/ 目录下添加 route-xxx,其中 xxx 是网络接口名称,比如 s3addr(VLAN 名称),假设此 VLAN 创建在聚合设备 team1.5 上,那么 /etc/sysconfig/network-scripts/route-s3addr 的内容如下:

default via 172.18.0.1 dev team1.5 src 172.18.0.11 table s3

表示 s3 这张路由表的默认网关为 172.18.0.1,即 源地址 为 172.18.0.11 的数据包全部从 team1.5 网络设备出去

临时生效也可以执行:

# ip route add default via 172.18.0.1 dev team1.5 src 172.18.0.11 table s3

3.6 重启系统

3.7 检查配置

# ip route show
# ip route show table s3

# ip rule list
# ip rule list table s3

如果规则不对,可以用 ip route flush table s3 清空路由表

参考:

未经允许不得转载: LIFE & SHARE - 王颜公子 » CentOS 7 管理 IP 网络

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列

文章目录