温馨提示×

如何用linux ifconfig断网

小樊
39
2026-10-13 19:51:10
栏目: 智能运维

在 Linux 中,ifconfig 本身不能直接“断网”,但可以通过关闭网络接口来实现断网效果。


一、用 ifconfig 关闭网卡(断网)

1️⃣ 查看网卡名称

ifconfig

常见名称:

  • eth0(有线)
  • wlan0(无线)
  • ens33(新系统常见)

2️⃣ 关闭网卡(断网)

sudo ifconfig eth0 down

或

sudo ifconfig wlan0 down

✅ 执行后:

  • 该网卡断开
  • 无法访问网络(断网)

3️⃣ 恢复网络

sudo ifconfig eth0 up

⚠️ 注意:

  • up 后可能需要重新获取 IP:
sudo dhclient eth0

二、彻底断网(所有网卡)

sudo ifconfig lo down

❌ 不推荐,会影响本地通信

更推荐:

for i in $(ls /sys/class/net); do sudo ifconfig $i down; done

三、现代替代方案(推荐)

ifconfig 已逐渐被弃用,推荐使用 ip 命令:

sudo ip link set eth0 down
sudo ip link set eth0 up

四、仅禁止某个程序联网(进阶)

如需只断某个程序,可用:

  • iptables
  • firejail
  • unshare

如需我给你示例可以告诉我使用场景。


总结

✅ ifconfig down = 断网
✅ ifconfig up = 恢复
⚠️ 需要 root 权限

如果你是测试断网、防干扰、做实验,可以告诉我具体用途,我给你更合适的方案。

0 踩