Skip to main content

Networking

Network commands and utilities for connectivity, monitoring, and troubleshooting in Bash.

Connection Testing

Basic Connectivity

# Test connectivity
ping google.com
ping -c 4 google.com # Send 4 packets
ping -i 2 google.com # 2-second intervals
ping -s 1000 google.com # 1000-byte packets

# Test IPv6
ping6 google.com

# Test specific port
telnet google.com 80
nc -zv google.com 80 # netcat port check

Network Path Testing

# Trace route to destination
traceroute google.com
tracepath google.com
mtr google.com # Real-time traceroute

# Test DNS resolution
nslookup google.com
dig google.com
host google.com

Network Information

Interface Information

# List network interfaces
ip addr show
ip link show
ifconfig # Legacy command

# Show specific interface
ip addr show eth0
ifconfig eth0

# Show routing table
ip route show
route -n # Legacy command
netstat -rn # Legacy command

Network Statistics

# Show network connections
netstat -tuln # TCP/UDP listening ports
netstat -tulpn # Include process info
ss -tuln # Modern alternative
ss -tulpn # Include process info

# Show active connections
netstat -an
ss -an

# Show network statistics
netstat -s
ss -s

Network Configuration

Interface Management

# Bring interface up/down
sudo ip link set eth0 up
sudo ip link set eth0 down
sudo ifconfig eth0 up # Legacy
sudo ifconfig eth0 down # Legacy

# Configure IP address
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# Remove IP address
sudo ip addr del 192.168.1.100/24 dev eth0

Routing Configuration

# Add route
sudo ip route add 192.168.2.0/24 via 192.168.1.1
sudo route add -net 192.168.2.0/24 gw 192.168.1.1

# Delete route
sudo ip route del 192.168.2.0/24
sudo route del -net 192.168.2.0/24

# Add default gateway
sudo ip route add default via 192.168.1.1
sudo route add default gw 192.168.1.1

Network Monitoring

Traffic Monitoring

# Monitor network traffic
sudo tcpdump -i eth0
sudo tcpdump -i eth0 port 80
sudo tcpdump -i eth0 host google.com

# Monitor bandwidth
iftop # Interactive bandwidth monitor
nethogs # Per-process bandwidth
vnstat # Network statistics

Connection Monitoring

# Monitor connections
watch -n 1 "netstat -an | grep ESTABLISHED"
watch -n 1 "ss -an | grep ESTABLISHED"

# Monitor specific port
watch -n 1 "netstat -an | grep :80"
watch -n 1 "ss -an | grep :80"

Network Utilities

File Transfer

# Download files
curl -O https://example.com/file.txt
wget https://example.com/file.txt

# Upload files
curl -X POST -F "file=@upload.txt" https://example.com/upload
scp file.txt user@server:/path/to/destination

# Secure copy
scp file.txt user@server:~/
rsync -av file.txt user@server:~/

Web Requests

# HTTP requests
curl https://api.example.com
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"value"}' https://api.example.com

# Save response
curl -o response.json https://api.example.com
curl -O https://example.com/file.txt

# Follow redirects
curl -L https://example.com

Port and Service Management

Port Scanning

# Scan ports
nmap google.com
nmap -p 80,443 google.com
nmap -p 1-1000 google.com

# Scan local network
nmap 192.168.1.0/24
nmap -sn 192.168.1.0/24 # Ping scan only

Service Testing

# Test services
nc -zv google.com 80 # Test TCP port
nc -zvu google.com 53 # Test UDP port
telnet google.com 25 # Test SMTP

# Test HTTP services
curl -I https://google.com # HEAD request
curl -s -o /dev/null -w "%{http_code}" https://google.com

Firewall and Security

Firewall Commands

# UFW (Ubuntu Firewall)
sudo ufw enable
sudo ufw disable
sudo ufw allow 80
sudo ufw allow ssh
sudo ufw deny 23
sudo ufw status

# iptables (traditional)
sudo iptables -L
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -j DROP

Security Scanning

# Port scanning
nmap -sS google.com # SYN scan
nmap -sU google.com # UDP scan
nmap -A google.com # Aggressive scan

# Service detection
nmap -sV google.com
nmap -O google.com # OS detection

Network Troubleshooting

Connectivity Issues

# Test connectivity step by step
ping 8.8.8.8 # Test internet
ping google.com # Test DNS
traceroute google.com # Test route
nslookup google.com # Test DNS resolution

# Check interface status
ip addr show
ip link show

DNS Troubleshooting

# Test DNS servers
nslookup google.com 8.8.8.8
dig @8.8.8.8 google.com
host google.com 8.8.8.8

# Check DNS configuration
cat /etc/resolv.conf
systemd-resolve --status

Performance Testing

# Test bandwidth
iperf3 -c server.example.com
iperf3 -s # Server mode

# Test latency
ping -c 100 google.com | tail -1
mtr --report-cycles 100 google.com

Network Scripts

Connection Monitor

#!/bin/bash
# monitor_connection.sh

HOST="google.com"
LOG_FILE="/tmp/network_monitor.log"

while true; do
if ping -c 1 "$HOST" > /dev/null 2>&1; then
echo "$(date): Connection to $HOST is UP" >> "$LOG_FILE"
else
echo "$(date): Connection to $HOST is DOWN" >> "$LOG_FILE"
fi
sleep 60
done

Port Scanner

#!/bin/bash
# port_scanner.sh

HOST="$1"
START_PORT="$2"
END_PORT="$3"

if [ $# -ne 3 ]; then
echo "Usage: $0 <host> <start_port> <end_port>"
exit 1
fi

for port in $(seq $START_PORT $END_PORT); do
if nc -z -w 1 "$HOST" "$port" 2>/dev/null; then
echo "Port $port is open"
fi
done

Network Speed Test

#!/bin/bash
# network_speed_test.sh

# Test download speed
echo "Testing download speed..."
curl -o /tmp/test_file -s -w "%{speed_download}\n" \
https://speed.cloudflare.com/100mb

# Test upload speed (requires test server)
echo "Testing upload speed..."
dd if=/dev/zero bs=1M count=10 2>/dev/null | \
curl -X POST -s -w "%{speed_upload}\n" \
--data-binary @- https://httpbin.org/post > /dev/null

# Cleanup
rm -f /tmp/test_file

Network Configuration Files

Important Files

# Network interfaces
/etc/network/interfaces # Debian/Ubuntu
/etc/sysconfig/network-scripts/ # Red Hat/CentOS

# DNS configuration
/etc/resolv.conf # DNS servers
/etc/hosts # Local hostname resolution

# Network services
/etc/services # Port/service mappings
/etc/protocols # Protocol definitions

Configuration Examples

# /etc/hosts
127.0.0.1 localhost
192.168.1.100 myserver.local

# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

Wireless Networks

WiFi Commands

# List wireless interfaces
iwconfig
iw dev

# Scan for networks
iwlist scan
iw dev wlan0 scan

# Connect to network
sudo iwconfig wlan0 essid "NetworkName"
sudo iwconfig wlan0 key "password"

# Modern WiFi management
nmcli device wifi list
nmcli device wifi connect "NetworkName" password "password"

WiFi Troubleshooting

# Check WiFi status
iwconfig wlan0
iw dev wlan0 link
nmcli device show wlan0

# Reset WiFi
sudo ifconfig wlan0 down
sudo ifconfig wlan0 up
sudo systemctl restart NetworkManager

Advanced Networking

Network Namespaces

# Create network namespace
sudo ip netns add test_ns

# List namespaces
ip netns list

# Execute command in namespace
sudo ip netns exec test_ns bash

# Delete namespace
sudo ip netns delete test_ns

Bridge Networks

# Create bridge
sudo ip link add name br0 type bridge
sudo ip link set br0 up

# Add interface to bridge
sudo ip link set eth0 master br0

# Remove interface from bridge
sudo ip link set eth0 nomaster

VLAN Configuration

# Create VLAN interface
sudo ip link add link eth0 name eth0.100 type vlan id 100
sudo ip link set eth0.100 up

# Configure VLAN IP
sudo ip addr add 192.168.100.1/24 dev eth0.100

# Remove VLAN
sudo ip link delete eth0.100

Performance Optimization

Network Tuning

# Check network buffer sizes
cat /proc/sys/net/core/rmem_max
cat /proc/sys/net/core/wmem_max

# Increase buffer sizes
echo 'net.core.rmem_max = 134217728' >> /etc/sysctl.conf
echo 'net.core.wmem_max = 134217728' >> /etc/sysctl.conf

# Apply changes
sudo sysctl -p

Connection Optimization

# Optimize TCP settings
echo 'net.ipv4.tcp_window_scaling = 1' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_timestamps = 1' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control = bbr' >> /etc/sysctl.conf

# Check current settings
sysctl net.ipv4.tcp_congestion_control

Security Best Practices

Network Security

# Disable unnecessary services
sudo systemctl stop telnet
sudo systemctl disable telnet

# Check listening ports
sudo netstat -tlnp
sudo ss -tlnp

# Monitor connections
sudo netstat -an | grep ESTABLISHED

Firewall Configuration

# Basic firewall setup
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

See System Operations for process monitoring and Advanced Features for automation integration.