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
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 ```bash
sudo vim /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: **3. Example configurations:**
**Static IP with single interface:**
```plaintext ```plaintext
# The loopback network interface
auto lo
iface lo inet loopback
# Primary network interface
auto eth0 auto eth0
iface eth0 inet static iface eth0 inet static
address 192.168.1.100 address 192.168.1.100/24
netmask 255.255.255.0
gateway 192.168.1.1 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
``` ```
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. **Multiple interfaces with specific routing:**
```plaintext
auto lo
iface lo inet loopback
- **Restart networking to apply changes**: # 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 ```bash
# Method 1: Restart networking service
sudo systemctl restart networking 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
``` ```
### 2. Using `systemd-networkd` ### 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
```
If your server uses `systemd-networkd` for managing network interfaces, you'll configure them via `.network` files located in `/etc/systemd/network/`. ## Method 2: systemd-networkd Configuration
- **Create or edit a network file for your interface**: ### 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 ```bash
sudo vim /etc/systemd/network/10-eth0.network sudo vim /etc/systemd/network/10-eth0.network
``` ```
Here is what the configuration might look like: **3. Configuration examples:**
```plaintext
**Basic static configuration:**
```ini
[Match] [Match]
Name=eth0 Name=eth0
# Alternative matching options:
# MACAddress=aa:bb:cc:dd:ee:ff
# Driver=e1000e
[Network] [Network]
DHCP=no DHCP=no
Address=192.168.1.100/24 Address=192.168.1.100/24
Gateway=192.168.1.1 Gateway=192.168.1.1
DNS=8.8.8.8 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
``` ```
Adjust the interface name and network settings as necessary. **Advanced configuration with multiple routes:**
```ini
[Match]
Name=eth0
- **Restart `systemd-networkd` to apply changes**: [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 ```bash
sudo systemctl restart systemd-networkd sudo systemctl restart systemd-networkd
sudo systemctl restart systemd-resolved
# Verify status
networkctl status
networkctl status eth0
``` ```
### 3. Using NetworkManager ## Method 3: NetworkManager Configuration
For servers with a graphical interface or for those preferring NetworkManager: ### When to Use
- Desktop environments
- Systems with wireless interfaces
- Dynamic network environments
- GUI management preferred
- **Edit connections using NMTUI**, or for command line changes: ### Command Line Interface
```bash ```bash
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 # List connections
``` nmcli connection show
Replace `<connection-name>` with the name of your connection.
- **Apply changes**: # 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 ```bash
nmcli connection up <connection-name> sudo nmtui
# Navigate through the menu-driven interface
``` ```
### Making Temporary Changes ## Temporary Route Management
For temporary routing adjustments: ### Understanding Route Priority
Routes with lower metric values take precedence. Use `ip route show` to view current routing table with metrics.
- **Delete the existing default route**: ### Temporary Changes
```bash ```bash
sudo ip route del default # 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
``` ```
- **Add a new default route**:
**Note**: Temporary changes are lost on reboot or network service restart.
## Troubleshooting and Verification
### Essential Commands
```bash ```bash
sudo ip route add default via 192.168.1.1 dev eth0 # 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
``` ```
These commands will modify the routing table until the next reboot or restart of the network service. ### Common Issues and Solutions
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. **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.