重庆思庄Oracle、Redhat认证学习论坛
标题: Linux和Windows中永久设置静态路由的方法 [打印本页]
作者: 梅钟园 时间: 2023-4-12 22:52
标题: Linux和Windows中永久设置静态路由的方法
注:以下内容仅作示例,若为内部学员,切勿贸然在对应主机直接执行。
一、RHEL8中设置静态路由
1、nmcli命令直接配置([+]ipv4.routes - NetworkManager)
- [root@servera ~]# nmcli con modify Wired\ connection\ 1 \
- ipv4.gateway 172.25.250.250 \
- ipv4.dns 172.25.250.254 \
- +ipv4.dns 114.114.114.114 \
- +ipv4.routes "172.25.0.0/16 172.25.250.254"
复制代码- [root@servera ~]# cat /etc/sysconfig/network-scripts/ifcfg-Wired_connection_1
- TYPE=Ethernet
- PROXY_METHOD=none
- BROWSER_ONLY=no
- BOOTPROTO=none
- DEFROUTE=yes
- IPV4_FAILURE_FATAL=no
- IPV6INIT=yes
- IPV6_AUTOCONF=yes
- IPV6_DEFROUTE=yes
- IPV6_FAILURE_FATAL=no
- IPV6_ADDR_GEN_MODE=stable-privacy
- NAME="Wired connection 1"
- UUID=81b08161-8925-3cb9-a94c-e3c827e2adc3
- DEVICE=eth0
- ONBOOT=yes
- AUTOCONNECT_PRIORITY=-999
- PEERDNS=no
- IPV6_PEERDNS=no
- IPADDR=172.25.250.10
- PREFIX=24
- GATEWAY=172.25.250.250
- DNS1=172.25.250.254
- DNS2=114.114.114.114
- DOMAIN="lab.example.com example.com"
复制代码- [root@servera ~]# cat /etc/sysconfig/network-scripts/route-Wired_connection_1
- ADDRESS0=172.25.0.0
- NETMASK0=255.255.0.0
- GATEWAY0=172.25.250.254
复制代码 2、手动设置静态路由(NetworkManager)
- [root@servera ~]# vim /etc/sysconfig/network-scripts/route-Wired_connection_1
- 172.25.0.0/16 via 172.25.250.254
- 0.0.0.0/0 via 172.25.250.250
复制代码 3、其他静态路由写入方式(network)
/etc/sysconfig/network
网卡不配置网关,然后只在 /etc/sysconfig/network文件中设置默认网关
- [root@host~]# vim /etc/sysconfig/network
- GATEWAY=172.25.250.254
复制代码 /etc/sysconfig/static-routes
- [root@host~]# vim /etc/sysconfig/static-routes
- any net 172.25.0.0/16 gw 172.25.250.254
- any host 192.168.123.11 gw 192.168.123.254
复制代码 /etc/sysconfig/network-scripts/route-ethX
- [root@host~]# vim /etc/sysconfig/network-scripts/route-Wired_connection_1
- 172.25.0.0/16 via 172.25.250.254
- 0.0.0.0/0 via 172.25.250.250
复制代码
二、RHEL9中设置静态路由
1、命令行设置(nmcli)
- [root@servera ~]# nmcli con mod Wired\ connection\ 1 \
- ipv4.gateway 172.25.250.250 \
- ipv4.dns 172.25.250.220 \
- +ipv4.dns 114.114.114.114 \
- +ipv4.routes "172.25.0.0/24 172.25.250.254"
复制代码 2、编辑配置文件
RHEL9采用原生NetworkManager,直接配置NetworkManager的配置文件即可.
- [root@servera ~]# cat \
- /etc/NetworkManager/system-connections/Wired\ connection\ 1.nmconnection
- [connection]
- id=Wired connection 1
- uuid=ec3a15fb-2e26-3254-9433-90c66981e924
- type=ethernet
- autoconnect-priority=-999
- interface-name=eth0
- timestamp=1681025641
- [ethernet]
- [ipv4]
- address1=172.25.250.10/24,172.25.250.250
- dns=172.25.250.220;114.114.114.114;
- dns-search=lab.example.com;example.com;
- ignore-auto-dns=true
- method=manual
- route1=172.25.0.0/24,172.25.250.254
- [ipv6]
- addr-gen-mode=stable-privacy
- ignore-auto-dns=true
- method=auto
- [proxy]
复制代码
三、Windows中设置静态路由
查看路由表和接口 - 注意红色部分接口编号