温馨提示×

如何配置Debian ifconfig

小樊
42
2026-07-21 21:09:46
栏目: 智能运维

Debian 系统中,ifconfig 属于 net-tools 工具包,但现代 Debian 默认推荐使用 ip 命令systemd-networkd / NetworkManager 来配置网络。下面分情况说明。


一、安装 ifconfig(如未安装)

Debian 10 及以后版本默认不再安装 ifconfig

sudo apt update
sudo apt install net-tools

验证:

ifconfig

二、使用 ifconfig 临时配置网络(重启失效)

1️⃣ 查看网卡名称

ifconfig -a

ip addr

示例网卡:eth0


2️⃣ 配置 IP 地址

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

3️⃣ 启动 / 关闭网卡

sudo ifconfig eth0 up
sudo ifconfig eth0 down

4️⃣ 配置网关

ifconfig 不能配置网关,需要配合 route

sudo route add default gw 192.168.1.1

查看路由:

route -n

5️⃣ 配置 DNS

ifconfig 也不能配置 DNS,需编辑:

sudo nano /etc/resolv.conf

示例:

nameserver 8.8.8.8
nameserver 114.114.114.114

⚠️ 注意:该文件可能被 NetworkManager 覆盖。


三、永久配置网络(推荐方式)

✅ 方法一:编辑 /etc/network/interfaces(传统方式)

sudo nano /etc/network/interfaces

示例(静态 IP):

auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 114.114.114.114

重启网络:

sudo systemctl restart networking

✅ 方法二:使用 NetworkManager(桌面/服务器常用)

查看连接:

nmcli device status

配置静态 IP:

nmcli con modify eth0 ipv4.addresses 192.168.1.100/24
nmcli con modify eth0 ipv4.gateway 192.168.1.1
nmcli con modify eth0 ipv4.dns "8.8.8.8"
nmcli con modify eth0 ipv4.method manual
nmcli con up eth0

四、ifconfig vs ip(推荐)

ifconfig 已逐渐被淘汰,建议学习 ip 命令:

ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up
ip route add default via 192.168.1.1

五、常见问题

❓ ifconfig 命令找不到

sudo apt install net-tools

❓ 配置后重启失效

ifconfig临时配置,永久配置请使用:

  • /etc/network/interfaces
  • 或 NetworkManager

如果你愿意,可以告诉我:

  • Debian 版本(如 11 / 12)
  • 服务器 还是 桌面
  • 需要 静态 IP 还是 DHCP

我可以给你一份完全针对你环境的配置示例

0