Appearance
网络配置
网络是现代计算机使用的基础,本章介绍 Windows 系统的网络配置和管理方法。
网络基础概念
网络类型
Windows 识别以下网络类型:
| 网络类型 | 说明 | 安全级别 |
|---|---|---|
| 公用网络 | 公共场所网络,如咖啡厅、机场 | 高(限制发现) |
| 专用网络 | 家庭或工作网络 | 中(允许发现) |
| 域网络 | 企业域环境 | 由域策略控制 |
网络组件
text
网络连接的基本组件:
├── 网络适配器(网卡)
├── IP 地址
├── 子网掩码
├── 默认网关
├── DNS 服务器
└── DHCP 服务器(自动分配 IP)IP 地址基础
text
IPv4 地址格式:192.168.1.100
├── 4 个字节,每字节 0-255
├── 私有地址范围:
│ ├── 10.0.0.0 - 10.255.255.255
│ ├── 172.16.0.0 - 172.31.255.255
│ └── 192.168.0.0 - 192.168.255.255
└── 子网掩码:255.255.255.0(/24)
IPv6 地址格式:2001:0db8:85a3:0000:0000:8a2e:0370:7334
├── 128 位地址
├── 8 组,每组 4 个十六进制数字
└── 可简化连续的零组网络适配器管理
查看网络适配器
powershell
# 查看所有网络适配器
Get-NetAdapter
# 查看详细信息
Get-NetAdapter | Format-List *
# 查看特定适配器
Get-NetAdapter -Name "以太网"
Get-NetAdapter -Name "Wi-Fi"
# 查看物理适配器
Get-NetAdapter -Physical
# 查看适配器硬件信息
Get-NetAdapterHardwareInfo
# 使用 ipconfig 命令
ipconfig # 基本信息
ipconfig /all # 详细信息启用/禁用适配器
powershell
# 禁用网络适配器
Disable-NetAdapter -Name "以太网" -Confirm:$false
# 启用网络适配器
Enable-NetAdapter -Name "以太网"
# 重命名网络适配器
Rename-NetAdapter -Name "以太网" -NewName "LAN"
# 使用 netsh 命令
netsh interface set interface "以太网" disable
netsh interface set interface "以太网" enable查看适配器驱动
powershell
# 查看网络适配器驱动信息
Get-NetAdapter | Select-Object Name, DriverDescription, DriverVersion, DriverDate
# 使用 WMI 查询
Get-CimInstance -ClassName Win32_NetworkAdapter |
Where-Object { $_.NetEnabled -eq $true } |
Select-Object Name, AdapterType, MACAddressIP 地址配置
查看 IP 配置
powershell
# 查看 IP 配置
Get-NetIPConfiguration
# 查看 IPv4 地址
Get-NetIPAddress -AddressFamily IPv4
# 查看 IPv6 地址
Get-NetIPAddress -AddressFamily IPv6
# 查看特定接口的 IP
Get-NetIPAddress -InterfaceAlias "以太网"
# 使用 ipconfig
ipconfig # 基本信息
ipconfig /all # 详细信息
ipconfig /release # 释放 DHCP 地址
ipconfig /renew # 更新 DHCP 地址配置静态 IP
powershell
# 配置静态 IP 地址
New-NetIPAddress -InterfaceAlias "以太网" `
-IPAddress 192.168.1.100 `
-PrefixLength 24 `
-DefaultGateway 192.168.1.1
# 配置多个 IP 地址
New-NetIPAddress -InterfaceAlias "以太网" `
-IPAddress 192.168.1.101 `
-PrefixLength 24
# 修改现有 IP 地址
Set-NetIPAddress -InterfaceAlias "以太网" `
-IPAddress 192.168.1.100 `
-PrefixLength 24
# 删除 IP 地址
Remove-NetIPAddress -InterfaceAlias "以太网" `
-IPAddress 192.168.1.100 -Confirm:$false配置 DHCP
powershell
# 启用 DHCP
Set-NetIPInterface -InterfaceAlias "以太网" -Dhcp Enabled
# 释放 DHCP 地址
ipconfig /release
# 更新 DHCP 地址
ipconfig /renew
# 查看 DHCP 状态
Get-NetIPInterface -InterfaceAlias "以太网" |
Select-Object InterfaceAlias, Dhcp, IPAddressDNS 配置
查看 DNS 设置
powershell
# 查看 DNS 服务器地址
Get-DnsClientServerAddress
# 查看特定接口的 DNS
Get-DnsClientServerAddress -InterfaceAlias "以太网"
# 查看 DNS 缓存
Get-DnsClientCache
# 使用 ipconfig
ipconfig /displaydns # 显示 DNS 缓存
ipconfig /flushdns # 清除 DNS 缓存配置 DNS 服务器
powershell
# 设置 DNS 服务器
Set-DnsClientServerAddress -InterfaceAlias "以太网" `
-ServerAddresses ("8.8.8.8", "8.8.4.4")
# 设置 IPv6 DNS
Set-DnsClientServerAddress -InterfaceAlias "以太网" `
-ServerAddresses ("2001:4860:4860::8888", "2001:4860:4860::8844") `
-AddressFamily IPv6
# 重置为 DHCP 分配的 DNS
Set-DnsClientServerAddress -InterfaceAlias "以太网" -ResetServerAddresses
# 使用 netsh 命令
netsh interface ip set dns "以太网" static 8.8.8.8 primary
netsh interface ip add dns "以太网" 8.8.4.4 index=2DNS 故障排查
powershell
# 清除 DNS 缓存
Clear-DnsClientCache
# 测试 DNS 解析
Resolve-DnsName www.baidu.com
Resolve-DnsName www.baidu.com -Server 8.8.8.8
# 使用 nslookup
nslookup www.baidu.com
nslookup www.baidu.com 8.8.8.8
# 使用 ping 测试
ping www.baidu.comWi-Fi 管理
查看 Wi-Fi 信息
powershell
# 查看无线网络适配器
Get-NetAdapter | Where-Object { $_.MediaType -eq "Native 802.11" }
# 查看可用 Wi-Fi 网络
netsh wlan show networks
# 查看详细网络信息
netsh wlan show networks mode=bssid
# 查看已保存的 Wi-Fi 配置
netsh wlan show profiles
# 查看特定配置详情
netsh wlan show profile name="网络名称"
# 查看无线适配器信息
netsh wlan show interfaces连接 Wi-Fi
powershell
# 连接到已保存的网络
netsh wlan connect name="网络名称"
# 连接到特定网络
netsh wlan connect name="网络名称" ssid="SSID" interface="Wi-Fi"
# 断开 Wi-Fi
netsh wlan disconnect
# 添加新的 Wi-Fi 配置
netsh wlan add profile filename="C:\path\to\profile.xml"
# 手动创建配置文件
# 首先导出现有配置作为模板
netsh wlan export profile name="现有网络" folder="C:\temp"管理 Wi-Fi 配置
powershell
# 删除 Wi-Fi 配置
netsh wlan delete profile name="网络名称"
# 导出 Wi-Fi 配置
netsh wlan export profile name="网络名称" folder="C:\temp"
# 导出所有配置
netsh wlan export profile folder="C:\temp"
# 设置网络优先级
netsh wlan set profileorder name="网络名称" interface="Wi-Fi" priority=1
# 设置自动连接
netsh wlan set profileparameter name="网络名称" connectionmode=auto
# 禁用自动连接
netsh wlan set profileparameter name="网络名称" connectionmode=manualWi-Fi 密码查看
powershell
# 查看已保存网络的密码
netsh wlan show profile name="网络名称" key=clear
# 使用 PowerShell 提取密码
$profiles = netsh wlan show profiles | Select-String "所有用户配置文件|All User Profile"
foreach ($profile in $profiles) {
$name = ($profile -split ":")[1].Trim()
$details = netsh wlan show profile name="$name" key=clear
$password = ($details | Select-String "关键内容|Key Content") -split ":" | Select-Object -Last 1
Write-Host "网络: $name, 密码: $password"
}网络共享
文件共享
powershell
# 创建共享文件夹
New-SmbShare -Name "共享名称" -Path "C:\Share" -FullAccess "Everyone"
# 设置共享权限
Grant-SmbShareAccess -Name "共享名称" -AccountName "用户名" -AccessRight Full -Force
# 查看共享列表
Get-SmbShare
# 查看共享权限
Get-SmbShareAccess -Name "共享名称"
# 删除共享
Remove-SmbShare -Name "共享名称" -Force
# 使用 net share 命令
net share 共享名称=C:\Share /grant:everyone,full
net share # 查看共享
net share 共享名称 /delete # 删除共享高级共享设置
powershell
# 设置共享文件夹的 NTFS 权限
$acl = Get-Acl "C:\Share"
$permission = "用户名","FullControl","Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
Set-Acl "C:\Share" $acl
# 查看文件夹权限
Get-Acl "C:\Share" | Format-List
# 启用网络发现
# 设置 → 网络和 Internet → 网络和共享中心 → 高级共享设置
# 通过注册表启用文件共享
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" `
-Name AutoShareWks -Value 1访问网络共享
powershell
# 通过资源管理器访问
explorer \\服务器地址\共享名称
# 映射网络驱动器
New-PSDrive -Name "Z" -PSProvider FileSystem -Root "\\服务器\共享" -Persist
# 使用 net use 命令
net use Z: \\服务器\共享
net use Z: \\服务器\共享 /user:用户名 密码
net use # 查看映射
net use Z: /delete # 删除映射
# 断开网络驱动器
Remove-PSDrive -Name "Z" -Force网络故障排查
基本诊断命令
powershell
# 测试网络连通性
ping 192.168.1.1
ping www.baidu.com
ping -t 192.168.1.1 # 持续 ping
# 跟踪路由
tracert www.baidu.com
tracert -d www.baidu.com # 不解析主机名
# 查看路由表
route print
Get-NetRoute
# 查看网络统计
netstat -a # 所有连接
netstat -n # 数字形式显示
netstat -an # 组合使用
netstat -b # 显示程序名
netstat -o # 显示进程 ID高级诊断
powershell
# 测试端口连通性
Test-NetConnection -ComputerName www.baidu.com -Port 80
Test-NetConnection -ComputerName 192.168.1.1 -Port 3389
# 查看网络路径
Test-NetConnection -ComputerName www.baidu.com -TraceRoute
# 查看网络延迟
Test-Connection -ComputerName www.baidu.com -Count 4
# 详细网络诊断
Get-NetConnectionProfile
Get-NetIPConfiguration
Get-DnsClientCache
# 使用 pathping(结合 ping 和 tracert)
pathping www.baidu.com网络重置
powershell
# 重置网络适配器
netsh winsock reset
# 重置 IP 配置
netsh int ip reset
# 清除 DNS 缓存
ipconfig /flushdns
# 重置防火墙
netsh advfirewall reset
# 重新安装网络适配器
# 设备管理器 → 网络适配器 → 右键卸载 → 重启电脑
# 完整网络重置
# 设置 → 网络和 Internet → 网络重置防火墙配置
查看防火墙状态
powershell
# 查看防火墙状态
Get-NetFirewallProfile
# 查看所有配置文件状态
Get-NetFirewallProfile | Select-Object Name, Enabled
# 查看防火墙规则
Get-NetFirewallRule
# 查看入站规则
Get-NetFirewallRule -Direction Inbound | Where-Object { $_.Enabled -eq $true }
# 查看特定程序的规则
Get-NetFirewallRule -DisplayName "*Chrome*"配置防火墙
powershell
# 启用防火墙
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
# 禁用防火墙(不推荐)
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
# 启用特定配置文件
Set-NetFirewallProfile -Profile Private -Enabled True
# 使用 netsh 命令
netsh advfirewall set allprofiles state on
netsh advfirewall set allprofiles state off创建防火墙规则
powershell
# 创建入站规则允许端口
New-NetFirewallRule -DisplayName "允许端口 8080" `
-Direction Inbound `
-LocalPort 8080 `
-Protocol TCP `
-Action Allow
# 创建出站规则
New-NetFirewallRule -DisplayName "阻止程序联网" `
-Direction Outbound `
-Program "C:\Path\To\App.exe" `
-Action Block
# 允许特定程序
New-NetFirewallRule -DisplayName "允许程序" `
-Direction Inbound `
-Program "C:\Path\To\App.exe" `
-Action Allow
# 删除规则
Remove-NetFirewallRule -DisplayName "规则名称"
# 使用 netsh 创建规则
netsh advfirewall firewall add rule name="允许端口 8080" dir=in action=allow protocol=tcp localport=8080远程连接
远程桌面
powershell
# 启用远程桌面
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" `
-Name fDenyTSConnections -Value 0
# 禁用远程桌面
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" `
-Name fDenyTSConnections -Value 1
# 添加防火墙规则
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
# 连接远程桌面
mstsc /v:192.168.1.100
mstsc /v:192.168.1.100 /admin
# 查看远程桌面端口
Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" `
-Name PortNumber远程管理
powershell
# 启用 PowerShell 远程管理
Enable-PSRemoting -Force
# 测试远程连接
Test-WSMan -ComputerName 192.168.1.100
# 创建远程会话
$session = New-PSSession -ComputerName 192.168.1.100 -Credential (Get-Credential)
# 在远程会话执行命令
Invoke-Command -Session $session -ScriptBlock { Get-Process }
# 进入远程会话
Enter-PSSession -ComputerName 192.168.1.100
# 退出远程会话
Exit-PSSession
# 关闭会话
Remove-PSSession -Session $session网络监视
监视网络流量
powershell
# 查看网络适配器统计
Get-NetAdapterStatistics
# 查看当前连接
Get-NetTCPConnection
# 查看监听端口
Get-NetTCPConnection -State Listen
# 查看已建立的连接
Get-NetTCPConnection -State Established
# 查看特定进程的网络连接
Get-NetTCPConnection -OwningProcess (Get-Process chrome).Id
# 使用 netstat 监视
netstat -anob # 显示所有连接和程序网络性能监控
powershell
# 获取网络性能计数器
Get-Counter -Counter "\Network Interface(*)\Bytes Total/sec"
# 持续监控
Get-Counter -Counter "\Network Interface(*)\Bytes Total/sec" -Continuous
# 查看网络吞吐量
Get-NetAdapter | Select-Object Name, LinkSpeed, Status
# 查看网络丢包
Get-NetAdapterStatistics | Select-Object Name, ReceivedDiscarded, OutboundDiscarded小结
本章介绍了 Windows 网络配置的主要内容:
- 网络基础:了解网络类型和 IP 地址概念
- 适配器管理:查看和配置网络适配器
- IP 配置:设置静态 IP 和 DHCP
- DNS 配置:管理 DNS 服务器和缓存
- Wi-Fi 管理:连接和管理无线网络
- 网络共享:配置文件和打印机共享
- 故障排查:诊断网络问题的方法
- 防火墙:配置 Windows 防火墙规则
- 远程连接:设置远程桌面和远程管理
- 网络监视:监控网络流量和性能
建议:
- 熟练掌握基本网络诊断命令
- 定期检查网络安全设置
- 合理配置防火墙规则
- 使用 PowerShell 自动化网络管理任务
