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

Combing routing configuration under Linux

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Linux dual network cards (internal and external network) simultaneously use routing settings

Http://blog.csdn.net/hshl1214/article/details/53103790

The company's business needs, one server, double network cards, need to access both the external network and the internal network. In the process of setting up, colleagues reflect that the original easy-to-use internal network address (192.168.1.100) becomes unusable after setting the external network address, as long as you stop the external network card and the internal network can be accessed normally.

In fact, it is because after you set up the internal network and then the external network, after setting the gateway for the public network, because there is no default gateway, the IP of the internal network will also go to the gateway of the external network, which is definitely not allowed. All we have to do is let the intranet use its own network card.

Private network card: eth0 192.168.1.100 Gateway: 192.168.1.100

External network card: eth2 119.222.222.222 Gateway: 119.222.222.1

First, set the default gateway so that all IP packets are forwarded through 119.222.222.1 by default:

Route add default gw 119.222.222.1

Then, set a forwarding special case for the private network separately. All those at the beginning of 192.168 go to eth0.

Route add-net 192.168.0.0 netmask 255.255.0.0 dev eth0

It is best to add the route to boot.

Vi / etc/rc.local

Also: the command to delete the default gateway is as follows:

Route del default

The method of setting permanent route under linux: / etc/sysconfig/static-routes

Written as: any net 192.168.0.0 gw 16 Gateway ip

Combing https://www.cnblogs.com/kevingrace/p/6490627.html for routing configuration under Linux

In the daily operation and maintenance operations, we often encounter the operation of routing table. Let's sort out the routing operations in linux OPS:

Let's start with some basic knowledge about routing:

1) routing concept

Routing: the process of forwarding packets across an internetwork from the source host to the destination host

Router: a device that can forward a packet to the correct destination and choose the best path in the forwarding process

Routing table: a route entry maintained in a router that the router chooses according to the routing table.

Directly connected routing: when the IP address of the interface is configured on the router and the interface status is up, a directly connected route entry appears in the routing table

Static routes: manually configured by the administrator and one-way.

Default route: when the router cannot find a routing entry for the destination network in the routing table, the router forwards the request to the default routing interface.

2) characteristics of static and default routes

Static routing characteristics:

The routing table is set manually

Static routes will not change unless the network administrator intervenes

The formation of routing table does not need to take up network resources.

Applicable environment: generally used in networks with small network size and fixed topology.

Default routing characteristics:

Of all route types, the default route has the lowest priority

Applicable environment: generally used in an end network with only one exit or as a supplement to other routes

Floating static route:

When there is a route entry for the same destination network in the routing table, the request is forwarded to the corresponding port according to the priority of the route entry.

The role of link redundancy

3) the encapsulation process when the router forwards the packet

The source IP and destination IP do not change. In each segment of the network, the source and destination MAC change and re-encapsulate, which are the source and destination addresses of each segment, respectively.

4) to route a packet, a router must know at least the following:

A) destination address

B) connect to the router and where can you get information about the remote network

C) possible routes to all remote networks

D) the best route to each remote network

E) how to maintain and verify routing information

F) comparison of routing and switching

Routing works at the network layer

A) forward data according to the routing Table

B) routing

C) routing forwarding

Switching works at the data link layer

D) forward data according to the MAC address Table

E) hardware forwarding

Then let's talk about some operations about routing in linux operation and maintenance.

1) use the route-n command to view the Linux kernel routing table

[root@dev] # route-n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.17 0.0.0.0 255.255.255.255 UH 000 ppp0 10.1.32.14 0.0.0.0 255.255.255.255 UH 2000 tun0 10.1.32.12 0.0.0.0 255.255.255.255 UH 000 tun0 10.4.8.2 192.168.9.254 255.255.255. 255 UGH 0 0 0 eth0 10.4.9.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0 192.168.9.0 0.0.0.0 255.255.255.0 U 10 0 eth0 10.2.0.0 0.0.0.0 255.255.0.0 U 0 0 0 tun0 10.0.0.0 0.0.0.0 255.255.0.0 U 0 0 0 tun0 10.1.0.0 0.0 . 0.0 255.255.0.0 U 0 0 0 tun0 192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 tun0 0.0.0.0 192.168.9.254 0.0.0.0 UG 0 0 0 eth0

2) description of three routing types

A) Host routing

A host route is a route record in the routing table that points to a single IP address or hostname. The Flags field of the host route is H. For example, in the following example, the local host reaches the host with the IP address 10.0.0.10 through the router with IP address 192.168.1.1.

Destination Gateway Genmask Flags Metric Ref Use Iface-10.0.0.10 192.168.1.1 255.255.255.255 UH 000 eth0

B) Network routing

A network route represents a network that can be reached by a host. The Flags field of the network route is N. For example, in the following example, the local host forwards packets sent to network 192.19.12 to the router with the IP address 192.168.1.1.

Destination Gateway Genmask Flags Metric Ref Use Iface-192.19.12 192.168.1.1 255.255.255.0 UN 2000 eth0

C) default route

When the host cannot find the IP address or network route of the target host in the routing table, the packet is sent to the default route (default gateway). The Flags field of the default route is G. For example, in the following example, the default route is the router with the IP address 192.168.1.1.

Destination Gateway Genmask Flags Metric Ref Use Iface-default 192.168.1.1 0.0.0.0 UG 2000 eth0

3) configure commands for routing route

You can use the route command to set and view the routing table. The command format for setting the kernel routing table is:

Route [add | del] [- net |-host] target [netmask Nm] [gw Gw] [[dev] If]

Parameter explanation:

Add adds a routing rule

Del deletes a routing rule

-net destination address is a network

-host destination address is a host

Target destination network or host

The network mask of the netmask destination address

The gateway through which gw routing packets pass

The network interface specified by dev for the route

4) examples of using route command

Route added to the host # route add-host 192.168.1.2 dev eth0:0 # route add-host 10.20.30.148 gw 10.20.30.40 added to the network # route add-net 10.20.30.40 netmask 255.255.255.248 eth0 # route add-net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 # route add-net 192.168.1.0 hand 24 eth2 add Default route # route add default gw 192.168.1.1 Delete route # route del-host 192.168.1.2 dev eth0:0 # route del-host 10.20.30.148 gw 10.20.30.40 # route del-net 10.20.30.40 netmask 255.255.255.248 eth0 # route del-net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 # route del-net 192.168.1.0 / 24 eth2 # route del default gw 192.168.1.1 / / route del default delete all default routes and add a default route # route add default gw 10.0.0.1 / / default only in memory boot can be appended to / etc/rc.local file # echo "route add default gw 10.0.0.1" > > / etc/rc.local add a static route # route add-net 192. 168.2.0host 24 gw 192.168.2.254 to take effect permanently: # echo "any net 192.168.2.0 gw 24 gw 192.168.2.254" > / static route added to a host # route add-host 192.168.2.2 gw 192.168.2.254 to take effect permanently: # echo "any host 192.168.2.2 gw 192 .168.2.254 "> > / etc/sysconfig/static-routes Note: Linux does not have this file by default You have to create one manually.

5) set packet forwarding

The default kernel configuration in Linux already includes the routing feature, but it is not enabled by default at system startup

The routing function of Linux can be enabled by adjusting the network parameters of the kernel as follows:

Temporarily enable routing function: # echo 1 > / proc/sys/net/ipv4/ip_forward or # sysctl-w net.ipv4.ip_forward=1 permanently enable routing function # vim / etc/sysctl.conf net.ipv4.ip_forward=1 # sysctl-p

6) static route configuration

The syntax for adding a static route to the routing table is as follows:

Ip route [destination_network] [mask] [next-hop_address] administrative_distance]

Parameter resolution:

The command used by ip route to create static routes.

Destination_network needs to be published to the network segment in the routing table.

The subnet mask that Mask uses on this network.

The address of the Next-hop_address next-hop router.

When administrative_distance defaults, static routes have an administrative distance of 1. Add administrative rights at the end of the command to modify the default value.

For example

Ip route 172.16.1.0 255.255.255.0 172.16.2.1

In addition to using the route-n command to view the routing table, you can also use ip route

[root@dev ~] # ip route 192.168.1.17 dev ppp0 proto kernel scope link src 192.168.1.190 10.1.32.14 dev tun0 scope link 10.1.32.12 dev tun0 scope link 10.4.8.2 via 192.168.9.254 dev eth0 src 192.168.9.200 mtu 1500 advmss 1460 10.4.9.0 dev tun0 scope link 192.168.9.0 dev eth0 proto kernel scope link src 192.168.9.200 metric 1 10 .2.0.0 / 16 dev tun0 scope link 10.0.0.0/16 dev tun0 scope link 10.1.0.0/16 dev tun0 scope link 192.168.0.0/16 dev tun0 scope link default via 192.168.9.254 dev eth0

-- example 1, Murray,

As shown in the figure above, there are two routers between the PC0 machine and the PC1 machine. For the two machines to communicate, the routing settings are as follows:

1) Route0 router settings:

Ip add 192.168.1.1 255.255.255.0 ip add 192.168.2.1 255.255.255.0 ip route 192.168.3.0 255.255.255.0 192.168.2.2

2) Route1 router settings:

Ip add 192.168.2.2 255.255.255.0 ip add 192.168.3.1 255.255.255.0 ip route 192.168.1.0 255.255.255.0 192.168.2.1

-- example 2, Murray,

As shown in the figure above, host A 192.168.1.2 can be used to ping E host 192.168.4.2, and the two machines can communicate.

Operating ideas:

1) set the default route next hop to 192.168.2.2 on host B, and enable route forwarding

2) set two static routes on host C, the next hop of the network segment to 192.168.1.0 and 192.168.2.1 to 192.168.1.0 and 192.168.3.2 to 192.168.4.0, respectively, and enable the route forwarding function.

3) set the default route next hop to 192.168.3.1 on host D, and enable route forwarding.

Operation record:

1) Operation on host A: ip is 192.168.1.2, gateway is 192.168.1.1 # route add default gw 192.168.1.1) Operation on host B: the first network card is 192.168.1.1, and the second network card is 192.168.2.1 # ifconfig eth0 192.168.1.1 # ifconfig eth2 192.168.2.1 / / you can set two ip on one network card, such as eth0 Eth0:0 B host sets default route, next hop is 192.168.2.2 # route add default gw 192.168.2.2 B host enables route forwarding function # echo 1 > / proc/sys/net/ipv4/ip_forward / / temporary forwarding, you can set permanent forwarding in / etc/sysctl.conf 3) operate on C host: the first network card is 192.168.2.2 The second network card is 192.168.3.1 # ifconfig eth0 192.168.2.2 # ifconfig eth2 192.168.3.1 / / if there is only one network card You can set ifconfig eth0:0 192.168.3.1 C host to set 2 default routes # route add-net 192.168.1.0 CPM 24 gw 192.168.2.1 # route add-net 192.168.4.0 CPM 24 gw 192.168.3.2 C host enable route forwarding function # echo 1 > / proc/sys/net/ipv4/ip_forward 4) Operation on host D: the first network card is 192.168.3.2 The second Nic sets the default route for the 192.168.4.1 # ifconfig eth0 192.168.3.2 # ifconfig eth2 192.168.4.1 D host, and the next hop is 192.168.3.1 # route add default gw 192.168.3.1 D host enables route forwarding function # echo 1 > / proc/sys/net/ipv4/ip_forward 5) E operation on the host: ip is 192.168.4.2 Set the gateway to 192.168.4.1 # route add default gw 192.168.4.1

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