温馨提示×

debian ifconfig如何设置

小樊
43
2026-09-09 13:55:54
栏目: 智能运维

在 Debian(以及大多数 Linux 发行版)中,ifconfig 属于 net-tools 工具包,主要用于查看和临时配置网络接口。需要注意的是:

  • ifconfig 设置的网络参数是临时的,重启后会失效
  • Debian 新版本更推荐使用 ip 命令或 NetworkManager / systemd-networkd / /etc/network/interfaces

下面按常见场景说明。


一、安装 ifconfig(如未安装)

sudo apt update
sudo apt install net-tools

二、查看网络接口

ifconfig

或只看某个接口:

ifconfig eth0

三、临时设置 IP 地址(重启失效)

1️⃣ 设置 IP 和子网掩码

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

2️⃣ 启用接口

sudo ifconfig eth0 up

3️⃣ 关闭接口

sudo ifconfig eth0 down

四、设置网关(ifconfig 不能设网关)

ifconfig 不能设置默认网关,需要用 routeip

sudo route add default gw 192.168.1.1

或使用推荐方式:

sudo ip route add default via 192.168.1.1

五、设置 MAC 地址(临时)

sudo ifconfig eth0 down
sudo ifconfig eth0 hw ether 00:11:22:33:44:55
sudo ifconfig eth0 up

六、永久配置(Debian 推荐方式)

方法 1:修改 /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

方法 2:使用 NetworkManager(桌面环境)

nmtui

或:

nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24
nmcli connection modify eth0 ipv4.gateway 192.168.1.1
nmcli connection modify eth0 ipv4.method manual
nmcli connection up eth0

七、总结

功能 ifconfig 推荐方式
查看接口 ip addr
临时 IP ip addr add
网关 ip route
永久配置 interfaces / NetworkManager

如果你告诉我:

  • Debian 版本(如 11 / 12)
  • 网络环境(DHCP / 静态)
  • 是有线还是无线

我可以给你更精确的配置示例

0