怎么在centos中添加一条路由

怎么在centos中添加一条路由,第1张

你好,

linux下静态路由修改命令

方法一:

添加路由

route add -net 192.168.0.0/24 gw 192.168.0.1

route add -host 192.168.1.1 dev 192.168.0.1

删除路由

route del -net 192.168.0.0/24 gw 192.168.0.1

add 增加路由

del 删除路由

-net 设置到某个网段的路由

-host 设置到某台主机的路由

gw 出口网关 IP地址

dev 出口网关 物理设备名

增 加默认路由

route add default gw 192.168.0.1

默认路由一条就够了

route -n 查看路由表

方法二:

添加路由

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 文件中就行了。

如加入:

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

修改 /etc/sysconfig/network-scripts/route-ens33

其中 ens33 应该是当前服务器的网卡名称

vim 修改一下。这里要注意一下格式

(1) TARGET via GW

例如:

192.168.0.0/24 via 172.16.0.1

0.0.0.0/0 via 172.16.10.2 dev eth0 (可以省略dev eth0 ,注意这里的掩码是0而不是32,因为这里是网段而不是路由)网页链接

(2) 每三行定义一条路由

ADDRESS#=TARGET   #表示数字

NETMASK#=mask

GATEWAY#=GW

例如:ADDRESS0=192.16.20.0

NETMASK0=255.255.255.0

GATEWAY0=172.16.0.1

一、ip route显示和设定路由

1、显示路由表

[root@centos7 ~]# ip route show

default via 192.168.150.254 dev enp0s3  proto static  metric 1024192.168.150.0/24 dev enp0s3  proto kernel  scope link  src 192.168.150.110

太难看了,格式化一下(显示的是默认网关和局域网路由,两行的内容没有共通性):

[root@centos7 tmp]# ip route show|column -t

default           via  192.168.150.254  dev    enp0s3  proto  static  metric  1024192.168.150.0/24  dev  enp0s3           proto  kernel  scope  link    src     192.168.150.110

2、添加静态路由

[root@centos7 ~]# ip route add 10.15.150.0/24 via 192.168.150.253 dev enp0s3

[root@centos7 ~]#

[root@centos7 ~]# ip route show|column -t

default           via  192.168.150.254  dev    enp0s3  proto  static  metric  102410.15.150.0/24    via  192.168.150.253  dev    enp0s3  proto  static  metric  1192.168.150.0/24  dev  enp0s3           proto  kernel  scope  link    src     192.168.150.110[root@centos7 ~]#

[root@centos7 ~]# ping 10.15.150.1PING 10.15.150.1 (10.15.150.1) 56(84) bytes of data.64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.77 ms64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08 ms64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57 ms

^C

3、删除静态路由

只需要把 add 替换成 del,或者更简单的只写目标网络

[root@centos7 ~]# ip route del 10.15.150.0/24


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11797248.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存