Skip to content

Instantly share code, notes, and snippets.

@thesaadarshad
Last active November 24, 2024 22:56
Show Gist options
  • Save thesaadarshad/7673a187df785393ac858778a215a5be to your computer and use it in GitHub Desktop.
Save thesaadarshad/7673a187df785393ac858778a215a5be to your computer and use it in GitHub Desktop.
TCP/IP
# Comprehensive Guide: TCP/IP, Networks, and Linux Commands
TCP/IP is the foundation of modern networking. This guide provides an in-depth explanation of its components, layers, and related Linux commands for system developers and network engineers.
---
## 1. TCP/IP Overview
TCP/IP (Transmission Control Protocol/Internet Protocol) is a suite of communication protocols used to interconnect network devices.
### Layers of TCP/IP
1. **Application Layer**: Interfaces between user applications and network services.
- Protocols: HTTP, FTP, SMTP, DNS
2. **Transport Layer**: Provides end-to-end communication and reliability.
- Protocols: TCP, UDP
3. **Internet Layer**: Handles addressing, routing, and delivery.
- Protocols: IP, ICMP, ARP
4. **Network Access Layer**: Defines protocols for hardware addressing and media access.
- Technologies: Ethernet, Wi-Fi, PPP
---
## 2. TCP/IP Protocols
### Internet Protocol (IP)
- Responsible for addressing and routing packets.
- **IPv4**: 32-bit addressing (e.g., 192.168.1.1)
- **IPv6**: 128-bit addressing (e.g., 2001:db8::1)
### Transmission Control Protocol (TCP)
- Connection-oriented protocol ensuring reliable data delivery.
- Features: Sequencing, error checking, retransmission.
### User Datagram Protocol (UDP)
- Connectionless protocol used for speed-sensitive applications.
- No sequencing or error recovery.
### Internet Control Message Protocol (ICMP)
- Used for diagnostics (e.g., `ping`) and error reporting.
### Address Resolution Protocol (ARP)
- Resolves IP addresses to MAC addresses.
---
## 3. Networking Concepts
### Fragmentation
- Splitting large packets into smaller ones to fit MTU (Maximum Transmission Unit).
- Managed by IP.
### Ports
- Logical endpoints for communication.
- **Well-known ports**:
- HTTP: 80
- HTTPS: 443
- SSH: 22
### Packet Sniffing
- Capturing network packets for analysis.
- Tools: `tcpdump`, `wireshark`
### Routing
- Determines the path data takes from source to destination.
### Firewalls
- Control incoming and outgoing traffic.
- Tools: `iptables`, `ufw`
---
## 4. Essential Linux Commands for Networking
### General Networking
| Command | Description | Example |
|---------------|-----------------------------------------------|---------------------------------------|
| `ip addr` | Display IP address and interface info. | `ip addr show` |
| `ifconfig` | Legacy command for IP and interface info. | `ifconfig eth0` |
| `netstat` | Show network stats and connections. | `netstat -tuln` |
| `ss` | Modern replacement for `netstat`. | `ss -tuln` |
| `ping` | Test connectivity to a host. | `ping -c 4 google.com` |
| `traceroute` | Show the route packets take to a host. | `traceroute example.com` |
| `dig` | Query DNS records. | `dig example.com` |
| `nslookup` | Perform DNS lookups. | `nslookup example.com` |
| `arp` | View ARP table (IP to MAC mappings). | `arp -a` |
| `ethtool` | View and configure NIC settings. | `ethtool eth0` |
### Packet Analysis
| Command | Description | Example |
|---------------|-----------------------------------------------|---------------------------------------|
| `tcpdump` | Capture and analyze packets. | `sudo tcpdump -i eth0` |
| `wireshark` | GUI tool for packet analysis. | `wireshark` |
### Firewall Management
| Command | Description | Example |
|---------------|-----------------------------------------------|---------------------------------------|
| `iptables` | Manage firewall rules. | `iptables -L -v -n` |
| `ufw` | Simplified firewall management. | `ufw allow 22/tcp` |
### Routing
| Command | Description | Example |
|---------------|-----------------------------------------------|---------------------------------------|
| `ip route` | Display and configure routing table. | `ip route show` |
| `route` | Legacy command to display routing table. | `route -n` |
---
## 5. Troubleshooting TCP/IP Issues
### Commands
| Command | Description | Example |
|---------------|-----------------------------------------------|---------------------------------------|
| `traceroute` | Diagnose routing issues. | `traceroute example.com` |
| `ping` | Check connectivity. | `ping -c 4 8.8.8.8` |
| `tcpdump` | Capture and analyze packets. | `sudo tcpdump -i eth0 port 80` |
| `curl` | Test HTTP/S connections. | `curl -I https://example.com` |
| `wget` | Test file downloads. | `wget https://example.com/file` |
| `mtr` | Combine `ping` and `traceroute`. | `mtr example.com` |
### ICMP Diagnostics
- **Fragmentation**: Test MTU using `ping`:
```bash
ping -M do -s 1472 8.8.8.8
Network Configuration
Command Description Example
nmcli Manage network connections. nmcli dev status
nmtui Text-based interface for network management. nmtui
6. Kernel Networking
Commands
Command Description Example
sysctl View and set kernel parameters. `sysctl -a
ip link Bring interfaces up or down. ip link set eth0 up
modprobe Load kernel modules. modprobe nf_conntrack
7. Network Performance Metrics
Commands
Command Description Example
iftop Real-time network bandwidth usage. sudo iftop
nload Real-time traffic visualization. nload
iperf Measure network bandwidth. iperf -c <server_ip>
8. Advanced TCP/IP Commands
Command Description Example
tc Traffic control utility. tc qdisc show dev eth0
nstat Show network statistics. nstat
9. Practical Examples
Scenario: Diagnose Slow Network
Test connectivity:
ping 8.8.8.8
Check route:
traceroute example.com
Capture packets:
tcpdump -i eth0
Analyze bandwidth:
iftop
Scenario: Add a Firewall Rule
Allow SSH:
ufw allow 22/tcp
Deny HTTP:
iptables -A INPUT -p tcp --dport 80 -j DROP
This guide provides a comprehensive understanding of TCP/IP and Linux commands for network management and troubleshooting. Let me know if you'd like to expand or add more examples!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment