温馨提示×

debian ifconfig怎么设置ip

小樊
47
2026-09-27 18:06:20
栏目: 智能运维

在 Debian 中,设置 IP 地址的方式取决于 Debian 版本 以及你使用的是 传统 ifconfig + 手动配置 还是 现代网络管理方式(如 ip 命令、NetworkManager、/etc/network/interfaces)。

下面分情况说明:


一、使用 ifconfig 临时设置 IP(立即生效,重启失效)

⚠️ ifconfig 属于 net-tools,新版本 Debian 可能默认未安装
安装:sudo apt install net-tools

1. 查看网卡名

ifconfig -a

或

ip addr

2. 设置 IP 和子网掩码

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

3. 设置网关

sudo route add default gw 192.168.1.1

或

sudo ip route add default via 192.168.1.1

4. 设置 DNS

临时 DNS(重启会丢):

sudo nano /etc/resolv.conf

添加:

nameserver 8.8.8.8

二、Debian 永久设置 IP(推荐)

方法 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

重启网络:

sudo systemctl restart networking

或

sudo ifdown eth0 && sudo ifup eth0

方法 2:使用 ip 命令(代替 ifconfig,推荐)

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

方法 3:Desktop 版使用 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.dns 8.8.8.8
nmcli connection modify eth0 ipv4.method manual
nmcli connection up eth0

三、常见问题

ifconfig 命令不存在?

sudo apt install net-tools

Debian 10/11/12 推荐?

✅ 使用 ip 命令
✅ 使用 /etc/network/interfaces 或 NetworkManager


如果你告诉我:

  • Debian 版本
  • 是服务器还是桌面
  • 网卡名(如 eth0 / ens33)

我可以给你精确命令。

0 踩