温馨提示×

Debian ifconfig如何配置

小樊
52
2026-08-31 10:47:48
栏目: 智能运维

Debian 系统中,ifconfig 属于 net-tools 工具包,但 已过时。现在推荐使用 iproute2ip 命令)来配置网络。下面我会分别说明 ifconfig 用法现代推荐方式,以及 临时/永久配置 的区别。


一、使用 ifconfig(临时配置,不推荐)

1️⃣ 安装 ifconfig(如未安装)

sudo apt update
sudo apt install net-tools

2️⃣ 查看网卡

ifconfig

ifconfig -a

3️⃣ 配置 IP 地址(临时)

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

启用网卡:

sudo ifconfig eth0 up

关闭网卡:

sudo ifconfig eth0 down

4️⃣ 配置网关(ifconfig 不能直接配置)

sudo route add default gw 192.168.1.1

查看路由:

route -n

5️⃣ 配置 DNS(ifconfig 无法配置)

编辑:

sudo nano /etc/resolv.conf

添加:

nameserver 8.8.8.8
nameserver 8.8.4.4

⚠️ 注意:该方式重启后失效。


二、推荐方式:使用 ip 命令(临时配置)

1️⃣ 配置 IP

sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip link set eth0 up

2️⃣ 配置网关

sudo ip route add default via 192.168.1.1

3️⃣ 查看网络状态

ip addr
ip route

三、永久配置(Debian 推荐)

✅ 方法一:/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 8.8.4.4

重启网络:

sudo systemctl restart networking

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

查看连接:

nmcli device status

配置静态 IP:

nmcli con modify "Wired connection 1" \
  ipv4.method manual \
  ipv4.addresses 192.168.1.100/24 \
  ipv4.gateway 192.168.1.1 \
  ipv4.dns "8.8.8.8"

应用:

nmcli con up "Wired connection 1"

四、Debian 版本建议

Debian 版本 推荐方式
Debian 9/10 ifconfig / interfaces
Debian 11/12 ip / NetworkManager

五、总结

临时配置ifconfig / ip
永久配置/etc/network/interfacesNetworkManager
不推荐:长期使用 ifconfig


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

  • Debian 版本
  • 是服务器还是桌面
  • 静态 IP 还是 DHCP

我可以给你 精确配置示例

0