Table of Contents
- Understanding Linux Networking: Key Components
- Essential Linux Networking Commands
- Configuring Network Interfaces
- Linux Firewalls: UFW (Uncomplicated Firewall)
- File Sharing: SCP and SFTP
- Troubleshooting Common Network Issues
- Conclusion
- References
1. Understanding Linux Networking: Key Components
Before diving into commands, let’s clarify the building blocks of Linux networking.
Network Interfaces
A network interface is the software or hardware component that connects a Linux machine to a network (e.g., Ethernet, Wi-Fi, or virtual networks like those used in Docker). Linux identifies interfaces with names like eth0 (traditional Ethernet), enp0s3 (predictable network interface names), wlan0 (Wi-Fi), or lo (loopback, a virtual interface for local communication).
IP Addressing
An IP address is a unique identifier assigned to a device on a network, enabling it to send/receive data. Linux supports two main IP versions:
IPv4
- 32-bit address (e.g.,
192.168.1.100), divided into four octets (0-255). - Limited to ~4.3 billion addresses (hence the shift to IPv6).
IPv6
- 128-bit address (e.g.,
2001:0db8:85a3:0000:0000:8a2e:0370:7334), written in hexadecimal with colons. - Virtually unlimited addresses (~3.4×10³⁸), designed to replace IPv4.
Subnetting and CIDR Notation
To organize networks, IP addresses are split into network (shared by all devices on the network) and host (unique to the device) portions using a subnet mask.
- Subnet Mask: A 32-bit number (for IPv4) that defines the network range. For example,
255.255.255.0means the first 24 bits are the network, and the last 8 are the host. - CIDR Notation: Shorthand for subnet masks (e.g.,
192.168.1.0/24=255.255.255.0). The/24indicates 24 network bits.
Gateway and DNS
- Gateway: The IP address of the router/device that connects your local network to external networks (e.g., the internet). Without a gateway, your machine can only communicate with devices on the same local network.
- DNS (Domain Name System): Translates human-readable domain names (e.g.,
google.com) to IP addresses (e.g.,142.250.185.142). Linux uses DNS servers listed in/etc/resolv.confor managed by tools likesystemd-resolved.
2. Essential Linux Networking Commands
Linux provides a rich set of command-line tools to manage and diagnose networks. Below are the most critical ones for beginners.
Checking Network Interfaces
To list all network interfaces and their status (up/down), use:
ip addr (Modern Tool)
Replaces the deprecated ifconfig and provides detailed interface info, including IP addresses, MAC addresses, and link status.
ip addr
Sample Output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:1a:b2:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.105/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3
valid_lft 86397sec preferred_lft 86397sec
lo: Loopback interface (always127.0.0.1).enp0s3: Physical Ethernet interface with IP192.168.1.105/24and MAC address08:00:27:1a:b2:c3.
ifconfig (Legacy Tool)
Still used on some systems, though deprecated. Install with sudo apt install net-tools (Debian/Ubuntu) or sudo dnf install net-tools (Fedora/RHEL) if missing.
ifconfig
Testing Connectivity
To verify if a device or service is reachable, use these tools:
ping
Sends ICMP echo requests to a target IP/domain to check connectivity.
# Ping a domain
ping google.com
# Ping an IP (limit to 4 packets with -c)
ping -c 4 8.8.8.8 # Google's public DNS
Output Explanation:
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=23.4 ms: Packet received successfully.Request timeout for icmp_seq=1: Target unreachable.
traceroute
Maps the path packets take from your machine to a target, showing intermediate routers (hops).
traceroute google.com
Sample Output:
traceroute to google.com (142.250.185.142), 30 hops max, 60 byte packets
1 router.local (192.168.1.1) 1.234 ms 1.123 ms 1.012 ms
2 10.0.0.1 (10.0.0.1) 10.456 ms 10.345 ms 10.234 ms
3 * * * # Hop 3 didn't respond
4 142.250.185.142 (142.250.185.142) 25.678 ms 25.567 ms 25.456 ms
mtr (Combines ping and traceroute)
A real-time tool that continuously pings all hops in the path. Install with sudo apt install mtr (Debian/Ubuntu) or sudo dnf install mtr (Fedora/RHEL).
mtr google.com
Viewing Active Connections
To list open ports, listening services, and active network connections:
ss (Modern Replacement for netstat)
Faster and more feature-rich than netstat. Use flags to filter by protocol (TCP/UDP) or state (listening, established).
# List all listening TCP ports
ss -tuln
# List established TCP connections
ss -t state established
Flags:
-t: TCP,-u: UDP,-l: Listening,-n: Numeric (no DNS lookup).
netstat (Legacy)
Still useful for familiarity, but ss is preferred.
netstat -tuln
DNS Queries
To debug DNS resolution (domain → IP translation):
nslookup
Queries DNS servers to resolve a domain.
nslookup google.com
Sample Output:
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
Name: google.com
Address: 142.250.185.142
Name: google.com
Address: 2607:f8b0:4005:805::200e
dig (Detailed DNS Info)
Shows raw DNS response data, including TTL (time-to-live) and DNS records (A, AAAA, MX).
dig google.com A # A record (IPv4)
dig google.com AAAA # AAAA record (IPv6)
3. Configuring Network Interfaces
Linux lets you configure network interfaces temporarily (resets on reboot) or permanently (survives reboots).
Temporary Configuration
Use the ip command to assign an IP address or bring an interface up/down temporarily.
Assign an IP Address
# Bring interface up (if down)
sudo ip link set enp0s3 up
# Assign a static IP (e.g., 192.168.1.100/24)
sudo ip addr add 192.168.1.100/24 dev enp0s3
# Set a default gateway (replace 192.168.1.1 with your router's IP)
sudo ip route add default via 192.168.1.1 dev enp0s3
Permanent Configuration
Permanent settings depend on your Linux distribution and network manager. Below are the most common methods.
Method 1: systemd-networkd (Systemd-Based Distros: Ubuntu 20.04+, Fedora, Arch)
systemd-networkd is a lightweight, systemd-native network manager. Configure interfaces via .network files in /etc/systemd/network/.
Example: DHCP Configuration
Create /etc/systemd/network/20-wired.network:
[Match]
Name=enp0s3 # Match interface by name
[Network]
DHCP=yes # Use DHCP to get IP/gateway/DNS
Example: Static IP Configuration
[Match]
Name=enp0s3
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8 8.8.4.4 # Google DNS
Apply changes:
sudo systemctl restart systemd-networkd
sudo systemctl enable systemd-networkd # Start on boot
Method 2: NetworkManager (User-Friendly, GUI/CLI: Ubuntu Desktop, Fedora)
NetworkManager simplifies configuration via nmcli (CLI) or GUI tools like nmtui.
Set Static IP with nmcli:
# List connections
nmcli con show
# Modify the connection (replace "Wired connection 1" with your connection name)
sudo nmcli con mod "Wired connection 1" \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8 8.8.4.4" \
ipv4.method manual
# Restart the connection
sudo nmcli con up "Wired connection 1"
Method 3: /etc/network/interfaces (Debian/Ubuntu Legacy)
Older Debian-based systems use /etc/network/interfaces (replaced by netplan in newer Ubuntu).
Example: Static IP
auto enp0s3 # Bring up on boot
iface enp0s3 inet static
address 192.168.1.100/24
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
Apply changes:
sudo systemctl restart networking
4. Linux Firewalls: UFW (Uncomplicated Firewall)
A firewall controls incoming/outgoing network traffic. UFW (Uncomplicated Firewall) simplifies managing iptables (Linux’s underlying firewall framework).
Install UFW
UFW is preinstalled on most Ubuntu systems. For others:
# Debian/Ubuntu
sudo apt install ufw
# Fedora/RHEL
sudo dnf install ufw
Basic UFW Commands
| Command | Purpose |
|---|---|
sudo ufw status | Check firewall status (inactive/active). |
sudo ufw enable | Enable firewall (starts on boot). |
sudo ufw disable | Disable firewall. |
sudo ufw default deny incoming | Block all incoming traffic (default). |
sudo ufw default allow outgoing | Allow all outgoing traffic (default). |
sudo ufw allow ssh | Allow SSH (port 22) for remote access. |
sudo ufw allow 80/tcp | Allow HTTP (port 80) for web servers. |
sudo ufw allow 443/tcp | Allow HTTPS (port 443). |
sudo ufw deny 23/tcp | Block Telnet (port 23). |
sudo ufw delete allow 80/tcp | Remove a rule. |
Example Workflow:
# Set defaults
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow essential services
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
# Enable firewall
sudo ufw enable
# Verify rules
sudo ufw status verbose
5. File Sharing: SCP and SFTP
Linux makes it easy to transfer files between machines securely using SSH-based tools like scp and sftp.
SCP (Secure Copy)
Copies files between local and remote machines over SSH.
# Copy local file to remote
scp /path/to/local/file.txt user@remote_ip:/path/to/remote/directory
# Copy remote file to local
scp user@remote_ip:/path/to/remote/file.txt /path/to/local/directory
# Copy a directory (recursive with -r)
scp -r /local/dir user@remote_ip:/remote/dir
SFTP (Secure File Transfer Protocol)
Interactive shell for browsing and transferring files over SSH.
# Connect to remote server
sftp user@remote_ip
# SFTP commands (similar to FTP)
get remote_file.txt # Download file
put local_file.txt # Upload file
ls # List remote files
cd remote_dir # Change remote directory
exit # Close connection
6. Troubleshooting Common Network Issues
Network problems are inevitable. Use this flowchart to diagnose issues:
Step 1: Check Interface Status
Is the interface up and has an IP?
ip addr show enp0s3 # Replace enp0s3 with your interface
- If no IP: Check DHCP (run
sudo dhclient enp0s3to force DHCP).
Step 2: Ping the Gateway
Is your machine connected to the router?
# Find gateway IP
ip route show default # Look for "default via <gateway_ip>"
# Ping gateway (e.g., 192.168.1.1)
ping -c 4 192.168.1.1
- If gateway unreachable: Check router power/cables.
Step 3: Ping a Public IP
Is the internet reachable?
ping -c 4 8.8.8.8 # Google's DNS (bypasses DNS)
- If 8.8.8.8 unreachable: Contact your ISP.
Step 4: Check DNS Resolution
Is domain-to-IP translation working?
nslookup google.com
- If “can’t resolve host”: Edit
/etc/resolv.confto add DNS servers (e.g.,nameserver 8.8.8.8).
Step 5: Check Firewall Rules
Is the firewall blocking traffic?
sudo ufw status
- If a port is blocked: Add a rule with
sudo ufw allow <port>/tcp.
7. Conclusion
Linux networking may seem daunting at first, but with the right tools and concepts, it becomes manageable. In this tutorial, you’ve learned:
- Core components: Interfaces, IP addresses, gateways, and DNS.
- Essential commands:
ip,ping,traceroute,ss, anddig. - How to configure interfaces temporarily or permanently.
- Firewall basics with UFW.
- File sharing with
scpandsftp. - Troubleshooting workflows for common issues.
The best way to master Linux networking is to practice: Experiment with static/DHCP configs in a virtual machine (e.g., VirtualBox), set up a small home network, or host a simple web server. With time, these tools will become second nature.
8. References
- Man Pages:
man ip,man ufw,man scp(run in terminal for official docs). - Ubuntu Networking Guide: Ubuntu Server Networking.
- Red Hat Networking: RHEL 9 Networking Guide.
- CIDR Notation: DNSimple’s CIDR Guide.
- Subnet Calculator: Subnet-Calculator.com.
Let me know if you have questions or need clarification—happy networking! 😊