debian

Debian ifconfig命令在脚本中的应用

小樊
38
2025-06-28 22:50:39
栏目: 智能运维

ifconfig 命令在 Debian 系统中用于配置、激活和查询网络接口的参数

  1. 获取所有活动网络接口的信息:
#!/bin/bash
interfaces=$(ifconfig -a | grep -o '^[^ ]\+')
for interface in $interfaces; do
    echo "Interface: $interface"
    ifconfig $interface
    echo ""
done
  1. 为指定网络接口分配 IP 地址:
#!/bin/bash
interface="eth0"
ip_address="192.168.1.100"
netmask="255.255.255.0"

ifconfig $interface $ip_address netmask $netmask
  1. 关闭指定网络接口:
#!/bin/bash
interface="eth0"

ifconfig $interface down
  1. 启用指定网络接口:
#!/bin/bash
interface="eth0"

ifconfig $interface up
  1. 删除指定网络接口的 IP 地址:
#!/bin/bash
interface="eth0"
ip_address="192.168.1.100"

ifconfig $interface del $ip_address

请注意,ifconfig 命令在某些 Debian 版本中可能已被弃用,建议使用 ip 命令替代。上述脚本可以根据需要进行相应的修改。

0
看了该问题的人还看了