CentOS 6.4 adds a summary of all methods for permanent static routing

  • 2020-05-15 03:26:57
  • OfStack

CentOS adds permanent static routing, as follows:

In the use of a dual network card, the use of two gateways need to add a static route. Of course, there are many times when routing is required.

Operating system version centos-6.4 64bit

1: add using the route command

1. The route added by the route command will be invalid after the machine is restarted or the network card is restarted. Methods:


// Routes added to the host 

 # route add  � host 192.168.1.11 dev eth0

# route add  � host 192.168.1.12 gw 192.168.1.1

// Routes added to the network 

# route add  � net 192.168.1.11 netmask 255.255.255.0 dev 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 dev eth1

// Add default gateway 

# route add default gw 192.168.2.1

// Delete the routing 

# route del  � host 192.168.1.11 dev eth0

2. You can also use the ip command to add and remove 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

2: method of setting up permanent route under Linux:

1. Add in /etc/ rc.local

Methods:


 route add -net 192.168.3.0/24 dev eth0

route add -net 192.168.2.0/24 gw 192.168.2.254

2. Add it to the end in /etc/sysconfig/network

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.250.228.128 netmask 255.255.255.192 gw 10.250.228.129

If adding a route to rc.local causes NFS to fail to mount automatically, it is best to use static-routes. Both system restart and service network restart will take effect.

Description of solving NFS problem:

According to the starting order of linux, the contents of rc.local are executed after all the services of linux are started. That is to say, the contents of linux are executed after netfs is started. That is to say, when netfs is started, the static route on the server is not added, so netfs cannot mount successfully.

4. Add routes under /etc/sysconfig/ network-script/route-interface (1 file for each interface, create 1 file if there is none, only routes for that interface can be added)

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 (can be omitted) dev eth0 ) 

0.0.0.0/0 via 172.16.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 after exiting service network restart.

View the routing table using route-n or netstat-r.


[root@localhost ~]# route -n
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
172.16.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  0    0 eth0
0.0.0.0     172.16.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 the default route to access internet.

All methods for adding static routing are verified to be correct above centos6.4.


Related articles: