温馨提示×

怎样用ifconfig配Debian网络

小樊
53
2026-09-03 11:49:57
栏目: 智能运维

在 Debian 中,ifconfig 通常用于查看临时配置网络接口,但它不会持久化(重启后失效)。下面分情况说明。


一、前提:安装 net-tools

新版本 Debian 默认可能没有 ifconfig

sudo apt update
sudo apt install net-tools

二、用 ifconfig 临时配置 IP(重启失效)

1. 查看网络接口

ifconfig

ifconfig -a

2. 配置 IP 地址和子网掩码

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
  • eth0:网卡名(可能是 ens33、enp0s3 等)
  • up:启用接口

3. 添加默认网关

sudo route add default gw 192.168.1.1

sudo ip route add default via 192.168.1.1

4. 配置 DNS(临时)

编辑(或创建):

sudo nano /etc/resolv.conf

写入:

nameserver 8.8.8.8
nameserver 114.114.114.114

⚠️ 若系统使用 systemd-resolved,重启可能被覆盖。


5. 关闭 / 启用网卡

sudo ifconfig eth0 down
sudo ifconfig eth0 up

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

Debian 10+ 推荐用 /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(桌面版)

nmtui

nmcli

四、ifconfig 的局限性

  • ❌ 不能持久化
  • ❌ 不支持现代网络命名
  • ✅ 适合排障、临时调试

如果你告诉我:

  • Debian 版本
  • 网卡名(ip a 输出)
  • 静态还是 DHCP

我可以给你精确命令

0