Linux routing configuration actual combat (different network segment communication)

Virtual machine configuration routing combat

Environmental description

virtual machine Description
centos7.0 Router 172.16.17.170 + 192.168.43.104
windows xp A Communication A end 192.168.43.79
ubuntu18.04 B1 Communication B end 172.16.17.181
ubuntu18.04 B2 Communication B end 172.16.17.192

Ensure that you can ping each other under the same network segment

Configuration process

The main thing is to configure the "router" so that it can forward packets between different network segments
(adding a network card will not repeat it, and directly describe the router configuration process)

1, open linux packet forwarding
open /etc/sysctl.conf
input stored net.ipv4.ip_forward=1
so that the commencementsudo sysctl -p

2. Routing communication
The two network cards of the router are ens33 and ens37, the purpose of which is to allow these two network cards to communicate

sudo iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE
sudo iptables -A FORWARD -i ens33:37 -o ens33 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i ens33 -o ens33:37 -j ACCEPT

sudo iptables -t nat -A POSTROUTING -o ens37 -j MASQUERADE
sudo iptables -A FORWARD -i ens37:33 -o ens37 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i ens37 -o ens37:33 -j ACCEPT

3. Add routing table
B1 (linux command): sudo route add -net 192.168.43.0/24 gw 172.16.17.170
A (windows command): route -p add 172.16.17.0 mask 255.255.255.0 192.168.43.104
specify routing path

test

A and B1 ping each other and the
Insert picture description here
Insert picture description here
configuration is successful

Guess you like

Origin blog.csdn.net/qq_42882717/article/details/112093227