Windows 平台的路由表配置

Windows 平台的路由表配置

原文链接 http://littlenewton.uk/2021/08/network-how-to-set-route-table-on-windows/

命令参考 https://learn.microsoft.com/en-us/powershell/module/nettcpip/?view=windowsserver2019-ps

 

######################

# 查看网卡的名称和序号
Get-NetAdapter | select Name, ifIndex

# 查看指定网卡的IP地址信息
Get-NetIPConfiguration -InterfaceIndex 27 -Detailed
Get-NetIPConfiguration -InterfaceIndex 40 -Detailed


# 查看当前系统的IPv4路由信息
Get-NetRoute -AddressFamily IPv4

# 删除指定路由
Remove-NetRoute -DestinationPrefix 110.42.96.64/32 -NextHop 192.168.11.1

# 添加新的指定路由
New-NetRoute -InterfaceIndex 40 -DestinationPrefix 110.42.96.64/32 -NextHop 192.168.11.1 -RouteMetric 11

# 查看指定路由
Get-NetRoute -AddressFamily IPv4 | Where-Object -filterScript {$_.DestinationPrefix -eq '110.42.96.64/32'}

# 修改指定路由的 RouteMetric 优先级
Set-NetRoute -InterfaceIndex 27 -DestinationPrefix 0.0.0.0/0 -NextHop 192.168.3.1 -RouteMetric 6
Get-NetRoute -AddressFamily IPv4 | Where-Object -filterScript {$_.DestinationPrefix -eq '0.0.0.0/0'}

# 检查域名解析结果
Resolve-DnsName -Name "www.google.com"
nslookup.exe www.google.com

######################

 

查看物理网卡信息

PS C:\Users\Lenovo> Get-NetAdapterHardwareInfo

Name                           Segment Bus Device Function Slot NumaNode PcieLinkSpeed PcieLinkWidth Version
----                           ------- --- ------ -------- ---- -------- ------------- ------------- -------
以太网 5                             0   2      2        0   34               5.0 GT/s            32 1.0
以太网 6                             0   2      3        0   35               5.0 GT/s            32 1.0


PS C:\Users\Lenovo>

 

查看网卡的名称和序号

PS C:\Users\Lenovo> Get-NetAdapter | select Name, ifIndex

Name     ifIndex
----     -------
以太网 5      40
本地连接      39
以太网 6      27
以太网 3      25
以太网 4       9
cfw-tap        7
以太网 2       5


PS C:\Users\Lenovo>

 

查看指定网卡的IP地址信息

PS C:\Users\Lenovo> Get-NetIPConfiguration -InterfaceIndex 27 -Detailed


ComputerName                          : LENOVO-VM
InterfaceAlias                        : 以太网 6
InterfaceIndex                        : 27
InterfaceDescription                  : vmxnet3 以太网适配器 #2
NetCompartment.CompartmentId          : 1
NetCompartment.CompartmentDescription : Default Compartment
NetAdapter.LinkLayerAddress           : 00-0C-29-97-50-9E
NetAdapter.Status                     : Up
NetProfile.Name                       : 网络 2
NetProfile.NetworkCategory            : Public
NetProfile.IPv6Connectivity           : NoTraffic
NetProfile.IPv4Connectivity           : Internet
IPv6LinkLocalAddress                  : fe80::70ed:aa83:fb12:faa2%27
IPv4Address                           : 192.168.3.191
IPv6DefaultGateway                    :
IPv4DefaultGateway                    : 192.168.3.1
NetIPv6Interface.NlMTU                : 1500
NetIPv4Interface.NlMTU                : 1500
NetIPv6Interface.DHCP                 : Enabled
NetIPv4Interface.DHCP                 : Enabled
DNSServer                             :



PS C:\Users\Lenovo>

 

设置指定网卡的主要IP地址

PS C:\Users\Lenovo> New-NetIPAddress -IPAddress 192.168.3.191 -PrefixLength 24 -DefaultGateway 192.168.3.1 -InterfaceIndex 27 -SkipAsSource $false


IPAddress         : 192.168.3.191
InterfaceIndex    : 27
InterfaceAlias    : 以太网 6
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.3.191
InterfaceIndex    : 27
InterfaceAlias    : 以太网 6
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Invalid
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : PersistentStore



PS C:\Users\Lenovo>

 

设置指定网卡的别名IP地址

PS C:\Users\Lenovo> New-NetIPAddress -IPAddress 192.168.9.191 -PrefixLength 24 -InterfaceIndex 27 -SkipAsSource $true


IPAddress         : 192.168.9.191
InterfaceIndex    : 27
InterfaceAlias    : 以太网 6
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : True
PolicyStore       : ActiveStore

IPAddress         : 192.168.9.191
InterfaceIndex    : 27
InterfaceAlias    : 以太网 6
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Invalid
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : True
PolicyStore       : PersistentStore



PS C:\Users\Lenovo>

 

删除指定网卡的IP地址

PS C:\Users\Lenovo> Remove-NetIPAddress -IPAddress 192.168.9.191 -PrefixLength 24

确认
是否确实要执行此操作?
Performing operation "Remove" on Target "NetIPAddress -IPv4Address 192.168.9.102 -InterfaceIndex 27 -Store Active"
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): A
PS C:\Users\Lenovo>

 

查看指定网卡的IP地址

PS C:\Users\Lenovo> Get-NetIPConfiguration -InterfaceIndex 27 -Detailed


ComputerName                          : LENOVO-VM
InterfaceAlias                        : 以太网 6
InterfaceIndex                        : 27
InterfaceDescription                  : vmxnet3 以太网适配器 #2
NetCompartment.CompartmentId          : 1
NetCompartment.CompartmentDescription : Default Compartment
NetAdapter.LinkLayerAddress           : 00-0C-29-97-50-9E
NetAdapter.Status                     : Up
NetProfile.Name                       : 网络 2
NetProfile.NetworkCategory            : Public
NetProfile.IPv6Connectivity           : NoTraffic
NetProfile.IPv4Connectivity           : Internet
IPv6LinkLocalAddress                  : fe80::70ed:aa83:fb12:faa2%27
IPv4Address                           : 192.168.3.191
IPv6DefaultGateway                    :
IPv4DefaultGateway                    : 192.168.3.1
NetIPv6Interface.NlMTU                : 1500
NetIPv4Interface.NlMTU                : 1500
NetIPv6Interface.DHCP                 : Enabled
NetIPv4Interface.DHCP                 : Disabled
DNSServer                             :



PS C:\Users\Lenovo>

 

查看当前系统的IPv4路由信息

PS C:\Users\Lenovo> Get-NetRoute -AddressFamily IPv4

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
27      255.255.255.255/32                             0.0.0.0                                          256 16       ActiveStore
40      255.255.255.255/32                             0.0.0.0                                          256 15       ActiveStore
7       255.255.255.255/32                             0.0.0.0                                          256 1        ActiveStore
25      255.255.255.255/32                             0.0.0.0                                          256 35       ActiveStore
5       255.255.255.255/32                             0.0.0.0                                          256 55       ActiveStore
1       255.255.255.255/32                             0.0.0.0                                          256 75       ActiveStore
27      224.0.0.0/4                                    0.0.0.0                                          256 16       ActiveStore
40      224.0.0.0/4                                    0.0.0.0                                          256 15       ActiveStore
7       224.0.0.0/4                                    0.0.0.0                                          256 1        ActiveStore
25      224.0.0.0/4                                    0.0.0.0                                          256 35       ActiveStore
5       224.0.0.0/4                                    0.0.0.0                                          256 55       ActiveStore
1       224.0.0.0/4                                    0.0.0.0                                          256 75       ActiveStore
40      192.168.11.255/32                              0.0.0.0                                          256 15       ActiveStore
40      192.168.11.191/32                              0.0.0.0                                          256 15       ActiveStore
40      192.168.11.0/24                                0.0.0.0                                          256 15       ActiveStore
27      192.168.9.255/32                               0.0.0.0                                          256 16       ActiveStore
27      192.168.9.191/32                               0.0.0.0                                          256 16       ActiveStore
27      192.168.9.0/24                                 0.0.0.0                                          256 16       ActiveStore
27      192.168.3.255/32                               0.0.0.0                                          256 16       ActiveStore
27      192.168.3.191/32                               0.0.0.0                                          256 16       ActiveStore
27      192.168.3.0/24                                 0.0.0.0                                          256 16       ActiveStore
1       127.255.255.255/32                             0.0.0.0                                          256 75       ActiveStore
1       127.0.0.1/32                                   0.0.0.0                                          256 75       ActiveStore
1       127.0.0.0/8                                    0.0.0.0                                          256 75       ActiveStore
40      110.42.96.64/32                                192.168.11.1                                      11 15       ActiveStore
27      0.0.0.0/0                                      192.168.3.1                                        6 16       ActiveStore
40      0.0.0.0/0                                      192.168.11.1                                      15 15       ActiveStore
7       0.0.0.0/0                                      10.0.0.0                                           1 1        ActiveStore


PS C:\Users\Lenovo>

 

添加新的指定路由条目和查看指定路由以及删除指定路由

PS C:\Users\Lenovo> New-NetRoute -InterfaceIndex 40 -DestinationPrefix 110.42.96.64/32 -NextHop 192.168.11.1 -RouteMetric 11

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
40      110.42.96.64/32                                192.168.11.1                                      11  15       ActiveStore
40      110.42.96.64/32                                192.168.11.1                                      11  15       Persiste...


PS C:\Users\Lenovo> Get-NetRoute -AddressFamily IPv4 | Where-Object -filterScript {$_.DestinationPrefix -eq '110.42.96.64/32'}

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
40      110.42.96.64/32                                192.168.11.1                                      11  15       ActiveStore


PS C:\Users\Lenovo>
PS C:\Users\Lenovo> Remove-NetRoute -DestinationPrefix 110.42.96.64/32 -NextHop 192.168.11.1

确认
是否确实要执行此操作?
Performing operation "Remove" on Target "NetRoute -DestinationPrefix 110.42.96.64/32 -InterfaceIndex 40 -NextHop 192.168.11.1 -Store Active"
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): A
PS C:\Users\Lenovo>
PS C:\Users\Lenovo>

 

修改指定路由的 RouteMetric 优先级

PS C:\Users\Lenovo> Set-NetRoute -InterfaceIndex 27 -DestinationPrefix 0.0.0.0/0 -NextHop 192.168.3.1 -RouteMetric 6
PS C:\Users\Lenovo> Get-NetRoute -AddressFamily IPv4 | Where-Object -filterScript {$_.DestinationPrefix -eq '0.0.0.0/0'}

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
27      0.0.0.0/0                                      192.168.3.1                                        6 16       ActiveStore
40      0.0.0.0/0                                      192.168.11.1                                      15 15       ActiveStore
7       0.0.0.0/0                                      10.0.0.0                                           1 1        ActiveStore


PS C:\Users\Lenovo>
PS C:\Users\Lenovo> Get-NetRoute -AddressFamily IPv4 | Where-Object -filterScript {$_.NextHop -eq '192.168.3.1'}

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
27      0.0.0.0/0                                      192.168.3.1                                        6 16       ActiveStore


PS C:\Users\Lenovo>

 

查看当前设置的DNS服务器

PS C:\Users\Lenovo> Get-DnsClientServerAddress

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
cfw-tap                              7 IPv4    {}
cfw-tap                              7 IPv6    {}
以太网 5                            40 IPv4    {}
以太网 5                            40 IPv6    {}
以太网 6                            27 IPv4    {}
以太网 6                            27 IPv6    {}
以太网 3                            25 IPv4    {}
以太网 3                            25 IPv6    {}
以太网 2                             5 IPv4    {}
以太网 2                             5 IPv6    {}
Loopback Pseudo-Interface 1          1 IPv4    {}
Loopback Pseudo-Interface 1          1 IPv6    {}


PS C:\Users\Lenovo>

 

设置指定接口的DNS服务器

PS C:\Users\Lenovo> Set-DnsClientServerAddress -InterfaceIndex 27 -ServerAddresses ("223.5.5.5","119.29.29.29")
PS C:\Users\Lenovo> Get-DnsClientServerAddress -InterfaceIndex 27

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
以太网 6                            27 IPv4    {}
以太网 6                            27 IPv6    {}


PS C:\Users\Lenovo>

 

检查域名解析结果

PS C:\Users\Lenovo> Resolve-DnsName -Name "www.google.com"

Name                                           Type   TTL   Section    IPAddress
----                                           ----   ---   -------    ---------
www.google.com                                 AAAA   15    Answer     2001::1
www.google.com                                 A      9     Answer     31.13.94.41


PS C:\Users\Lenovo> Resolve-DnsName -Name "www.binance.com"

Name                                           Type   TTL   Section    IPAddress
----                                           ----   ---   -------    ---------
www.binance.com                                AAAA   3383  Answer     2001::c73b:95ce
www.binance.com                                A      3383  Answer     45.114.11.25


PS C:\Users\Lenovo>

 

=========== End

 

posted @ 2025-07-30 11:31  lsgxeva  阅读(139)  评论(0)    收藏  举报