KodeCloud Networking Basics
Links: 111 KodeCloud Index
Recommended Reads: Linux Networking
Networking¶
Switching¶
- We can connect two computers using a switch
- To connect these computers to a switch we need an interface on each host.
- This interface can be physical or virtual.
The very first requirement of assigning an IP address to a system is that it must have a physical or virtual interface.
- To see the list of interfaces on the host we use the
ip link
command.- List the ip addresses of the interfaces
ip addr
- List the ip addresses of the interfaces
- Assuming switch has an IP address of
192.168.1.0
we assign IP addresses to our interfaces. - Once the IP addresses have been assigned the computers can now connect with each other.
Routing¶
- A router helps connect two networks.
Gateway¶
- We configure a gateway to let machine B know how to reach machine C.
- The system's need to know the address of the gateway.
- This can be done using the
route
command. This will display kernel's routing table.
- This can be done using the
- Adding a gateway
- Default gateway entry is used to route to the internet
- Any request outside of your network goes to the default gateway.
- Instead of default we can also use
0.0.0.0
Suppose if you have 2 different routers then you will need two different entries.
Using Linux Host as a router¶
- How do we connect A to C
- We add a routing table entry to A saying it has to go through B to reach C
ip route add 192.168.2.0/24 via 192.168.1.6
- Similarly we also need to let host C know that it can reach A using B.
ip route add 192.168.1.0/24 via 192.168.2.6
This still won't be enough because by default in linux packets are not forwarded.
This is for security reasons.
Suppose your eth0
is connected to internet and your eth1
is connected to your private network. We don't want anyone from the internet to send packets to your private network.
- Whether a host can forward packets is governed by
/proc/sys/net/ipv4/ip_forward
- By default value in this file is set to 0.
- Setting this to 1 should enable packet forwarding.
- Just changing this won't persist the changes on reboot we need to also change
/etc/sysctl.conf
DNS¶
We can configure a custom DNS resolver by adding its entry to /etc/resolv.conf
This is particularly useful when you have a lot of machines and you want to reference them using their names and don't want to modify each machine's /etc/hosts
file.
-
By default for DNS resolution the system first looks into the
/etc/hosts
file and then looks for the DNS resolver.- This order can be changed in
/etc/nsswitch.conf
- This order can be changed in
-
We can configure the custom DNS to forward all the requests to the public name servers (like google or Cloudflare) if the entry is not present in our custom DNS.
DNS troubleshooting tools like dig
and nslookup
doesn't consider the entries of /etc/hosts
file.
Search domain¶
- It is the domain name that gets appended.
- The host is intelligent enough that if you type
web.company.com
then it won't appendcompany.com
to it.
Must Watch¶
- Docker Networking Crash Course - YouTube - Great for understanding routing, DNS and docker networking.
Last updated: 2023-05-24