用 netsh 在 CMD 中设置静态 IP 地址

一行命令为任意网卡配置固定 IPv4 地址、子网掩码和默认网关,无界面管理服务器的必备技能。

Windows网络系统终端中风险
运行 `netsh interface ip set address name="Ethernet" static <IP> <掩码> <网关>` 设置静态 IP,把 `static …` 换成 `dhcp` 即可还原。

01命令

命令
$ netsh interface ip set address name="Ethernet" static 192.168.1.50 255.255.255.0 192.168.1.1

02作用说明

name 参数必须与网卡的连接名称完全一致(含空格),可先用 `netsh interface show interface` 或 `Get-NetAdapter` 查看。上例设置的是地址、掩码和网关;DNS 是单独一条命令:`netsh interface ip set dns name="Ethernet" static 1.1.1.1`。配置后用 `ipconfig /all` 或 `netsh interface ip show config` 验证,并 ping 网关确认连通。若要在同一网卡上追加第二个 IP 而不替换主 IP,应使用 `add address` 变体,`set address` 会直接替换主地址。

03风险

风险中风险
使用条件Run as administrator; the interface name must match exactly (check with `netsh interface show interface`).
!注意: 在远程机器上写错网关或使用了已被占用的 IP,连接会立刻中断——建议改用添加辅助 IP 的方式,或通过控制台/iLO 会话操作。

04撤销

撤销
$ netsh interface ip set address name="Ethernet" dhcp

05来源

  • Netsh contexts and commands | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents the netsh interface ipv4 context with set address commands accepting static address, mask and gateway parameters or dhcp.
  • Netsh - Network Shell - Windows CMD
    SS64 · technical · 2026-09-17
    依据: SS64 shows 'netsh interface ip set address name=... static addr mask gateway' syntax and the dhcp variant for reverting.

最后验证: 2026-09-17 · 分类: 网络

06相关方案