重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 386|回复: 0

[网络管理] Linux和Windows中永久设置静态路由的方法

[复制链接]
发表于 2023-4-12 22:52:58 | 显示全部楼层 |阅读模式
注:以下内容仅作示例,若为内部学员,切勿贸然在对应主机直接执行。


一、RHEL8中设置静态路由
1、nmcli命令直接配置([+]ipv4.routes - NetworkManager)
  1. [root@servera ~]# nmcli con modify Wired\ connection\ 1 \
  2. ipv4.gateway 172.25.250.250 \
  3. ipv4.dns 172.25.250.254 \
  4. +ipv4.dns 114.114.114.114 \
  5. +ipv4.routes "172.25.0.0/16 172.25.250.254"
复制代码
  1. [root@servera ~]# cat /etc/sysconfig/network-scripts/ifcfg-Wired_connection_1
  2. TYPE=Ethernet
  3. PROXY_METHOD=none
  4. BROWSER_ONLY=no
  5. BOOTPROTO=none
  6. DEFROUTE=yes
  7. IPV4_FAILURE_FATAL=no
  8. IPV6INIT=yes
  9. IPV6_AUTOCONF=yes
  10. IPV6_DEFROUTE=yes
  11. IPV6_FAILURE_FATAL=no
  12. IPV6_ADDR_GEN_MODE=stable-privacy
  13. NAME="Wired connection 1"
  14. UUID=81b08161-8925-3cb9-a94c-e3c827e2adc3
  15. DEVICE=eth0
  16. ONBOOT=yes
  17. AUTOCONNECT_PRIORITY=-999
  18. PEERDNS=no
  19. IPV6_PEERDNS=no
  20. IPADDR=172.25.250.10
  21. PREFIX=24
  22. GATEWAY=172.25.250.250
  23. DNS1=172.25.250.254
  24. DNS2=114.114.114.114
  25. DOMAIN="lab.example.com example.com"
复制代码
  1. [root@servera ~]# cat /etc/sysconfig/network-scripts/route-Wired_connection_1
  2. ADDRESS0=172.25.0.0
  3. NETMASK0=255.255.0.0
  4. GATEWAY0=172.25.250.254
复制代码
2、手动设置静态路由(NetworkManager
  1. [root@servera ~]# vim /etc/sysconfig/network-scripts/route-Wired_connection_1
  2. 172.25.0.0/16 via 172.25.250.254
  3. 0.0.0.0/0 via 172.25.250.250
复制代码
3、其他静态路由写入方式(network)
/etc/sysconfig/network
网卡不配置网关,然后只在 /etc/sysconfig/network文件中设置默认网关
  1. [root@host~]# vim /etc/sysconfig/network
  2. GATEWAY=172.25.250.254
复制代码
/etc/sysconfig/static-routes
  1. [root@host~]# vim /etc/sysconfig/static-routes
  2. any net 172.25.0.0/16 gw 172.25.250.254
  3. any host 192.168.123.11 gw 192.168.123.254
复制代码
/etc/sysconfig/network-scripts/route-ethX
  1. [root@host~]# vim /etc/sysconfig/network-scripts/route-Wired_connection_1
  2. 172.25.0.0/16 via 172.25.250.254
  3. 0.0.0.0/0 via 172.25.250.250
复制代码


二、RHEL9中设置静态路由

1、命令行设置(nmcli)
  1. [root@servera ~]# nmcli con mod Wired\ connection\ 1 \
  2. ipv4.gateway 172.25.250.250 \
  3. ipv4.dns 172.25.250.220 \
  4. +ipv4.dns 114.114.114.114 \
  5. +ipv4.routes "172.25.0.0/24 172.25.250.254"
复制代码
2、编辑配置文件
RHEL9采用原生NetworkManager,直接配置NetworkManager的配置文件即可.
  1. [root@servera ~]# cat \
  2. /etc/NetworkManager/system-connections/Wired\ connection\ 1.nmconnection
  3. [connection]
  4. id=Wired connection 1
  5. uuid=ec3a15fb-2e26-3254-9433-90c66981e924
  6. type=ethernet
  7. autoconnect-priority=-999
  8. interface-name=eth0
  9. timestamp=1681025641

  10. [ethernet]

  11. [ipv4]
  12. address1=172.25.250.10/24,172.25.250.250
  13. dns=172.25.250.220;114.114.114.114;
  14. dns-search=lab.example.com;example.com;
  15. ignore-auto-dns=true
  16. method=manual
  17. route1=172.25.0.0/24,172.25.250.254

  18. [ipv6]
  19. addr-gen-mode=stable-privacy
  20. ignore-auto-dns=true
  21. method=auto

  22. [proxy]
复制代码

三、Windows中设置静态路由

查看路由表和接口 - 注意红色部分接口编号
658396436c44a3951e.png

永久设置静态路由
985256436c4681ca70.png
注:if后面的2为网卡编号,即route print看到的网卡接口编号;-p表示永久设置。
  1. route add -p 192.168.199.0 255.255.255.0 192.168.199.2 if 2
复制代码
命令帮助摘要
  1. route add /?
  2. ... outpute omitted ...
  3. ROUTE [-f] [-p] [-4|-6] command [destination]
  4.                   [MASK netmask]  [gateway] [METRIC metric]  [IF interface]
  5. ... output omitted ...
  6.   -p           与 ADD 命令结合使用时,将路由设置为
  7.                在系统引导期间保持不变。默认情况下,重新启动系统时,
  8.                不保存路由。忽略所有其他命令,
  9.                这始终会影响相应的永久路由。

  10.   -4           强制使用 IPv4。

  11.   -6           强制使用 IPv6。

  12.   command      其中之一:
  13.                  PRINT     打印路由
  14.                  ADD       添加路由
  15.                  DELETE    删除路由
  16.                  CHANGE    修改现有路由
  17.   destination  指定主机。
  18.   MASK         指定下一个参数为“netmask”值。
  19.   netmask      指定此路由项的子网掩码值。
  20.                如果未指定,其默认设置为 255.255.255.255。
  21.   gateway      指定网关。
  22.   interface    指定路由的接口号码。
  23.   METRIC       指定跃点数,例如目标的成本。
  24. ...output omitted ...
  25.     > route ADD 157.0.0.0 MASK 255.0.0.0  157.55.80.1 METRIC 3 IF 2
  26.              destination^      ^mask      ^gateway     metric^    ^
  27.                                                          Interface^
  28.       如果未给出 IF,它将尝试查找给定网关的最佳
  29.       接口。
  30.     > route ADD 3ffe::/32 3ffe::1

  31.     > route CHANGE 157.0.0.0 MASK 255.0.0.0 157.55.80.5 METRIC 2 IF 2

  32.       CHANGE 只用于修改网关和/或跃点数。

  33.     > route DELETE 157.0.0.0
  34.     > route DELETE 3ffe::/32
复制代码
刷新DNS缓存
  1. ipconfig /flushdns
复制代码


四、Ubuntu中永久设置静态路由
配置示例
557626436c4df67d79.png

帮助手册可以查到诸多配置示例(man netplan)
  1. 帮助手册中含有大量配置示例,可作为参考进行配置。
  2. man netplan
  3. 搜索 Examples 或 route 关键字

  4. eth0:
  5.   [...]
  6.   routes:
  7.     - to: default # could be 0/0 or 0.0.0.0/0 optionally
  8.       via: 10.0.0.1
  9.       metric: 100
  10.       on-link: true
  11.     - to: default # could be ::/0 optionally
  12.       via: cf02:de:ad:be:ef::2
  13. eth1:
  14.   [...]
  15.   routes:
  16.     - to: default
  17.       via: 172.134.67.1
  18.       metric: 100
  19.       on-link: true
  20.       table: 76 # Not on the main routing table, does not conflict with the eth0 default route

  21. network:
  22.   version: 2
  23.   renderer: networkd
  24.   ethernets:
  25.     eno1:
  26.       addresses:
  27.       - 10.0.0.10/24
  28.       - 11.0.0.11/24
  29.       nameservers:
  30.       addresses:
  31.       - 8.8.8.8
  32.       - 8.8.4.4
  33.       routes:
  34.       - to: 0.0.0.0/0
  35.         via: 10.0.0.1
  36.         metric: 100
  37.       - to: 0.0.0.0/0
  38.         via: 11.0.0.1
  39.         metric: 100
复制代码





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2023-10-2 23:01 , Processed in 0.099772 second(s), 25 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表