ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux系统添加永久静态路由的方法

2020-07-11 18:36:52  阅读:334  来源: 互联网

标签:静态 0.0 route 192.168 dev Linux 路由 eth0


一、使用route命令添加

使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:
A、添加到主机的路由

# route add –host 192.168.1.10 dev eth0
# route add –host 192.168.1.10 gw 192.168.1.1
# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0  # 其中dev eth0可以省略

B、添加到网络的路由

# route add –net 192.168.1.0/24 gw 192.168.1.1
# route add –net 192.168.1.0/24 dev eth1
# route  del  -net 192.168.32.7/32  gw 172.18.0.200  # 删除路由

C、添加默认路由

# route add default gw 192.168.1.1

D、删除路由

# route del –host 192.168.1.10 dev eth0 
# route del default gw 192.168.1.1
# route  del  -net 192.168.32.7/32  gw 172.18.0.200  # 删除路由

E、查看添加的所有路由信息

[root@centos-7 network-scripts]# route -n  # 第一种查看路由表方法
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.7.2     0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.0.0.1        192.168.7.2     255.255.255.255 UGH   0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0


[root@centos-7 network-scripts]# ip route  # 第二种查看路由表方法
default via 192.168.7.2 dev eth0 
10.0.0.0/24 dev eth0 scope link 
10.0.0.1 via 192.168.7.2 dev eth0 
169.254.0.0/16 dev eth0 scope link metric 1002 
192.168.7.0/24 dev eth0 proto kernel scope link src 192.168.7.100 

二、使用ip route 添加路由

A、添加路由

# ip route add 2.2.2.0/24  via  1.1.1.1  # 前面是路由,后面是网关地址
# ip  a  a  1.1.1.1/24 dev eth1    # 添加一个IP地址
# ip route add 192.168.0.0/24 via 172.16.0.1  # 添加路由

B、查看路由信息

[root@centos-7 network-scripts]# ip route   # 此方法查看到的路由信息方便存在配置文件中,或者用命令也可以直接复制粘贴,方便你我他。
default via 192.168.7.2 dev eth0  
10.0.0.0/24 dev eth0 scope link 
10.0.0.1 via 192.168.7.2 dev eth0 
169.254.0.0/16 dev eth0 scope link metric 1002 
192.168.7.0/24 dev eth0 proto kernel scope link src 192.168.7.100 

三、将路由永久写到配置文件中,重启网卡永久生效

A、在/etc/sysconfig/network-scripts目录下创建一个route-ethx的网卡名称,添加以下对应要添加的内容即可

[root@centos-7 network-scripts]# cat route-eth0 
10.0.0.1 via 192.168.7.2
10.0.0.0/24 dev eth0

B、查看此时配置的路由信息

[root@centos-7 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.7.2     0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.0.0.1        192.168.7.2     255.255.255.255 UGH   0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

 

多个路由器之间的互通,详见此链接:https://www.cnblogs.com/struggle-1216/p/12730939.html

  

  

 

 

 

标签:静态,0.0,route,192.168,dev,Linux,路由,eth0
来源: https://www.cnblogs.com/struggle-1216/p/13284805.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有