温馨提示×

ifconfig在Debian如何配置

小樊
49
2026-07-21 21:23:53
栏目: 智能运维

在 Debian 系统中,ifconfig 主要用于查看网络接口信息,但不能直接用来做持久化网络配置。下面分情况说明:


一、ifconfig 的用途(临时配置)

ifconfig 属于 net-tools 工具包,配置是临时的,重启后失效

1. 安装 ifconfig(Debian 默认可能未安装)

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 up

4. 启用 / 禁用网卡

sudo ifconfig eth0 down
sudo ifconfig eth0 up

5. 临时配置网关(ifconfig 不能配网关)

sudo route add default gw 192.168.1.1

二、Debian 推荐的网络配置方式(持久化)

✅ 方法一:使用 /etc/network/interfaces(传统、稳定)

1. 编辑配置文件

sudo nano /etc/network/interfaces

2. 静态 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

3. 动态 IP(DHCP)

auto eth0
iface eth0 inet dhcp

4. 重启网络

sudo systemctl restart networking

sudo ifdown eth0 && sudo ifup eth0

✅ 方法二:使用 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 与 ip 命令对比(推荐用 ip)

功能 ifconfig ip 命令
查看 IP ifconfig ip addr
启用网卡 ifconfig up ip link set eth0 up
配置 IP ifconfig ip addr add
是否推荐 ✅ 推荐

四、总结

临时测试 → 用 ifconfig
正式配置 → 修改 /etc/network/interfaces 或使用 NetworkManager
不建议ifconfig 做永久网络配置


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

  • Debian 版本(10 / 11 / 12)
  • 是服务器还是桌面环境
  • 是虚拟机 / 实体机 / 云主机

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

0