Update tech_docs/linux/debian_networking.md

This commit is contained in:
2025-06-30 05:16:49 +00:00
parent 285d2f1e4e
commit fe5fcab388

View File

@@ -1,93 +1,338 @@
Certainly! Here's a more detailed guide on how to manage network configurations on a Debian 12 server using different methods, with additional context and instructions tailored to your preference for using VIM as a text editor. # Network Configuration on Debian 12: Comprehensive Guide
### Network Configuration on Debian 12 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.
Debian 12 can manage network configurations through traditional Debian methods like the `/etc/network/interfaces` file, or modern methods such as `systemd-networkd` and NetworkManager. Below is a comprehensive guide on how to adjust the default route using these methods, and how to use VIM for editing configuration files. ## Understanding Network Management Systems
### 1. Using `/etc/network/interfaces` ### System Detection
Before making changes, determine which network management system is active:
For servers not using NetworkManager or `systemd-networkd`, the network settings are traditionally managed via the `/etc/network/interfaces` file. ```bash
# Check if NetworkManager is running
systemctl is-active NetworkManager
**Steps to modify the default route:** # Check if systemd-networkd is running
systemctl is-active systemd-networkd
- **Open the configuration file with VIM**: # Check for traditional ifupdown configuration
```bash ls -la /etc/network/interfaces
sudo vim /etc/network/interfaces ```
```
- **Configure your network interface**: Here's an example of what your configuration might look like if you're setting a static IP and want to define which gateway the server should use: **Important**: Only one network management system should be active to avoid conflicts.
```plaintext ### Network Management Hierarchy
auto eth0 1. **NetworkManager**: Best for desktop environments and laptops with changing network conditions
iface eth0 inet static 2. **systemd-networkd**: Ideal for servers and containers requiring predictable, declarative configuration
address 192.168.1.100 3. **ifupdown** (`/etc/network/interfaces`): Traditional Debian method, suitable for simple server configurations
netmask 255.255.255.0
gateway 192.168.1.1
```
Make sure to replace `eth0` with the correct interface name, and update the `address`, `netmask`, and `gateway` with appropriate values for your network. Only set the `gateway` for the interface that should be the default route. ## Method 1: Traditional ifupdown Configuration
- **Restart networking to apply changes**: ### When to Use
```bash - Simple server setups with static configurations
sudo systemctl restart networking - Legacy systems requiring compatibility
``` - Environments where minimal dependencies are preferred
### 2. Using `systemd-networkd` ### Implementation
If your server uses `systemd-networkd` for managing network interfaces, you'll configure them via `.network` files located in `/etc/systemd/network/`. **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
```
- **Create or edit a network file for your interface**: **2. Edit the configuration file:**
```bash ```bash
sudo vim /etc/systemd/network/10-eth0.network sudo vim /etc/network/interfaces
``` ```
Here is what the configuration might look like: **3. Example configurations:**
```plaintext
[Match]
Name=eth0
[Network]
DHCP=no
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
```
Adjust the interface name and network settings as necessary. **Static IP with single interface:**
```plaintext
# The loopback network interface
auto lo
iface lo inet loopback
- **Restart `systemd-networkd` to apply changes**: # Primary network interface
```bash auto eth0
sudo systemctl restart systemd-networkd 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
```
### 3. Using NetworkManager **Multiple interfaces with specific routing:**
```plaintext
auto lo
iface lo inet loopback
For servers with a graphical interface or for those preferring NetworkManager: # 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
- **Edit connections using NMTUI**, or for command line changes: # Secondary interface (no default gateway)
```bash auto eth1
nmcli connection modify <connection-name> ipv4.addresses "192.168.1.100/24" ipv4.gateway "192.168.1.1" ipv4.dns "8.8.8.8" ipv4.method manual iface eth1 inet static
``` address 10.0.0.100/24
Replace `<connection-name>` with the name of your connection. # Note: No gateway specified to avoid routing conflicts
metric 200
```
- **Apply changes**: **4. Apply changes:**
```bash ```bash
nmcli connection up <connection-name> # Method 1: Restart networking service
``` sudo systemctl restart networking
### Making Temporary Changes # Method 2: Bring interface down/up (less disruptive)
sudo ifdown eth0 && sudo ifup eth0
For temporary routing adjustments: # Verify configuration
ip route show
ip addr show eth0
```
- **Delete the existing default route**: ### VIM Tips for Network Configuration
```bash ```bash
sudo ip route del default # VIM commands for efficient editing:
``` # :set number - Show line numbers
- **Add a new default route**: # :set syntax=conf - Enable syntax highlighting
```bash # /gateway - Search for "gateway"
sudo ip route add default via 192.168.1.1 dev eth0 # :%s/old_ip/new_ip/g - Replace all occurrences of old_ip with new_ip
``` ```
These commands will modify the routing table until the next reboot or restart of the network service. ## Method 2: systemd-networkd Configuration
This comprehensive guide should help you manage your Debian server's network settings effectively. Whether you're making temporary changes or configuring settings for long-term use, these steps will ensure your network is set up according to your needs. ### 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.