How to configure static routes in Linux

Source: Internet
Author: User
Tags documentation

When a host has only one network card, the problem of default routing and static routing is not involved. Two network cards can also be configured with a gateway (the default route to this network card), and the other only configure the IP and mask confirmation (the host and the network can be interoperable). However, in a slightly larger, more network-segment environment, the default routing and static route configuration are required separately.

I. Demand

At present, there is a network, the host has two network cards, two network segments are business address eth0 and management address eth1.

Business Address segment is: 192.168.3.0/24 paragraph

Manage Address segment: 10.212.52.0/24 section

Firewall segment: 10.211.6.0/24 segment

Now the demand is that the default route to the business address (192.168.3.0/24), the firewall segment to go 10.211.6.0/24 business address, 10.0.0.0/8 all addresses are gone to manage the address section.

Second, Redhat static routing configuration

In Redhat environment, there are three kinds of configuration methods

Method One: Configure in the/etc/sysconfig/network configuration file:

The code is as follows:
Default via 192.168.3.1 dev eth0 #192.168.3.1 gateway address for eth0 network card
10.211.6.0/24 via 192.168.3.1 Dev eth0
10.0.0.0/8 via 10.212.52.1 Dev eth1 #10.212.52.1 gateway address for ETH1 network card
Note: This configuration notation also supports writing to the/etc/sysconfig/network-scripts/route-interferface configuration file.

Refer to Redhat official documentation.

Method Two: In/etc/sysconfig/network-scripts/route-interferface configuration file configuration

The two configuration formats are supported here

A: Methods mentioned in Method 1

The code is as follows:
# Cat/etc/sysconfig/network-scripts/route-eth0
0.0.0.0/0 via 192.168.3.1 Dev eth0
10.211.6.0/24 via 192.168.3.1 Dev eth0
# cat/etc/sysconfig/network-scripts/route-eth1
10.0.0.0/8 via 10.212.52.1 Dev eth1
B: Network Mask method

The code is as follows:
# Cat/etc/sysconfig/network-scripts/route-eth0
address0=0.0.0.0
netmask0=0.0.0.0
gateway0=192.168.3.1
address1=10.211.6.0
netmask1=255.255.255.0
gateway1=192.168.3.1
Where the network segment address and mask are all 0 represent all network segments, that is, the default route.

# cat/etc/sysconfig/network-scripts/route-eth1
address0=10.0.0.0
netmask0=255.0.0.0
gateway0=10.212.52.1
The network mask method can also refer to the official Redhat documentation.

Method Three:/etc/sysconfig/static-routes configuration

The code is as follows:

# Cat/etc/sysconfig/static-route
Any net any GW 192.168.3.1
Any net 10.211.6.0/24 GW 192.168.3.1
Any net 10.0.0.0 netmask 255.0.0.0 GW 10.212.52.1
Note: The file is not available on the host by default, and it can be because the/etc/init.d/network startup script calls the file, and the specific calling code is as follows:

The code is as follows:
# Add non interface-specific static-routes.
if [-f/etc/sysconfig/static-routes]; Then
grep "^any"/etc/sysconfig/static-routes | While read ignore args; Todo
/sbin/route add-$args
Done
Fi
Third, SuSE static routing configuration

Method 1: Through the/etc/sysconfig/network/routes configuration file

The code is as follows:
# Cat Routes
Default 192.168.3.1--
10.0.0.0 10.212.52.1 255.0.0.0 eth1
10.211.6.0 192.168.3.1 255.255.255.0 eth0
Note: The first one is configured as the default route, and the default route mask and interface can be replaced with "-". Note Non-default routes are not allowed to do this.

Method 2: Through the/etc/sysconfig/network/ifroute-* configuration file

/etc/sysconfig/network/ifroute-*, the asterisk here represents the corresponding device number, such as the above configuration, which can be configured using the following:

The code is as follows:
# Cat/etc/sysconfig/network/ifroute-eth0
0.0.0.0 192.168.3.1 0.0.0.0 eth0
10.211.6.0 192.168.3.1 255.255.255.0 eth0
# cat/etc/sysconfig/network/ifroute-eth1
10.0.0.0 10.212.52.1 255.0.0.0 eth1
You can also refer to the Novell site documentation or SUSE official documentation. In the Novell site documentation, the following paragraph describes:

The code is as follows:
Define an additional configuration file:/etc/sysconfig/network/ifroute-*. Replace * With the name of the interface.
The entries in the routing configuration files look like this:
Destination GATEWAY NETMASK INTERFACE [TYPE] [OPTIONS]
Destination GATEWAY Prefixlen INTERFACE [TYPE] [OPTIONS]
Destination/prefixlen gateway-interface [TYPE] [OPTIONS]
You can see that the four options that must be configured are: Target segment or host interface gateway address target segment Mask interface

Four, view the routing table

After the configuration is complete, you can access the routing table information by restarting the Network service and by using any of the following commands:

The

Code is as follows:
# NETSTAT-NR
# route-n
# IP Route list
# IP route show

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.