linux路由相关

临时路由

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

添加到主机的路由

# route add –host 192.168.1.11 dev eth0
# route add –host 192.168.1.12 gw 192.168.1.1

添加到网络的路由

# route add –net 192.168.1.11  netmask 255.255.255.0 eth0
# route add –net 192.168.1.11  netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 eth1

添加默认网关

# route add default gw 192.168.2.1

删除路由

# route del –host 192.168.1.11 dev eth0

route -n 查看到的路由信息

#route -n 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.0.0     10.138.255.247  255.255.0.0     UG    0      0        0 eth0
10.138.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
172.16.0.0      10.138.255.247  255.240.0.0     UG    0      0        0 eth0
100.64.0.0      10.138.255.247  255.192.0.0     UG    0      0        0 eth0
10.0.0.0        10.138.255.247  255.0.0.0       UG    0      0        0 eth0
42.0.0.0        10.138.255.247  255.0.0.0       UG    0      0        0 eth0
11.0.0.0        10.138.255.247  255.0.0.0       UG    0      0        0 eth0
30.0.0.0        10.138.255.247  255.0.0.0       UG    0      0        0 eth0
170.0.0.0       10.138.255.247  255.0.0.0       UG    0      0        0 eth0
0.0.0.0         10.138.255.247  0.0.0.0         UG    0      0        0 eth0
## genmask 就是-net              gateway 就是gw

永久路由

  1. 在/etc/rc/local中添加
# route add -net 192.168.3.0/24 dev eth0 
# route add -net 192.168.2.0/24 gw 192.168.2.254
  1. 在/etc/sysconfig/network里添加到末尾
    方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
  2. /etc/sysconfig/static-routes : (没有static-routes的话就手动建立一个这样的文件)
any net 192.168.3.0/24 gw 192.168.3.254 
any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129
  1. 开启 IP 转发:
# echo "1" >/proc/sys/net/ipv4/ip_forward (临时)
# vi /etc/sysctl.conf --> net.ipv4.ip_forward=1 (永久开启)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容