linux系统路由设置

linux下静态路由设置

查看路由:

route -e

route -n

 添加静态路由

route add -net 192.168.128.0/17 gw 192.168.1.32      #该条路由表示目的地为192.168.128.0这半个B段(根据子网掩码判断)的下一跳都指向192.168.1.32

route add -host 192.168.1.25 dev 192.168.0.1        #该条路由表示目的地为192.168.1.1的主机添加到192.168.0.1的设备上(默认没有192.168.0.1这个设备,可以使用Iface上查看到的例如ens3)

添加网关:

route add 10.222.222.10/32 dev ens3 

 根据新添加的网关,可以新添加指向该网关的路由,没有新网关,添加不了新路由

添加默认路由:(一般只有一条)

root add default gw 192.168.1.1

 删除路由:

把添加路由的对应的add改成del即可

参数介绍

add 增加路由
del 删除路由
-net 设置到某个网段的路由
-host 设置到某台主机的路由
gw 出口网关 IP地址
dev 出口网关 物理设备名

上述添加的都是临时添加的,重启服务器或者systemctl restart network重启网卡会失效。

方法二:
添加路由
ip route add 192.168.0.0/24 via 192.168.0.1
ip route add 192.168.1.1 dev 192.168.0.1
删除路由
ip route del 192.168.0.0/24 via 192.168.0.1

add 增加路由
del 删除路由
via 网关出口 IP地址
dev 网关出口 物理设备名

增加默认路由
ip route add default via 192.168.0.1 dev eth0
via 192.168.0.1 是我的默认路由器

查看路由信息
ip route

保存路由设置,使其在网络重启后任然有效 
在/etc/sysconfig/network-script/目录下创建名为route- eth0的文件 
vi /etc/sysconfig/network-script/route-eth0 
在此文件添加如下格式的内容 

192.168.1.0/24 via 192.168.0.1 

重启网络验证

路由设置永久保存方法:

配置文件/etc/rc.d/init.d/network中有这么几行:

# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi 

也就是说,将静态路由加到/etc/sysconfig/static-routes 文件中就行了,如果没有static-routes 文件自己新建一个。

如加入:
route add -net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1

则static-routes的格式为
any net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1

转自:https://www.cnblogs.com/chenxiaomeng/p/10450636.html

猜你喜欢

转载自www.cnblogs.com/longchengruoxi/p/12190733.html