Linux hosts implement simple routing and forwarding functions

Source: Internet
Author: User

as long as the a Few simple routing entries are added to the Linux host, which can act as a simple router.

in the actual production of the bad environment, when we need a router as simple as forwarding data, but without a router on hand, we can designate a single The Linux host acts as a simple router, enabling simple forwarding of routes.

the above-mentioned complaints are especially simple, so just to implement the simple routing function, if you want to implement a very complex function, a single Linux hosts may not be able to implement the conditions you want.

experiment bad ": vmware 4 virtual host, host name r1 r2 two hosts ( all system ) acting as a router, and hostname centos7.2 (1) and centos7.2 (2) two hosts as test hosts.

here is an experimental topology diagram :

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/E3/wKiom1fOKcaSvJ37AACGoPXvw0I304.png "title=" Picture 1.png "alt=" Wkiom1fokcasvj37aacgopxvw0i304.png "/>

Here are the experimental steps:


1>. The firewall affects the routing forwarding feature, first emptying R1 and the R2 firewall rules on two hosts.

[Email protected] ~]# iptables-f[[email protected] ~]# iptables-f


2> Linux System, the default route forwarding function is closed, we need to modify the configuration file to open the route forwarding function.

routing forwarded configuration file /proc/sys/net/ipv4/ip_forward

View and set The /proc/sys/net/ipv4/ip_forward on the R1

[[email protected] ~]# Cat/proc/sys/net/ipv4/ip_forward0[[email protected] ~]# echo "1" >/proc/sys/net/ipv4/ip_ Forward[[email protected] ~]# cat/proc/sys/net/ipv4/ip_forward1


View and set The /proc/sys/net/ipv4/ip_forward on the R2

[[email protected] ~]# Cat/proc/sys/net/ipv4/ip_forward0[[email protected] ~]# echo "1" >/proc/sys/net/ipv4/ip_ Forward[[email protected] ~]# cat/proc/sys/net/ipv4/ip_forward1


3>. gave R1 Host eth0 and the eth1 Interface Configuration IP Address

[email protected] network-scripts]# cat ifcfg-eth0device=eth0hwaddr=00:0c:29:b3:32:cctype=ethernetuuid= A7f94e4f-1f87-439f-a044-9888cc8ddc7conboot=yesnm_controlled=nobootproto=noneipaddr=10.0.0.2prefix=8[[email Protected] network-scripts]# Cat ifcfg-eth1device=eth1type=ethernetonboot=yesnm_controlled=nonebootproto= Staticipaddr=172.16.0.1prefix=16


4>. gave R2 Host eth1 and the eth2 Interface Configuration IP Address

[email protected] network-scripts]# cat ifcfg-eth1device=eth1type=ethernetonboot=yesnm_controlled=yesbootproto= Staticipaddr=172.16.0.2prefix=16[[email protected] network-scripts]# cat ifcfg-eth2device=eth2bootproto=noneipaddr =192.168.1.1netmask=255.255.255.0


5>. temporarily designate a host Centos7.2 (1) and the Centos7.2 (2) IP address.

Temporary designation Centos7.2 (1) eno16777736 IP address :

[Email protected] sysconfig]# ifconfig eno16777736 10.0.0.1


Temporary designation Centos7.2 (2) eno16777736 IP address :

[Email protected] ~]# ifconfig eno16777736 192.168.1.2


6>. The IP address is already configured, now to add some routing rules on the router, you need to add the gateway configuration to the test host.

a>. in add A route entry to the 192.168.1.0 network segment on the R1 host

[Email protected] network-scripts]# Route add-net 192.168.1.0/24 GW 172.16.0.2 Dev eth1


View Host route entry for R1 :

[Email protected] network-scripts]# IP route192.168.96.0/24 dev eth2 proto kernel scope link src 192.168.96.137 metri C 1 192.168.1.0/24 via 172.16.0.2 dev eth1 172.16.0.0/16 dev eth1 proto kernel scope link src 172.16.0.1 metric 1 169. 254.0.0/16 Dev eth0 scope link metric 1002 10.0.0.0/8 dev eth0 proto kernel scope link src 10.0.0.2


b>. in add A route entry to the 192.168.1.0 network segment on the R2 host

[Email protected] network-scripts]# Route add-net 10.0.0.0/8 GW 172.16.0.1 Dev eth1


View Host route entry for R2 :

[Email protected] network-scripts]# IP route192.168.96.0/24 dev eth3 proto kernel scope link src 192.168.96.139 metri C 1 192.168.1.0/24 Dev eth2 proto kernel scope link src 192.168.1.1 metric 1 172.16.0.0/16 dev eth1 Proto kernel SCO PE link SRC 172.16.0.2 metric 1 10.0.0.0/8 via 172.16.0.1 Dev eth1


C> To test two hosts centos7.2 (1) and centos7.2 (2) Add the specified gateway

[[email protected] sysconfig]# route add-net default GW 10.0.0.2[[email protected] ~]# route add-net default GW 192.168. 1.1


7>. Test :

use centos7.2 (1) to Ping the host of centos7.2 (2) :

[Email protected] sysconfig]# ping 192.168.1.2PING 192.168.1.2 (192.168.1.2) (+) bytes of data.64 bytes from 192.168.1 .2:icmp_seq=1 ttl=62 time=16.2 ms64 bytes from 192.168.1.2:icmp_seq=2 ttl=62 time=1.02 ms64 bytes from 192.168.1.2:icmp _seq=3 ttl=62 time=1.39 ms64 bytes from 192.168.1.2:icmp_seq=4 ttl=62 time=1.06 ms ....


It is clear that two hosts are connected to one another.

Linux hosts implement simple routing and forwarding functions

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.