Last modified: April 18, 2022
This article is written in: 🇺🇸
Networking is the practice of connecting computers and devices so that they can communicate and exchange data. It forms the backbone of the internet, local area networks, and even small home networks. To grasp the intricacies of networking, it's imperative to familiarize oneself with key terminologies and concepts.
TODO:
Understanding the fundamentals can pave the way for more advanced networking tasks. Here's a breakdown of basic networking terms:
A network interface serves as the point of interconnection between a device and a network.
+--------------------------------------------------------+
| COMPUTER SYSTEM |
| |
| +------------------------------------------------+ |
| | OPERATING SYSTEM | |
| | | |
| | +--------------+ +--------------+ | |
| | | APPLICATION | <-> | APPLICATION | | |
| | +--------------+ +--------------+ | |
| | ... | |
| | +----------------------------------------+ | |
| | | NETWORK STACK | | |
| | +----------------------------------------+ | |
| +------------------------------------------------+ |
| | | |
| +-------+-------+ +-------+-------+ |
| | NETWORK CARD | | WIRELESS CARD | |
| +---------------+ +---------------+ |
| |
+--------------------------------------------------------+
Different types of network interfaces include:
Loopback interface (lo)
: It's primarily for internal communication within the device. It usually has an IP address of 127.0.0.1
. You can use it to access a locally hosted website on your device. It's important to note that this interface is not accessible from other devices.
Ethernet interface (eth0)
: This interface connects devices to a local area network (LAN). If you're running Linux, even within a virtual machine (VM), you'll often find an eth0 interface. For network and internet access, ensure this interface is active and assigned an IP address.
A MAC (Media Access Control) address is a hardware-based unique identifier for every network interface. It's used for device identification and tracking on a network. The MAC address is either hardcoded into a physical network card at the time of manufacturing or assigned to a virtual adapter during its creation. A typical MAC address format is: aa:bb:cc:dd:ee:ff
.
+-----------------------------------------+
| Manufacturer ID | Device Identifier |
+-----------------------------------------+
xx:xx:xx : xx:xx:xx
To retrieve the MAC address of a network interface on a Linux-based system, the ip link
command is useful:
ip link show
This command's output will enumerate details about all the network interfaces present on your system. The MAC address is highlighted adjacent to the link/ether field.
For example:
2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
In the provided example, the MAC address associated with the eth0 interface is 00:11:22:33:44:55
.
An IP (Internet Protocol) address is a unique numerical label assigned to every device participating in a computer network that uses the Internet Protocol for communication. IP addresses help in identifying and locating devices on a network, ensuring proper routing of data packets. Contrary to a common misconception, IP addresses range from 0.0.0.0
to 255.255.255.255
. While a device can possess multiple IP addresses, each must be unique within its network.
IPv4 Address: 192.168.1.10
+-----+-----+-----+-----+
| 192 | 168 | 1 | 10 |
+-----+-----+-----+-----+
| | | |
| | | +--- Host ID (Identifies device in local network)
| | +-------- Subnet (Often represents different segments of a network)
| +----------- Private Address Space (Commonly used in local networks)
+-------------- Network ID (Identifies the specific network)
Private IP addresses are reserved for internal use within a local network and are not routable on the public internet. Devices within the same local network communicate using these private IPs. Routers or other Network Address Translation (NAT) devices translate these addresses to a public IP when accessing the internet. Here are the typical reserved ranges for private IP addresses:
10.0.0.0
to 10.255.255.255
172.16.0.0
to 172.31.255.255
192.168.0.0
to 192.168.255.255
+---------------------------------+
| Private IP Address |
+---------------------------------+
| |
+------|---------++-----------------++---|-------------+
| 10.x.x.x || 172.16.x.x || 192.168.x.x |
| to || to || to |
| 10.255.255.255 || 172.31.255.255 || 192.168.255.255 |
+----------------++-----------------++-----------------+
| | |
| | +-----> Commonly used in home networks,
| | small offices, etc.
| |
| +--------> Used by medium-sized enterprises due to
| the larger subnetting options it offers.
|
+---------> Rarely used in home networks but can be found in
larger enterprises due to its vast address space.
To retrieve your device's private IP address, utilize the terminal with this command:
ip -4 address show
The output showcases details about your network interfaces and their corresponding IPv4 addresses. Specifically, search for the inet
label which is immediately followed by the IP address.
Sample output:
2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
In the depicted sample, the private IP address for the eth0
interface is 192.168.1.10
.
A public IP address uniquely identifies your network on the worldwide internet. Internet Service Providers (ISPs) assign this address to your router or modem, making it the external face of your network. Whenever you interact with a website or an online service, this public IP address is the identifiable source of your connection.
Internet
+----------------+
| |
| WWW Cloud |
| |
+--------+-------+
|
| Public IP (e.g., 203.0.113.10)
|
+--------+-------+
| Router |
+--------+-------+
/ | \
/ | \
Private IP Private IP Private IP
(e.g., 192.168.1.2) (e.g., 192.168.1.3) (e.g., 192.168.1.4)
Device A Device B Device C
For identifying your public IP address, third-party services can be handy. Using tools like curl
in combination with online utilities such as ifconfig.me
, ipify.org
, or icanhazip.com
can fetch your public IP. Here's how you can do it:
curl ifconfig.me
Your public IP address will be displayed as an output. For instance:
203.0.113.10
Note: Since public IP addresses are exposed to the internet, they're susceptible to malicious actors and potential cyber threats. It's crucial to ensure proper security measures like firewalls and VPNs to protect your network.
As networks grow and accommodate more devices, manually assigning IP addresses to each one becomes cumbersome and inefficient. DHCP, or Dynamic Host Configuration Protocol, automates this process, enabling seamless IP address allocation.
Device (DHCP Client) DHCP Server
| |
| |
| 1. DHCPDISCOVER |
|--------------------------------->|
| |
| |
| 2. DHCPOFFER |
|<---------------------------------|
| |
| |
| 3. DHCPREQUEST |
|--------------------------------->|
| |
| |
| 4. DHCPACK |
|<---------------------------------|
| |
| |
When a device, often referred to as a DHCP client, joins a network, it sends out a broadcast message requesting an IP address. If a DHCP server is present within the network, it responds by assigning an available IP address to that device. To ensure no IP address conflicts arise, the DHCP server maintains a record of all allocated IP addresses, thus preventing the same address from being assigned to multiple devices.
To determine if a device is utilizing DHCP, inspect its network configuration or interface information. The presence of the term "dynamic" often indicates an IP address assigned via DHCP.
While DHCP is incredibly valuable, it might not be suitable for every scenario. Devices that necessitate consistent, unchanging IP addresses—such as servers or printers—might be better off with a static IP assignment. This can be achieved in two ways:
On Linux systems, DHCP client configurations can often be found and modified within the /etc/dhcp/dhclient.conf
file. Editing this file allows users to define custom configurations for obtaining IP addresses from a DHCP server.
In conclusion, DHCP streamlines the IP address management process, especially for larger networks or environments with frequently changing devices. However, for infrastructure components that require a stable IP address, static assignments or reservations are recommended.
Networking commands are essential for configuring, managing, and troubleshooting network connections on a system. Below are some commonly used commands and their typical use-cases:
Historically one of the primary tools for network configuration on Linux systems, ifconfig
displays information about all active network interfaces, including their IP addresses, MAC addresses, and more.
Usage:
ifconfig
ifconfig eth0
Note: While ifconfig
is still widely used, it's considered deprecated in many modern Linux distributions in favor of the ip
command.
The ip
command is a versatile and powerful tool for network administration, replacing functionalities previously offered by ifconfig
, route
, and others.
Usage:
ip addr show
ip link show
ip -s link
ip -6 route show
The ping
command is a network diagnostic tool used to test the connectivity between your computer and another host, usually specified by an IP address or a domain name. It works by sending ICMP echo request packets to the target host and waits for a reply.
Usage:
ping
followed by the domain or IP. For example, ping google.com
. It displays round-trip times for each packet.-c
. For example, ping -c 5 google.com
sends only 5 packets.-t
. For instance, ping -t 5 google.com
will stop after 5 seconds.ping
with the address.ping
can indicate packet loss, a sign of network issues.This tool provides network statistics. It's useful for displaying active network connections, listening ports, and network protocol statistics.
Usage:
netstat -a
netstat -l
traceroute
helps in identifying the route taken by packets across a network. It's particularly useful for troubleshooting network slowdowns and failures.
Usage:
traceroute
followed by the domain name. For example, traceroute google.com
.-m
to set the maximum number of hops (routers) traceroute
will probe. For instance, traceroute -m 30 google.com
.traceroute
uses ICMP. To use TCP or UDP, use -T
or -U
respectively. For example, traceroute -T google.com
.-s
, you can specify the size of the probing packets. This can be useful to understand how packet size affects routing. For example, traceroute -s 60 google.com
.-w
option sets how long traceroute
waits for a response from each hop. For example, traceroute -w 5 google.com
.traceroute
resolves IP addresses to hostnames. Use -n
to show numeric IP addresses only.The route
command is a crucial tool for managing the IP routing table in Unix-based systems. This table controls how packets are forwarded and routed between different networks and hosts.
Usage:
route -n
: Displays the routing table in a numeric format. This provides an overview of routes with their destination, gateway, netmask, flags, and other associated metrics. Numeric format ensures IP addresses are displayed rather than hostnames.route add default gw IP_ADDRESS
: Sets the default gateway for the system. Replace IP_ADDRESS
with the IP address of the desired gateway. This effectively directs packets destined for networks not explicitly listed in the routing table to be sent to this gateway.route add -host IP_ADDRESS gw GATEWAY_IP
: Directs traffic intended for a specific host (given by IP_ADDRESS
) to be routed through the specified gateway (GATEWAY_IP
).route add -net NETWORK_IP netmask NETMASK gw GATEWAY_IP
: Routes traffic for an entire network range (NETWORK_IP
with the given NETMASK
) through the specified gateway.route add -host IP_ADDRESS reject
: This command prevents any traffic from being routed to the specified host IP address. Useful for intentionally blocking access to or from a particular host.route del default
: Removes the default gateway, which can halt all outbound traffic unless there are specific routes available or another default route is set.route del -host IP_ADDRESS
: Removes the route for a specific host.route del -net NETWORK_IP netmask NETMASK
: Removes the route for a specific network range.The changes made using the route
command are temporary and will be lost after a system reboot. To make routes persistent across reboots:
/etc/network/interfaces
./etc/sysconfig/network-scripts/
directory named route-INTERFACE_NAME
(e.g., route-eth0
).ip
command or network management systems like NetworkManager or systemd-networkd, which offer mechanisms for persistent route configurations.Network Manager is a versatile service on Linux systems responsible for managing network configurations, making it easier to handle network resources on both desktops and servers.
+------------+ +-------------+ +------------+
| | | | | |
| User GUI <------> Network <-----> Network |
| Tools | | Manager | | Interfaces |
| (nmtui, | | Daemon | | (eth0, wlan0,..)
| nm-applet) | | | | |
| | | | | |
+------------+ +------^------+ +------------+
|
|
+----v----+
| |
| D-Bus |
| |
+----^----+
|
|
+-----v------+
| |
| System |
| Services |
|(DNS, DHCP, |
| VPN,...) |
| |
+------------+
I. Interfaces
nmcli
, lets you handle all networking tasks from the terminal.II. Versatile Connectivity Support
Network Manager is adept at handling a variety of connection types, not just wired networks. Its capabilities extend to:
III. Network Profiles and Configurations
One of the key strengths of Network Manager is its ability to manage multiple network profiles:
nmcli -t -f RUNNING general
: Determines Network Manager's state. Outputs either "running" or "stopped" based on its current state.
nmcli con reload
: Useful after manually editing the network configuration files. This command reloads the settings.
nmcli con show
: Lists all saved network connection profiles.
nmcli dev status
: Showcases the status of all network devices recognized by Network Manager.
Setting a static IP can be essential for devices that should have a consistent IP, like servers or specific workstations. Here's the command structure:
CODE_BLOCK_PLACEHOLDER`
For example, to assign the IP 192.168.1.10
with a subnet mask of 255.255.255.0
(prefix 24
) and gateway 192.168.1.1
to eth0
, execute:
nmcli con add con-name [interface] type ethernet ifname [interface] ipv4.method manual ipv4.address [IP address]/[network prefix] ipv4.gateway [default gateway]
For devices that don't need a fixed IP, obtaining one dynamically via DHCP is the way to go:
nmcli con add con-name eth0 type ethernet ifname eth0 ipv4.method manual ipv4.address 192.168.1.10/24 ipv4.gateway 192.168.1.1
For eth0
:
nmcli con add con-name [interface] type ethernet ifname [interface] ipv4.method auto
nmtui
nmtui
, or Network Manager Text User Interface, is an excellent alternative for those operating on a system without a GUI or who find the nmcli
command line interface a bit intimidating. It strikes a balance by providing a user-friendly, text-based interface for managing network settings.
To launch nmtui
, simply enter the following in your terminal:
nmcli con add con-name eth0 type ethernet ifname eth0 ipv4.method auto
This command opens up a straightforward, menu-driven interface where you can navigate using your keyboard to configure network settings. It's particularly useful for:
Once you've made your adjustments and saved them within nmtui
, you can apply these changes by restarting the Network Manager service. This ensures that your network configurations are updated and active. To restart Network Manager, use:
nmtui
The Domain Name System (DNS) serves as the internet's phonebook. It allows users to input human-friendly domain names, like www.example.com, and translates them into IP addresses that computers use for communication.
systemctl restart NetworkManager
/etc/hosts
file to see if there's a stored mapping for the requested domain to an IP address./etc/hosts
doesn't have the needed mapping, the system consults the /etc/resolv.conf
file to determine which DNS server it should query.Changing DNS servers can provide various benefits, including faster browsing, improved security, and the ability to bypass regional restrictions on websites. Adjusting your DNS settings can help you take advantage of these features.
I. Using nmtui
The Network Manager Text User Interface (nmtui
) is a user-friendly, text-based tool for modifying network configurations, including DNS settings. To adjust DNS configurations:
nmtui
.II. Direct Configuration File Edits
Editing configuration files manually is another method to set DNS servers. Follow these steps:
/etc/sysconfig/network-scripts
.ifcfg-eth0
for the primary Ethernet connection.DNS1
, DNS2
, etc., to the desired DNS server IP addresses.Example configuration:
User's Device Local DNS Resolver Root & Top-Level
| | Domain (TLD) Servers
| | |
| 1. Request | |
| "www.example.com" | |
|------------------------------>| |
| | |
| 2. Ask Root Server | |
|------------------------------>| |
| | 3. Reply with .com Server|
| |<-------------------------|
| | |
| | |
| 4. Ask .com Server | |
|------------------------------>| |
| | 5. Reply with IP for |
| | "www.example.com" |
| |<-------------------------|
| | |
| | |
| 6. Return IP to User's Device| |
|<------------------------------| |
| | |
| | |
After making these changes, restart the network service to apply them.
III. Verifying DNS Configuration
To check the active DNS settings, inspect the /etc/resolv.conf
file. This file lists the DNS servers your system is using, identified with nameserver
tags.
For example, /etc/resolv.conf
might contain:
DEVICE=eth0
...
DNS1=8.8.8.8
DNS2=8.8.4.4
DNS issues can arise due to misconfigurations, unreachable DNS servers, or delays in DNS record updates. When encountering difficulties accessing websites, it's crucial to determine if DNS is the underlying problem.
Potential Indicators of DNS Issues:
Tools for DNS Diagnostics:
I. dig
dig
is a powerful tool for conducting detailed DNS queries, providing comprehensive information including the answer, authority, and additional sections.dig www.example.com
.II. nslookup
nslookup www.example.com
.III. host
host
is used for DNS lookups to quickly find the IP address of a domain or the domain of an IP address.host www.example.com
.The default gateway is a critical networking concept, functioning as the intermediary device, typically a router, which forwards network traffic from the local network to other distant networks or the internet. It's the "gate" between two networks, and it acts as the default route when no specific path is defined for a data packet.
nameserver 8.8.8.8
nameserver 8.8.4.4
You can quickly determine the currently configured default gateway on a Linux system with the following command:
+----------------+ +---------------+ +---------------------+
| Local Device A | | Local | | External Device/ |
| 192.168.1.2 |-----| Network |-----| Internet |
+----------------+ | 192.168.1.0/24| +---------------------+
| Gateway: |
+----------------+ | 192.168.1.1 |
| Local Device B | +---------------+
| 192.168.1.3 |
+----------------+
This command fetches the routing table, filters out the default route, and then extracts the IP address of the default gateway.
While the ip command has largely replaced route for many network configurations, you can still use route to manage the default gateway:
I. Set a Default Gateway
The following command establishes a default gateway, routing all external traffic through the specified IP address:
ip route show | grep 'default' | awk '{print $3}'
II. Remove the Default Gateway
If you need to remove the currently configured default gateway, perhaps for troubleshooting or to set a new one, use:
route add default gw 192.168.1.254
The ip command provides more advanced features and is now the preferred tool for many network configuration tasks:
I. Set a Default Gateway
route del default
II. Remove the Default Gateway
ip route add default via 192.168.1.254
Packet analysis, often termed packet sniffing, delves into the observation and detailed examination of network traffic. By capturing the individual packets—data's essential building blocks—circulating between network devices, administrators and security experts can:
ip route del default
tcpdump
is an essential packet analysis tool for Linux, providing powerful capabilities for packet capture and analysis from the command line.
Example of Packet Capture with tcpdump
:
To capture packets on the eth0 network interface and save them to a file:
+-----------------------+
| Internet |
+-----------------------+
|
|
v
+--------------+ +-------+-------+ +---------------+
| Source | ====> | Packet River | ====> | Destination |
| Device | <==== | | <==== | Device |
+--------------+ +-------+-------+ +---------------+
^
|
[Packet Analysis Tool]
/ | \
/ | \
Source Data Destination
Address Address
Explanation:
-i eth0
: Selects the eth0 network interface for capturing packets.-w traffic.pcap
: Directs tcpdump to write the captured packets to traffic.pcap file.Advanced Options in tcpdump:
I. Limiting Packet Capture (-c
)
Set a specific number of packets to capture. For example, -c 10
will limit the capture to 10 packets:
tcpdump -i eth0 -w traffic.pcap
II. Setting Snapshot Length (-s
)
Defines the maximum amount of each packet to capture, measured in bytes. -s 100
captures the first 100 bytes of each packet:
tcpdump -i eth0 -w traffic.pcap -c 10
III. Using Packet Filters (-f
)
Filters capture to specific packet types or criteria. For example, capturing only HTTP traffic (typically port 80):
tcpdump -i eth0 -w traffic.pcap -s 100
IP forwarding, sometimes referred to as packet forwarding or routing, facilitates the relay of data packets across different networks.
tcpdump -i eth0 -w traffic.pcap -f "port 80"
This mechanism is important for:
Activating and Verifying IP Forwarding:
I. Check the current IP forwarding status
+-------------+ +------------+ +-------------+
| Network A | | | | Network B |
| 192.168.1.0 |-------| IP |-------| 10.0.1.0 |
| /24 | | Forwarding | | /24 |
+-------------+ | Device | +-------------+
| (Router) |
+-------------+ | | +-------------+
| Network C | | | | Network D |
| 10.0.2.0 |-------| |-------| 172.16.1.0 |
| /24 | +------------+ | /24 |
+-------------+ +-------------+
II. Temporarily enable IP forwarding
cat /proc/sys/net/ipv4/ip_forward
III. Permanently enable IP forwarding
Modify the /etc/sysctl.conf
file, appending these configurations:
sysctl -w net.ipv4.ip_forward=1 # For IPv4 forwarding
sysctl -w net.ipv6.conf.all.forwarding=1 # For IPv6 forwarding
Apply the changes and restart the network services:
net.ipv4.ip_forward=1 # Activates IPv4 forwarding
net.ipv6.conf.all.forwarding=1 # Activates IPv6 forwarding
Note: IP forwarding should be enabled judiciously, keeping security considerations in mind. When active, it allows the device to forward packets from one network to another, which, if not secured correctly, can be a potential vulnerability.
Network troubleshooting is a crucial skill for IT professionals. Adopting a systematic and structured approach to troubleshooting can hasten the resolution of network issues.
Steps for Network Troubleshooting:
I. Verify Network Connection and Settings
Ensure the network interface is active and its configuration is correct.
sysctl -p /etc/sysctl.conf
service network restart # For RedHat and related distributions
/etc/init.d/networking restart # For Debian and its derivatives
Example of Incorrect Output for ip link
:
ip link
ip -4 address
The state DOWN shows that the interface is not active, which is a sign of a problem.
Example of Incorrect Output for ip -4 address
:
2: eth0: <broadcast,multicast> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
An IP in the 169.254.x.x range suggests a failure in DHCP configuration or a lack of connectivity with the DHCP server, often seen in Windows as "Limited Connectivity".
II. Inspect Routing Table
The routing table guides packet direction. Confirm routes, particularly the default gateway, are correctly configured.
inet 169.254.x.x/16 brd 169.254.x.x scope global dynamic eth0
Incorrect Output for ip route
:
ip route
route -n
If the default gateway IP doesn’t match your network's actual gateway, or if the network route points to the wrong interface (like eth1 instead of eth0), there's a configuration issue.
Incorrect Output for route -n
:
default via 192.168.1.1 dev eth0 metric 202
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.3
A gateway of 0.0.0.0 or a mismatched subnet suggests routing issues that could hinder network communication.
III. Examine Firewall Rules
Firewalls can block or allow specific traffic. Verify that firewall settings are correctly configured to permit essential traffic and block potential threats.
On Linux:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 UG 0 0 0 eth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
On Windows:
iptables -L
IV. Monitor Network Traffic
Utilize tools like tcpdump and Wireshark for packet inspection, aiding in spotting unusual patterns or malicious activities.
Capturing packets with tcpdump
:
netsh advfirewall firewall show rule name=all
What to Look For:
Example of Potential Issue:
tcpdump -i eth0
Repeated lines like this could suggest a potential network scan or attack attempt.
Using netstat
to Review Network Statistics and Active Connections:
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
23:45:10.123456 IP [suspicious IP] > [your IP].http: Flags [S], seq 123456789:123456890, win 65535, length 0
What to Look For:
Example of Potential Issue:
netstat -s
Here, a high number of retransmitted segments could point to network congestion or reliability issues.
V. Assess Physical Hardware
Hardware problems are common culprits. Look for:
VI. Reset Network Settings or Services
Sometimes restarting network services can resolve issues due to temporary glitches.
On Linux:
Tcp:
5 active connections openings
20 passive connection openings
2 failed connection attempts
25 retransmitted segments
3 resets sent
On Windows (replace 'service_name' with the actual service name):
systemctl restart networking
ip
command. Explain the purpose of each setting and discuss how IP addressing impacts network communication./etc/resolv.conf
file to set up a DNS server, then test DNS resolution using tools like dig
, nslookup
, or host
. Discuss the role of DNS in network communication and how it translates domain names to IP addresses.ping
or traceroute
and explain how IP forwarding is essential in network routing.tcpdump
to capture network packets on a specified interface and save the output to a file. Analyze the captured data to identify key patterns, potential network issues, or unusual traffic. Discuss how packet capture helps in network troubleshooting and security monitoring.traceroute
to map the network path to a remote IP address and identify any potential bottlenecks or delays in the route. Discuss how traceroute reveals the path data packets take and why certain hops might exhibit higher latency.netstat
or ss
. Identify which services are listening on which ports, and explain how these tools can help monitor network services, detect potential issues, and enhance security.