Debian系统网络配置的常用方法(注:“Debian Strings”未作为标准网络配置工具存在,以下为实际使用的方法)
/etc/network/interfaces文件(适用于Debian 9及更早版本)这是Debian最经典的网络配置方式,通过编辑文本文件直接定义接口参数。
sudo nano /etc/network/interfaces,添加以下内容(以eth0接口为例):auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.100 # 静态IP地址
netmask 255.255.255.0 # 子网掩码
gateway 192.168.1.1 # 默认网关
dns-nameservers 8.8.8.8 8.8.4.4 # DNS服务器
iface eth0 inet static改为iface eth0 inet dhcp,其余参数无需填写,系统会自动获取IP。sudo systemctl restart networking。netplan(适用于Debian 10及更高版本)netplan是Debian 10引入的现代化网络配置工具,采用YAML格式,支持systemd-networkd或NetworkManager作为渲染器。
/etc/netplan/目录下(如01-netcfg.yaml)。sudo nano /etc/netplan/01-netcfg.yaml,内容如下:network:
version: 2
renderer: networkd # 可选:networkd(默认)或NetworkManager
ethernets:
eth0:
dhcp4: no # 关闭DHCP
addresses: [192.168.1.100/24] # IP地址及子网掩码(CIDR格式)
gateway4: 192.168.1.1 # 默认网关
nameservers:
addresses: [8.8.8.8, 8.8.4.4] # DNS服务器
sudo netplan apply,系统会自动应用新配置。NetworkManagerNetworkManager适合桌面环境或需要频繁切换网络(如Wi-Fi)的场景,提供图形界面和命令行工具nmcli。
sudo apt install network-manager;启动服务:sudo systemctl enable --now NetworkManager。eth0为例):sudo nmcli connection modify "Wired connection 1" ipv4.method manual # 设置为手动模式
sudo nmcli connection modify "Wired connection 1" ipv4.addresses 192.168.1.100/24
sudo nmcli connection modify "Wired connection 1" ipv4.gateway 192.168.1.1
sudo nmcli connection modify "Wired connection 1" ipv4.dns "8.8.8.8,8.8.4.4"
sudo nmcli connection up "Wired connection 1" # 激活连接
若需配置无线网络,可通过/etc/network/interfaces或NetworkManager实现:
/etc/network/interfaces示例:auto wlan0
iface wlan0 inet static
address 192.168.1.120
netmask 255.255.255.0
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf # 指向WiFi配置文件
wpa_supplicant.conf配置:sudo nano /etc/wpa_supplicant/wpa_supplicant.conf,内容如下:network={
ssid="Your_WiFi_SSID" # WiFi名称
psk="Your_WiFi_Password" # WiFi密码
}
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf启动WiFi连接。sudo cp /etc/network/interfaces /etc/network/interfaces.bak),避免配置错误导致网络中断。netplan,其配置更简洁且支持更多功能;旧版本使用/etc/network/interfaces。systemd-resolved服务:sudo systemctl restart systemd-resolved,或修改/etc/resolv.conf(部分系统需创建/etc/resolv.conf.head文件)。