Debian ifconfig高级配置技巧
导读:Debian ifconfig 高级配置技巧 一 常用高级操作速查 查看与启停 查看全部接口:ifconfig -a 查看指定接口:ifconfig eth0 启用/停用:sudo ifconfig eth0 up、sudo ifcon...
Debian ifconfig 高级配置技巧
一 常用高级操作速查
- 查看与启停
- 查看全部接口:ifconfig -a
- 查看指定接口:ifconfig eth0
- 启用/停用:sudo ifconfig eth0 up、sudo ifconfig eth0 down
- 地址与广播
- 设置 IP 与掩码:sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0
- 设置广播:sudo ifconfig eth0 broadcast 192.168.1.255
- 链路与抓包
- 调整 MTU:sudo ifconfig eth0 mtu 1500
- 混杂模式:sudo ifconfig eth0 promisc(关闭:sudo ifconfig eth0 -promisc)
- 硬件地址
- 修改 MAC:sudo ifconfig eth0 hw ether 00:11:22:33:44:55
- 别名接口
- 添加别名:sudo ifconfig eth0:1 192.168.1.20 netmask 255.255.255.0
- 统计与排障
- 查看收发包/错误:ifconfig eth0(关注 RX/TX packets、errors、dropped 等字段)
二 进阶用法与实用脚本
- 删除 IP 地址:sudo ifconfig eth0 192.168.1.10 del
- 点对点链路:为接口设置对端地址(如 ppp 场景),示例:sudo ifconfig eth0 pointopoint 192.168.1.2
- 批量与脚本化(示例)
- 多别名快速配置
#!/usr/bin/env bash for i in { 1..3} ; do sudo ifconfig eth0:${ i} 192.168.1.$((100+i)) netmask 255.255.255.0 up done - 监控抓包前自动开启混杂模式
#!/usr/bin/env bash IFACE=eth0 sudo ifconfig "$IFACE" promisc up echo "Started promiscuous mode on $IFACE. Use 'sudo ifconfig $IFACE -promisc' to turn off."
- 多别名快速配置
- 提示:ifconfig 多数操作需要 root 权限,建议放入脚本前做好权限与回滚设计。
三 持久化配置与 ip 命令替代
- 使用 /etc/network/interfaces 持久化
- 静态地址
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 mtu 1500 - 别名接口
auto eth0:1 iface eth0:1 inet static address 192.168.1.101 netmask 255.255.255.0 - 使配置生效:sudo systemctl restart networking(或 sudo ifdown eth0 & & sudo ifup eth0)
- 静态地址
- 使用 ip 命令的现代替代
- 查看地址:ip addr show
- 启停接口:sudo ip link set eth0 up/down
- 添加地址:sudo ip addr add 192.168.1.100/24 dev eth0
- 说明:在现代 Debian 系统中,ip 命令功能更全、兼容性更好,官方与社区均更推荐作为首选工具。
四 故障排查与最佳实践
- 变更前备份:sudo cp /etc/network/interfaces /etc/network/interfaces.bak
- 冲突与占用检查:配置前确认接口未被 NetworkManager 或 systemd-networkd 管理,避免并行配置导致覆盖
- 变更后验证
- 连通性:ping -c 4 192.168.1.1
- 路由:ip route
- DNS:检查 /etc/resolv.conf 是否写入 dns-nameservers
- 回滚策略:准备好将接口恢复到 DHCP 或备份配置的命令,以便快速回退
- 安全提示:修改 MAC 或开启 混杂模式 可能影响网络与合规,请在授权范围内操作。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian ifconfig高级配置技巧
本文地址: https://pptw.com/jishu/770500.html
