linux

如何用ifconfig配置静态IP

小樊
99
2025-02-18 03:36:30
栏目: 编程语言

ifconfig 是一个用于配置和显示网络接口参数的命令行工具。要使用 ifconfig 配置静态 IP,请按照以下步骤操作:

  1. 打开终端(在 Linux 和 macOS 上)或命令提示符/PowerShell(在 Windows 上)。

  2. 首先,找到要配置静态 IP 的网络接口。运行以下命令:

    ifconfig -a
    

    或者在 Windows 上:

    ipconfig /all
    

    在输出中找到要配置的网络接口,例如 eth0(Linux/macOS)或 Ethernet(Windows)。

  3. 接下来,使用 ifconfig 命令为该网络接口分配静态 IP。在 Linux 和 macOS 上,运行以下命令:

    sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
    

    其中,将 eth0 替换为您要配置的网络接口,将 192.168.1.100 替换为您要分配的静态 IP 地址,将 255.255.255.0 替换为子网掩码。up 参数表示启用网络接口。

    在 Windows 上,您需要使用 netsh 命令:

    netsh interface ip set address "Ethernet" static 192.168.1.100 255.255.255.0
    

    "Ethernet" 替换为您要配置的网络接口,将 192.168.1.100 替换为您要分配的静态 IP 地址,将 255.255.255.0 替换为子网掩码。

  4. (可选)如果您还想为网络接口配置默认网关和 DNS 服务器,可以执行以下操作:

    在 Linux 和 macOS 上:

    sudo route add default gw 192.168.1.1
    echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
    

    192.168.1.1 替换为您的默认网关地址,将 8.8.8.8 替换为您要使用的 DNS 服务器地址。

    在 Windows 上:

    route add 0.0.0.0 mask 0.0.0.0 192.168.1.1
    echo "nameserver 8.8.8.8" | out-file -FilePath "C:\Windows\System32\drivers\etc\resolv.conf" -Append
    

    192.168.1.1 替换为您的默认网关地址,将 8.8.8.8 替换为您要使用的 DNS 服务器地址。

  5. 最后,使用 ifconfig(Linux/macOS)或 ipconfig(Windows)命令验证网络接口已成功配置静态 IP。

请注意,这些更改在系统重启后可能会丢失。要使更改永久生效,您需要将它们添加到系统的网络配置文件中。具体操作方法因操作系统而异。

0
看了该问题的人还看了