在没有图形界面的 Windows Server 2022 Server Core 版本中,虽然无法使用传统的 GUI 工具(如控制面板或服务器管理器),但你仍然可以通过命令行工具、PowerShell 和配置工具来高效地配置网络和防火墙。以下是详细的操作方法:
一、配置网络设置
1. 查看当前网络配置
# 查看所有网络适配器信息
Get-NetIPConfiguration
# 或使用 netsh(传统方式)
netsh interface ipv4 show config
2. 设置静态 IP 地址
假设你要为名为 “Ethernet” 的网卡设置:
- IP: 192.168.1.100
- 子网掩码: 255.255.255.0
- 网关: 192.168.1.1
- DNS: 8.8.8.8
New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses 8.8.8.8
💡 提示:
-PrefixLength 24表示子网掩码255.255.255.0
3. 改为 DHCP 获取 IP
Remove-NetIPAddress -InterfaceAlias "Ethernet" -Confirm:$false
Set-NetIPInterface -InterfaceAlias "Ethernet" -DHCP Enabled
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddresses
4. 修改网卡名称(可选)
Rename-NetAdapter -Name "Ethernet" -NewName "LAN"
二、配置防火墙(Windows Defender 防火墙)
1. 查看防火墙状态
# 查看防火墙整体状态
Get-NetFirewallProfile
# 查看所有允许的规则
Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True"} | Select-Object Name, Direction, Action
2. 启用/禁用防火墙(按配置文件)
# 启用域、专用、公用防火墙
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled True
# 禁用(不推荐生产环境)
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled False
3. 允许特定端口(例如开放 RDP 3389 和 HTTP 80)
# 开放 TCP 80 端口
New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow
# 开放远程桌面(TCP 3389)
New-NetFirewallRule -DisplayName "Allow RDP" -Direction Inbound -Protocol TCP -LocalPort 3389 -Action Allow
# 开放自定义端口(如 8080)
New-NetFirewallRule -DisplayName "Allow Port 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow
4. 删除防火墙规则
Remove-NetFirewallRule -DisplayName "Allow HTTP"
5. 快速启用预定义规则(如文件共享、Ping等)
# 允许 ICMPv4(ping)
netsh advfirewall firewall add rule name="ICMP Allow" dir=in action=allow protocol=icmpv4
# 或使用 PowerShell:
New-NetFirewallRule -DisplayName "Allow ICMPv4" -Protocol ICMPv4 -IcmpType 8 -Action Allow
三、其他常用网络配置
1. 设置主机名
Rename-Computer -NewName "MyServerCore"
重启后生效:
Restart-Computer
2. 加入域(需网络可达域控)
Add-Computer -DomainName "example.com" -Credential exampleadministrator
3. 配置远程管理(强烈建议)
确保开启 WinRM 以便远程 PowerShell 管理:
Enable-PSRemoting -Force
然后从另一台机器连接:
Enter-PSSession -ComputerName 192.168.1.100 -Credential Administrator
四、实用工具汇总
| 功能 | 命令 |
|---|---|
| 查看 IP | Get-NetIPConfiguration |
| 设置 IP | New-NetIPAddress |
| 设置 DNS | Set-DnsClientServerAddress |
| 防火墙规则 | Get-NetFirewallRule, New-NetFirewallRule |
| 测试网络 | Test-NetConnection 8.8.8.8 -Port 53 |
| 重启网络适配器 | Restart-NetAdapter -Name "Ethernet" |
五、小贴士
- 使用 PowerShell 远程管理 是管理 Server Core 的最佳实践。
- 可以通过 Sconfig 工具进行基础配置(输入
sconfig启动文本菜单)。 - 推荐保留至少一个管理员本地登录权限,避免远程锁死。
✅ 总结:
尽管没有图形界面,Windows Server Core 依然功能完整,通过 PowerShell 和命令行可以精确控制网络与防火墙设置,甚至比 GUI 更适合自动化和脚本化部署。
如需进一步帮助(比如批量配置脚本),欢迎继续提问!
云小栈