Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add permanent static routes to rhel7

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to add permanent static routes to rhel7". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to add permanent static routes to rhel7".

One: use the route command to add

1. The route added with the route command becomes invalid after the machine is rebooted or the network card is restarted.

For example:

/ / routes added to the host

# route add-host 11.11.1.11 dev eth0

# route add-host 11.11.1.11 gw 11.11.1.1

/ / routes added to the network

# route add-net 11.11.1.11 netmask 255.255.255.0 dev eth0

# route add-net 11.11.1.11 netmask 255.255.255.0 gw 11.11.1.1

# route add-net 11.11.1.0 Compact 24 dev eth2

/ / add a default gateway

# route add default gw 11.11.2.1

/ / Delete a route

# route del-host 11.11.1.11 dev eth0

2. You can also use the ip command to add or delete routes

Ip route add default via 172.16.10.2 dev eth0

Ip route add 172.16.1.0/24 via 172.16.10.2 dev eth0

The format is as follows:

Ip route

(default via gateway dev interface)

(ip/netmask via gateway dev interface)

Second: the method of setting permanent route under linux:

1. Add to / etc/rc.local

Methods:

Route add-net 192.168.3.0 ax 24 dev eth0

Route add-net 192.168.2.0ax 24 gw 192.168.2.254

Note: if adding routes to rc.local will cause the problem that NFS cannot be mounted automatically, you can use the / etc/sysconfig/static-routes method. Both the restart of the system and service network restart will take effect.

Description of the NFS problem:

According to the order in which the linux starts, the contents of the rc.local are executed only after all the services of the linux are started, that is to say, the contents are executed after the NFS, that is, when the NFS starts, the static routes on the server are not added, so the NFS mount cannot succeed.

two。 Added to the end in / etc/sysconfig/network, it takes effect globally. (effective method)

Methods:

GATEWAY=gw-ip

Or

GATEWAY=gw-dev

3./etc/sysconfig/static-routes:

Any net 192.168.3.0/24 gw 192.168.3.254

Any net 10.30.27.128 netmask 255.255.255.192 gw 10.30.27.129

4. Add a route under / etc/sysconfig/network-script/route-interface (one file for each interface. If not, you can only add a route for that interface)

The format is as follows:

Network/prefix via gateway dev intf

For example, add a default gateway to eth0:

Vim / etc/sysconfig/network-scripts/route-eth0

# add the following (dev eth0 can be omitted)

0.0.0.0/0 via 11.11.10.2 dev eth0

Ps: notice that the mask here is 0 instead of 32, because this is a network segment, not a route.

Save and exit after service network restart or systemctl restart network.

Use route-n or netstat-r to view the routing table.

[root@localhost ~] # route-n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

11.11.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

169.254.0.0 0.0.0.0 255.255.0.0 U 1002 00 eth0

0.0.0.0 11.11.10.2 0.0.0.0 UG 0 0 0 eth0

The default route has been added to the routing table.

Note that if you have two network cards, you need to set a default route to access the internet.

All methods of adding static routes have been verified on rhel7 and are correct.

These are all the contents of the article "how to add permanent static routes to rhel7". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report