|$ curl https://forge-ai.dev/api/markdown?path=docs/linux/networking
$cat docs/networking.md
updated Recently·22 min read·published
Networking
Introduction
Every server, container, and cloud instance relies on Linux networking. Knowing how to inspect interfaces, diagnose connectivity, and secure traffic is essential for backend engineers, DevOps, and anyone deploying production workloads.
Network Interfaces
interfaces.sh
Bash
| 1 | # Modern tool: ip |
| 2 | ip addr show |
| 3 | ip link show |
| 4 | |
| 5 | # Bring an interface up or down |
| 6 | sudo ip link set eth0 up |
| 7 | sudo ip link set eth0 down |
| 8 | |
| 9 | # Add or remove an IP address |
| 10 | sudo ip addr add 192.168.1.50/24 dev eth0 |
| 11 | sudo ip addr del 192.168.1.50/24 dev eth0 |
| 12 | |
| 13 | # Routing table |
| 14 | ip route show |
| 15 | ip route get 8.8.8.8 |
📝
note
The older ifconfig and route commands still work on many systems, but ip is the modern replacement and should be preferred.
Sockets & Listening Ports
sockets.sh
Bash
| 1 | # Modern replacement for netstat |
| 2 | ss -tlnp # TCP listening ports with process |
| 3 | ss -tunlp # TCP and UDP listening with process |
| 4 | ss -s # socket statistics |
| 5 | |
| 6 | # Legacy netstat (still common) |
| 7 | netstat -tlnp |
| 8 | |
| 9 | # Check if a port is open locally |
| 10 | curl -v http://localhost:3000 |
HTTP with curl
curl.sh
Bash
| 1 | # Simple GET |
| 2 | curl https://api.example.com/users |
| 3 | |
| 4 | # Follow redirects, show headers |
| 5 | curl -L -I https://forgelearn.dev |
| 6 | |
| 7 | # POST JSON |
| 8 | curl -X POST https://api.example.com/users \ |
| 9 | -H "Content-Type: application/json" \ |
| 10 | -d '{"name":"alice","role":"admin"}' |
| 11 | |
| 12 | # Save response to file |
| 13 | curl -o output.json https://api.example.com/data |
| 14 | |
| 15 | # Show timing and verbose output |
| 16 | curl -w "@curl-format.txt" -o /dev/null -s https://forgelearn.dev |
| 17 | curl -v https://forgelearn.dev |
DNS
dns.sh
Bash
| 1 | # Lookup IP for a domain |
| 2 | nslookup forgelearn.dev |
| 3 | host forgelearn.dev |
| 4 | dig forgelearn.dev |
| 5 | |
| 6 | # Query a specific nameserver |
| 7 | dig @8.8.8.8 forgelearn.dev |
| 8 | |
| 9 | # Reverse DNS |
| 10 | dig -x 8.8.8.8 |
| 11 | |
| 12 | # DNS resolution config |
| 13 | cat /etc/resolv.conf |
Firewall
firewall.sh
Bash
| 1 | # ufw (Uncomplicated Firewall) — Debian/Ubuntu |
| 2 | sudo ufw status |
| 3 | sudo ufw allow 22/tcp |
| 4 | sudo ufw allow 80/tcp |
| 5 | sudo ufw allow 443/tcp |
| 6 | sudo ufw enable |
| 7 | |
| 8 | # iptables / nftables |
| 9 | sudo iptables -L -v -n |
| 10 | sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT |
⚠
warning
Before enabling a firewall on a remote server, make sure you have allowed your SSH port (usually 22) so you do not lock yourself out.
Troubleshooting
troubleshooting.sh
Bash
| 1 | # Test connectivity |
| 2 | ping 8.8.8.8 |
| 3 | ping forgelearn.dev |
| 4 | |
| 5 | # Trace route |
| 6 | mtr forgelearn.dev |
| 7 | traceroute forgelearn.dev |
| 8 | |
| 9 | # Check DNS resolution |
| 10 | getent hosts forgelearn.dev |
| 11 | |
| 12 | # Inspect TLS certificate |
| 13 | openssl s_client -connect forgelearn.dev:443 -servername forgelearn.dev </dev/null |