Windows命令行配置ip地址

win + R  可以进入ncpa.cpl

 

一、添加 IP 地址及默认网关 (是添加,要重设或更新原来IP的请看第三点)

netsh interface ip add address

用法: add address [name=]<string> [[addr=]IP address [mask=]IP subnet mask] [[gateway=]IP address [gwmetric=]integer]

参数:

name        IP 接口的名称
addr        要添加到此接口的 IP 地址
mask        IP 地址的 IP 子网掩码
gateway      IP 地址的默认网关
gwmetric    默认网关的跃点数。0 表示自动跃点数

 

注释 : 向一个用静态 IP 地址配置的接口添加 IP 地址和默认网关。

示例 :

add address "本地连接" 172.18.10.197 255.255.255.0
add address "本地连接" gateway=172.18.10.254 gwmetric=2
add address "本地连接" 172.18.10.197 255.255.255.0 172.18.10.254 2

第一个命令向本地连接接口添加一个静态 IP 地址 172.18.10.197 ,子网掩码为 255.255.255.0 。
第二个命令向网关跃点数为 2 的接口添加 IP 地址 172.18.10.254 作为第二个默认网关。
第三个命令是将第一第二个命令合并起来使用。

实例 :

netsh interface ip add address "本地连接" 172.18.10.197 255.255.255.0
netsh interface ip add address "本地连接" gateway=172.18.10.254 gwmetric=1
netsh interface ip add address "本地连接" 172.18.10.197 255.255.255.0 172.18.10.254 1

第一个命令设置 本地连接 的 IP 为 172.18.10.197, 子网掩码为 255.255.255.0
第二个命令设置 本地连接 的默认网关为 172.18.10.254, 默认网关的跃点数为 1
第三个命令作用相当于前两条命令的集合。

二、添加 DNS

netsh interface ip add dns

用法: add dns [name=]<string> [addr=]<IP address> [[index=]<integer>]

参数:

     标记           值
     name         - 添加 DNS 服务器的接口的名称。
     addr         - 添加的 DNS 服务器的 IP 地址。
     index        - 为指定的 DNS 服务器地址指定索引(首选项)。

注释 : 静态添加新的 DNS 服务器 IP 地址或重新索引现有 DNS 服务器地址的列表以修改 DNS 服务器首选项。

示例 :

add dns "本地连接" 210.38.176.1
add dns "本地连接" 210.38.176.1 index=2

我的实例:

netsh interface ip add dns "本地连接" 210.38.176.1
netsh interface ip add dns "本地连接" 210.38.176.2 index=2

三、设置 IP 地址、默认网关及 DNS

用法: set address [name=]<string> [[source=]dhcp | [source=] static [addr=]IP address [mask=]IP subnet mask] [[gateway=]<IP address>|none [gwmetric=]integer]

参数:

     标记           值
     name         - 接口名称。
     source       - 下列值之一:
                    dhcp: 对于指定接口,设置用 DHCP 配置 IP 地址。
                    static: 设置使用本地静态配置设置 IP 地址。

     gateway      - 下列值之一:
                    <IP address>: 您设置的 IP 地址的指定默认网关。
                    none: 不设置默认网关。
     gwmetric     - 默认网关的跃点数。如果网关设置为 'none',则不应设置此字段。

     只有在 'source' 为 'static' 时才设置下列选项:
     addr         - 指定接口的 IP 地址。
     mask         - 指定 IP 地址的子网掩码。

注释 : 用来将 IP 地址配置模式从 DHCP 模式改为 static,或从 static模式改为 DHCP。用静态 IP 地址在接口上添加 IP 地址,或添加默认网关。

示例 :

netsh interface ip set address "本地连接" static 172.18.10.197 255.255.255.0
netsh interface ip set address "本地连接" static gateway=172.18.10.254 gwmetric=1
netsh interface ip set address "本地连接" static 172.18.10.197 255.255.255.0 172.18.10.254 1
netsh interface ip set dns "本地连接" static 210.38.176.1

(set address source=dhcp 此条表示从 dhcp 服务器得到 IP)
(set dns " source=dhcp)

四、综合例

netsh interface ip set address "本地连接" static 172.18.10.197 255.255.255.0
netsh interface ip set address "本地连接" static gateway=172.18.10.254 gwmetric=1
netsh interface ip set dns "本地连接" static 210.38.176.1
netsh interface ip add dns "本地连接" 210.38.176.2 index=2

以上命令做成批处理:

edit setIP.bat

netsh interface ip set address local static 172.18.10.197 255.255.255.0
netsh interface ip set address local static gateway=172.18.10.254 gwmetric=1
netsh interface ip set dns "%1" static 210.38.176.1
netsh interface ip add dns "%1" 210.38.176.2 index=2

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值