Linux如何添加路由

Linux如何添加路由
a.如何使用命令给Linux添加一个默认网关?
缺省网关路由:
默认网关就是数据包不匹配任何的路由规则,最后流经的地址关口!网关按字面意思就是网络的关口
,就相当于我们办公室的大门一样,大家上班就要经过办公室的门一样。
使用route -n 查看网关信息,或者 netstat -rn查看路由
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
如何删除路由表:
[root@machine1 ~]# route del default gw 192.168.0.1
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
配置方法:
1、setup
2、配置网卡配置文件:/etc/sysconfig/network-scripts/ifcfg-eth0
关键字:GATEWAY=192.168.0.
重启网卡配置生效
3、route add default gw 192.168.0.1 #(临时生效)
[root@machine1 ~]# route add default gw 192.168.0.1
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
如何添加网络路由?
b.网络路由:即去往某个网段的路由
一般多个网段之间相互通信,希望建立一条优先的路由,而不是通过默认的网关。拿家里来说,
你要去卫生间有卫生间的门,去卧室有卧室的门。这两个门就是网络路由了。家里的大门呢,就是
默认路由。


[root@machine1 ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.0.1
route add -net [目的地] [网关]
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     192.168.0.1     255.255.255.0   UG    0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
可以将路由添加命令加到rc.local里,开机生效,但是重启网卡配置文件不会生效。
或者配置路由配置文件/etc/sysconfig/network-scripts/route-eth0
如下字段:
192.168.1.0/24 via 192.168.0.1
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值