Prerequisite Switch Routing
- Switching - how does Host A reach Host B? Through a switch.
- The switch contains a network between the two systems.
- Require an interface on each host.
- Run
ip linkas a good way to check.
- Run
- Add IPs to each host:
ip addr add 192.168.1.10/24 dev eth0 ip addr add 192.168.1.11/24 dev eth0 - Routing - On one network with
192.168.1.0and another network of192.168.2.0, how do the two networks talk to each other?- Routers are therefore needed for this.
- To bridge the two networks, one address is assigned to one side of the router
192.168.1.1and192.168.2.1
- How does one Host on one Network know where to send its packet to on the second Network?
- Gateways - Use the
routecommand to show routing information such asDestination, Gateway, Genmask, Flags, Metric, Ref, Use, Ifaceroutedisplays the kernel’s routing table.- If there is nothing populated from the
routecommand, Host A on192.168.1.0can’t see any hosts on192.168.2.0and vice versa. - To therefore be able to reach hosts on
192.168.2.0, need theip route addcommand withip route add 192.168.2.0/24 via 192.168.1.1- Need to select the interface on the router closest to the network.
- Running the
routecommand again will see the routing table populated. - Needs to be configured on all systems, not just
ip route add 192.168.2.0/24 via 192.168.1.1, but also on the other side withip route add 192.168.1.0/24 via 192.168.2.1
- Default Gateway -
ip route add default via 192.168.2.1, then you don’t need to add individual IPs for everything on the Internet- Can also write it as
ip route add 0.0.0.0 via 192.168.2.1.0.0.0.0means all IPs. - For two separate routers, one for the outside Internet and one on the internal network.
- For example, you have the router from above and also the secondary router with an interface of
192.168.2.2. To add this, runip route add 192.168.1.0/24 via 192.168.2.2How to set up a Linux Host as a Router
- For example, you have the router from above and also the secondary router with an interface of
- Can also write it as
- Three hosts, Host A, Host B and Host C.
- Hosts A and B are connected to network
192.168.1.0 - Hosts B and C are connected to network
192.168.2.0 - Host B has left-hand side IP of
192.168.1.6and a right-hand side IP of192.168.2.6. - Host A has
192.168.1.5and Host C has192.168.2.5
- Hosts A and B are connected to network
- How does Host A talk to Host C?
- Add the following to Host A:
ip route add 192.168.2.0/24 via 192.168.1.6 - If the packages reach Host C, Host C needs to send a response to Host A.
- We need to tell Host C that it can reach Host A through Host B.
- We add the following to Host C:
ip route add 192.168.1.0/24 via 192.168.2.6 - Pinging from either Host A or Host C at this point and no
network unreachablemessages should be present. - By default in Linux, packets are not forwarded from one interface to the next.
- For example, packets received on Host B on Eth0 are not forwarded else here on Eth1.
- This is for security reasons, if Eth0 is on the private network and Eth1 is on the public network, don’t want anyone from the public network to easily send messages to the private network.
- Therefore need to make sure the
cat /proc/sys/net/ipv4/ip_forwardsays1 - To quickly change this, run
echo 1 > /proc/sys/net/ipv4/ip_forward - To persist between reboots:
/etc/sysctl.conf net.ipv4.ip_forward = 1
- For example, packets received on Host B on Eth0 are not forwarded else here on Eth1.
- Key commands:
ip link- list modifiable interfacesip addr- see IP addresses assigned.ip route/routeis to view the routing table.ip route add- add routes into the routing table.- To persist changes, need to add them to the
/etc/network/interfacesfile.