温馨提示×

Debian网络设置如何通过ifconfig完成

小樊
96
2025-11-16 07:24:55
栏目: 智能运维

Debian 网络设置 ifconfig 实用指南

一 准备与接口名称

  • 确认接口名:常见有线为 eth0enpXsY(如 enp0s3),无线为 wlan0。使用命令查看:
    • 查看全部接口:ifconfig -a
    • 查看指定接口:ifconfig eth0
  • 若系统未安装 ifconfig,安装 net-tools
    • sudo apt update && sudo apt install net-tools -y
  • 注意:在现代 Debian 上,ifconfig 已被标记为过时,功能更全的替代是 ip 命令;但在多数版本中仍可使用 ifconfig 完成基本配置。

二 临时配置网络(重启后失效)

  • 启用/停用接口:
    • 启用:sudo ifconfig eth0 up
    • 停用:sudo ifconfig eth0 down
  • 配置静态 IP 与子网掩码:
    • sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
  • 设置广播地址(可选):
    • sudo ifconfig eth0 broadcast 192.168.1.255
  • 设置默认网关:
    • sudo route add default gw 192.168.1.1 eth0
  • 配置 DNS(临时):
    • 编辑 /etc/resolv.conf,添加:
      • nameserver 8.8.8.8
      • nameserver 8.8.4.4
  • 说明:以上为运行时配置,系统重启或网络服务重启后会丢失。

三 常用 ifconfig 高级操作

  • 调整 MTU(最大传输单元):
    • sudo ifconfig eth0 mtu 1500
  • 开启/关闭混杂模式(抓包/监控常用):
    • 开启:sudo ifconfig eth0 promisc
    • 关闭:sudo ifconfig eth0 -promisc
  • 修改 MAC 地址(需先 down 接口):
    • sudo ifconfig eth0 down
    • sudo ifconfig eth0 hw ether 00:11:22:33:44:55
    • sudo ifconfig eth0 up
  • 创建别名接口(同一物理口多 IP):
    • 添加:sudo ifconfig eth0:1 192.168.1.20 netmask 255.255.255.0
    • 删除:sudo ifconfig eth0:1 down(必要时 sudo ifconfig eth0:1 del
  • 提示:部分操作需要 root 权限,执行失败请加 sudo

四 持久化配置与验证

  • 使用 /etc/network/interfaces(传统方式,适合多数 Debian 版本):
    • 编辑: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(桌面/动态网络常用):
    • 查看设备:sudo nmcli dev
    • 连接 Wi‑Fi:sudo nmcli dev wifi connect "SSID" password "PASS" ifname wlan0
  • 验证配置:
    • 查看地址:ifconfig eth0
    • 查看路由:route -n
    • 测试连通:ping -c 4 8.8.8.8ping -c 4 google.com
  • 提醒:通过 ifconfig 做的临时设置不会自动持久化,需写入上述配置文件或使用相应网络管理服务。

0