# Network Configuration on Debian 12: Comprehensive Guide

Debian 12 (Bookworm) provides multiple approaches for network configuration, each suited to different use cases and environments. Understanding which method your system uses is crucial for effective network management.

## Understanding Network Management Systems

### System Detection
Before making changes, determine which network management system is active:

```bash
# Check if NetworkManager is running
systemctl is-active NetworkManager

# Check if systemd-networkd is running  
systemctl is-active systemd-networkd

# Check for traditional ifupdown configuration
ls -la /etc/network/interfaces
```

**Important**: Only one network management system should be active to avoid conflicts.

### Network Management Hierarchy
1. **NetworkManager**: Best for desktop environments and laptops with changing network conditions
2. **systemd-networkd**: Ideal for servers and containers requiring predictable, declarative configuration
3. **ifupdown** (`/etc/network/interfaces`): Traditional Debian method, suitable for simple server configurations

## Method 1: Traditional ifupdown Configuration

### When to Use
- Simple server setups with static configurations
- Legacy systems requiring compatibility
- Environments where minimal dependencies are preferred

### Implementation

**1. Verify ifupdown is managing your interface:**
```bash
# Check current network interfaces
ip addr show
# Verify no NetworkManager or systemd-networkd conflicts
systemctl is-enabled NetworkManager systemd-networkd
```

**2. Edit the configuration file:**
```bash
sudo vim /etc/network/interfaces
```

**3. Example configurations:**

**Static IP with single interface:**
```plaintext
# The loopback network interface
auto lo
iface lo inet loopback

# Primary network interface
auto eth0
iface eth0 inet static
    address 192.168.1.100/24
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4
    dns-search example.com
    # Optional: Set metric for route priority
    metric 100
```

**Multiple interfaces with specific routing:**
```plaintext
auto lo
iface lo inet loopback

# Management interface (default route)
auto eth0  
iface eth0 inet static
    address 192.168.1.100/24
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8
    metric 100

# Secondary interface (no default gateway)
auto eth1
iface eth1 inet static
    address 10.0.0.100/24
    # Note: No gateway specified to avoid routing conflicts
    metric 200
```

**4. Apply changes:**
```bash
# Method 1: Restart networking service
sudo systemctl restart networking

# Method 2: Bring interface down/up (less disruptive)
sudo ifdown eth0 && sudo ifup eth0

# Verify configuration
ip route show
ip addr show eth0
```

### VIM Tips for Network Configuration
```bash
# VIM commands for efficient editing:
# :set number          - Show line numbers
# :set syntax=conf     - Enable syntax highlighting
# /gateway             - Search for "gateway"
# :%s/old_ip/new_ip/g  - Replace all occurrences of old_ip with new_ip
```

## Method 2: systemd-networkd Configuration

### When to Use
- Modern server environments
- Container deployments
- Systems requiring advanced networking features (VLAN, bonding)
- Predictable network interface naming

### Implementation

**1. Enable systemd-networkd:**
```bash
# Disable conflicting services
sudo systemctl disable NetworkManager
sudo systemctl stop NetworkManager

# Enable systemd-networkd
sudo systemctl enable systemd-networkd
sudo systemctl enable systemd-resolved  # For DNS resolution
```

**2. Create network configuration:**
```bash
sudo vim /etc/systemd/network/10-eth0.network
```

**3. Configuration examples:**

**Basic static configuration:**
```ini
[Match]
Name=eth0
# Alternative matching options:
# MACAddress=aa:bb:cc:dd:ee:ff
# Driver=e1000e

[Network]
DHCP=no
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
Domains=example.com

# Optional advanced settings
[Route]
Destination=10.0.0.0/8
Gateway=192.168.1.254
Metric=100
```

**Advanced configuration with multiple routes:**
```ini
[Match]
Name=eth0

[Network]
DHCP=no
Address=192.168.1.100/24
DNS=8.8.8.8

# Multiple routes
[Route]
Gateway=192.168.1.1
Metric=100
# This becomes the default route due to lowest metric

[Route]
Destination=10.0.0.0/8
Gateway=192.168.1.254
Metric=200
```

**4. Apply configuration:**
```bash
sudo systemctl restart systemd-networkd
sudo systemctl restart systemd-resolved

# Verify status
networkctl status
networkctl status eth0
```

## Method 3: NetworkManager Configuration

### When to Use
- Desktop environments
- Systems with wireless interfaces
- Dynamic network environments
- GUI management preferred

### Command Line Interface
```bash
# List connections
nmcli connection show

# Modify existing connection
nmcli connection modify "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

# Create new connection
nmcli connection add \
    type ethernet \
    con-name "Static-eth0" \
    ifname eth0 \
    ipv4.addresses 192.168.1.100/24 \
    ipv4.gateway 192.168.1.1 \
    ipv4.dns "8.8.8.8" \
    ipv4.method manual

# Apply changes
nmcli connection up "Static-eth0"
```

### Text User Interface
```bash
sudo nmtui
# Navigate through the menu-driven interface
```

## Temporary Route Management

### Understanding Route Priority
Routes with lower metric values take precedence. Use `ip route show` to view current routing table with metrics.

### Temporary Changes
```bash
# View current routing table
ip route show

# Delete specific default route
sudo ip route del default via 192.168.1.1 dev eth0

# Add new default route with metric
sudo ip route add default via 192.168.1.1 dev eth0 metric 100

# Add specific network route
sudo ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0

# Flush all routes for interface (use with caution)
sudo ip route flush dev eth0
```

**Note**: Temporary changes are lost on reboot or network service restart.

## Troubleshooting and Verification

### Essential Commands
```bash
# Network interface status
ip addr show
ip link show

# Routing table
ip route show
ip route get 8.8.8.8  # Test route to specific destination

# DNS resolution
resolvectl status
nslookup google.com

# Connectivity testing
ping -c 4 192.168.1.1    # Gateway connectivity
ping -c 4 8.8.8.8        # Internet connectivity
traceroute 8.8.8.8       # Route tracing
```

### Common Issues and Solutions

**1. Multiple default routes:**
```bash
# Identify multiple default routes
ip route show | grep default

# Remove unwanted default route
sudo ip route del default via [unwanted_gateway]
```

**2. Interface naming changes:**
```bash
# Find interface names
ip link show
# or
ls /sys/class/net/

# Update configuration files with correct interface names
```

**3. DNS resolution issues:**
```bash
# Check DNS configuration
cat /etc/resolv.conf
resolvectl status

# Restart DNS resolution service
sudo systemctl restart systemd-resolved
```

## Best Practices

1. **Backup configurations** before making changes:
   ```bash
   sudo cp /etc/network/interfaces /etc/network/interfaces.backup
   ```

2. **Test connectivity** after changes:
   ```bash
   ping -c 4 [gateway_ip]
   ping -c 4 8.8.8.8
   ```

3. **Use consistent interface naming** with systemd predictable network interface names

4. **Document network changes** for future reference

5. **Implement gradual changes** in production environments

6. **Monitor network performance** after configuration changes:
   ```bash
   ss -tuln  # Show listening ports
   netstat -rn  # Show routing table
   ```

This enhanced guide provides the context and depth needed for effective network management on Debian 12 systems, with clear explanations of when and why to use each method.