网络配置
Windows 网络诊断、配置与管理,涵盖 ipconfig、netstat、netsh、防火墙及代理设置。
查看网络信息
ipconfig :: 简要显示 IP 配置
ipconfig /all :: 完整信息(含 MAC、DHCP、DNS)
ipconfig /release :: 释放 DHCP 地址
ipconfig /renew :: 重新获取 DHCP 地址
ipconfig /flushdns :: 清空 DNS 缓存
ipconfig /displaydns :: 查看 DNS 缓存Get-NetIPAddress # 所有 IP 地址
Get-NetIPAddress -AddressFamily IPv4 # 仅 IPv4
Get-NetAdapter # 网络适配器列表
Get-NetAdapter | Where-Object Status -eq "Up" # 仅已连接
Get-NetIPConfiguration # 完整网络配置
Get-DnsClientCache # DNS 缓存网络诊断
ping 8.8.8.8 :: 测试连通性
ping -t 8.8.8.8 :: 持续 ping
ping -n 10 -l 1024 8.8.8.8 :: 发送 10 个 1024 字节包
tracert 8.8.8.8 :: 路由追踪
pathping 8.8.8.8 :: 路由+丢包率统计
nslookup baidu.com :: DNS 查询
nslookup baidu.com 8.8.8.8 :: 指定 DNS 服务器查询Test-Connection 8.8.8.8 -Count 4 # PowerShell ping
Test-Connection 8.8.8.8 -Quiet # 仅返回 True/False
Test-NetConnection 8.8.8.8 -Port 443 # 测试 TCP 端口连通性
Test-NetConnection -ComputerName baidu.com -TraceRoute # 路由追踪
Resolve-DnsName baidu.com # DNS 解析
Resolve-DnsName baidu.com -Server 8.8.8.8 # 指定 DNS 服务器端口与连接
netstat -an :: 所有连接和监听端口
netstat -ano :: 同上,附带 PID
netstat -b :: 显示占用端口的进程名(需管理员)
netstat -rn :: 路由表
:: 查找占用指定端口的进程
netstat -ano | findstr :8080
tasklist /fi "pid eq 1234"Get-NetTCPConnection # 所有 TCP 连接
Get-NetTCPConnection -State Listen # 监听中的端口
Get-NetTCPConnection -LocalPort 8080 # 指定端口
# 找出占用 8080 端口的进程
$conn = Get-NetTCPConnection -LocalPort 8080 -State Listen
Get-Process -Id $conn.OwningProcessIP 地址配置
:: 设置静态 IP(需管理员)
netsh interface ip set address "以太网" static 192.168.1.100 255.255.255.0 192.168.1.1
netsh interface ip set dns "以太网" static 8.8.8.8
netsh interface ip add dns "以太网" 8.8.4.4 index=2
:: 改回 DHCP
netsh interface ip set address "以太网" dhcp
netsh interface ip set dns "以太网" dhcp$idx = (Get-NetAdapter -Name "以太网").ifIndex
# 设置静态 IP
New-NetIPAddress -InterfaceIndex $idx -IPAddress "192.168.1.100" `
-PrefixLength 24 -DefaultGateway "192.168.1.1"
Set-DnsClientServerAddress -InterfaceIndex $idx -ServerAddresses "8.8.8.8","8.8.4.4"
# 改回 DHCP
Set-NetIPInterface -InterfaceIndex $idx -Dhcp Enabled
Set-DnsClientServerAddress -InterfaceIndex $idx -ResetServerAddresses防火墙
netsh advfirewall show allprofiles :: 查看防火墙状态
netsh advfirewall set allprofiles state on :: 开启防火墙
netsh advfirewall set allprofiles state off :: 关闭防火墙
:: 添加入站规则(允许 TCP 8080)
netsh advfirewall firewall add rule name="Allow8080" dir=in action=allow protocol=TCP localport=8080
:: 删除规则
netsh advfirewall firewall delete rule name="Allow8080"
:: 列出所有规则
netsh advfirewall firewall show rule name=allGet-NetFirewallProfile # 查看防火墙配置
# 允许端口
New-NetFirewallRule -DisplayName "Allow 8080" `
-Direction Inbound -LocalPort 8080 -Protocol TCP -Action Allow
# 允许程序
New-NetFirewallRule -DisplayName "Allow MyApp" `
-Direction Inbound -Program "C:\apps\myapp.exe" -Action Allow -Protocol TCP
# 删除规则
Remove-NetFirewallRule -DisplayName "Allow 8080"
# 查看已启用规则
Get-NetFirewallRule | Where-Object Enabled -eq "True" |
Select-Object DisplayName, Direction, Action路由表
route print :: 查看路由表
route add 10.0.0.0 mask 255.255.255.0 192.168.1.1 :: 添加路由
route add 10.0.0.0 mask 255.255.255.0 192.168.1.1 -p :: 持久路由
route delete 10.0.0.0 :: 删除路由Get-NetRoute
New-NetRoute -DestinationPrefix "10.0.0.0/24" -NextHop "192.168.1.1" -InterfaceIndex 5
Remove-NetRoute -DestinationPrefix "10.0.0.0/24"代理设置
netsh winhttp show proxy :: 查看当前代理
netsh winhttp set proxy 127.0.0.1:7890 :: 设置代理
netsh winhttp import proxy source=ie :: 从 IE/Edge 导入
netsh winhttp reset proxy :: 重置(直接连接)$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
# 启用代理
Set-ItemProperty $reg -Name ProxyEnable -Value 1
Set-ItemProperty $reg -Name ProxyServer -Value "127.0.0.1:7890"
# 关闭代理
Set-ItemProperty $reg -Name ProxyEnable -Value 0
# 查看当前设置
Get-ItemProperty $reg | Select-Object ProxyEnable, ProxyServer, ProxyOverridehosts 文件
位置:C:\Windows\System32\drivers\etc\hosts
编辑需要管理员权限
# 追加条目(管理员)
Add-Content "C:\Windows\System32\drivers\etc\hosts" "`n127.0.0.1 mylocal.dev"
# 查看有效条目(过滤注释和空行)
Get-Content "C:\Windows\System32\drivers\etc\hosts" |
Where-Object { $_ -notmatch "^#" -and $_.Trim() -ne "" }网络共享
net share :: 查看共享列表
net share myshare=C:\data :: 创建共享
net share myshare /delete :: 删除共享
net use Z: \\server\share :: 映射网络驱动器
net use Z: /delete :: 断开映射
net use * /delete /yes :: 断开所有映射相关文档
- 常用命令 — 常用命令速查
- PowerShell — PowerShell 详解
- 用户与权限 — 网络访问权限