Prerequisite
由于独立服务器相关角色已经安装并配置完成,以下部分内容由 Windows Server 2025 虚拟机补充
- 独立服务器所装 OS 为 Windows Server 2022 Datacenter,并在 BIOS 或 UEFI 中开启了虚拟化支持
- 掌握 Active Directory(AD 域), DNS, DHCP 的基础概念以及相关配置
- 能够执行和理解基本的 PowerShell 命令
安装相关角色以及管理工具
此处安装角色顺序:DNS 服务器 --> DHCP 服务器 --> AD 域 --> Hyper-V
安装 AD 域
# 安装 Active Directory 域服务以及相关管理工具
PS C:\Users\Administrator> Install-WindowsFeature -name AD-Domain-Services -IncludeManagementTools
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Active Directory 域服务, 组策略管理, Acti...
安装 Hyper-V
# 安装 Hyper-V 以及相关管理工具
PS C:\Users\Administrator> Install-WindowsFeature Hyper-V -IncludeManagementTools
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True Yes SuccessRest... {Hyper-V, Windows PowerShell 的 Hyper-V 模...
警告: 必须重新启动此服务器才能完成安装过程。
PS C:\Users\Administrator> Restart-Computer -Force
若创建外部虚拟交换机则会导致失联,原因是新创建的外部虚拟交换机虚拟网卡配置破坏了原来物理网卡的配置,重新配置网卡即可
Windows Server 2022 : Hyper-V : Install : Server World
安装 DHCP 服务器
# 安装 DHCP 服务器以及相关管理工具
PS C:\Users\Administrator> Install-WindowsFeature DHCP -IncludeManagementTools
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {DHCP 服务器, DHCP 服务器工具}
安装 DNS 服务器
# 安装 DNS 服务器以及相关管理工具
PS C:\Users\Administrator> Install-WindowsFeature DNS -IncludeManagementTools Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {DNS 服务器, DNS 服务器工具}
配置角色以及相关服务
配置 Hyper-V
设置 Hyper-V 默认存储路径
# 创建 Hyper-V 目录结构
New-Item -ItemType Directory -Path "D:\Hyper-V" -Force
New-Item -ItemType Directory -Path "D:\Hyper-V\Virtual Hard Disk" -Force
# 设置虚拟机配置文件的默认位置
Set-VMHost -VirtualMachinePath "D:\Hyper-V"
# 设置虚拟硬盘的默认位置
Set-VMHost -VirtualHardDiskPath "D:\Hyper-V\Virtual Hard Disk"
创建新的内部虚拟交换机,并配置静态 IP 地址以及子网前缀长度
# 创建新的虚拟交换机
New-VMSwitch -Name "Internal Virtual Switch" -SwitchType Internal
# 配置静态 IP 地址
$interface = Get-NetAdapter -Name "vEthernet (Internal Virtual Switch)"
New-NetIPAddress -InterfaceIndex $interface.ifIndex -IPAddress 192.168.137.1 -PrefixLength 24
获取配置的虚拟交换机信息以及网络接口相关的 IP 地址配置
PS C:\Users\Administrator> Get-VMSwitch
Name SwitchType NetAdapterInterfaceDescription
---- ---------- ------------------------------
Internal Virtual Switch Internal
PS C:\Users\Administrator> Get-NetIPAddress -InterfaceAlias "vEthernet (Internal Virtual Switch)"
IPAddress : fe80::1d6f:a2ca:16b:bafb%4
InterfaceIndex : 4
InterfaceAlias : vEthernet (Internal Virtual Switch)
AddressFamily : IPv6
Type : Unicast
PrefixLength : 64
PrefixOrigin : WellKnown
SuffixOrigin : Link
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
IPAddress : 192.168.137.1
InterfaceIndex : 4
InterfaceAlias : vEthernet (Internal Virtual Switch)
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
配置 NAT 网络
配置 NAT 网络,命名为 "Primary-NAT",使用 192.168.137.0/24
# 配置 NAT
New-NetNat -Name "Primary-NAT" -InternalIPInterfaceAddressPrefix "192.168.137.0/24"
查询系统中当前配置的所有 NAT 设置
PS C:\Users\Administrator> Get-NetNat
Name : Primary-NAT
ExternalIPInterfaceAddressPrefix :
InternalIPInterfaceAddressPrefix : 192.168.137.0/24
IcmpQueryTimeout : 30
TcpEstablishedConnectionTimeout : 1800
TcpTransientConnectionTimeout : 120
TcpFilteringBehavior : AddressDependentFiltering
UdpFilteringBehavior : AddressDependentFiltering
UdpIdleSessionTimeout : 120
UdpInboundRefresh : False
Store : Local
Active : True
配置 AD 域
将服务器提升为 DC 域控制器。创建新林,有关林根域命名的规范,请参考如下
Active Directory: Best Practices for Internal Domain and Network Names | Microsoft Learn
此处选择 compute.microcharon.com
作为林根域
# 创建新林
Install-ADDSForest -DomainName "compute.microcharon.com"
# 或者指定 DSRM 密码
Install-ADDSForest -DomainName "compute.microcharon.com" `
-ForestMode WinThreshold `
-DomainMode WinThreshold `
-DomainNetbiosName COMPUTE `
-SafeModeAdministratorPassword (ConvertTo-SecureString -AsPlainText "YourComplexPassword123!@#" -Force)
PS C:\Users\Administrator> Install-ADDSForest -DomainName "compute.microcharon.com" `
-ForestMode WinThreshold `
-DomainMode WinThreshold `
-DomainNetbiosName FD3S01 `
-SafeModeAdministratorPassword (ConvertTo-SecureString -AsPlainText "YourComplexPassword123!@#" -Force)
目标服务器将配置为域控制器并在此操作完成时重新启动。
是否要继续此操作?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“Y”): A
警告: 此计算机上至少有一个物理网络适配器未将静态 IP 地址分配给其 IP 属性。如果同时为某个网络适配器启用 IPv4 和
IPv6,则应将 IPv4 和 IPv6 静态 IP 地址分配给该物理网络适配器的 IPv4 和 IPv6 属性。应对所有物理网络适配器执行此类静态 IP
地址分配,以便执行可靠的域名系统(DNS)操作。
警告: 无法创建该 DNS 服务器的委派,因为无法找到有权威的父区域或者它未运行 Windows DNS 服务器。如果你要与现有 DNS
基础结构集成,应在父区域中手动创建对该 DNS
服务器的委派,以确保来自域“compute.microcharon.com”以外的可靠名称解析。否则,不需要执行任何操作。
警告: 此计算机上至少有一个物理网络适配器未将静态 IP 地址分配给其 IP 属性。如果同时为某个网络适配器启用 IPv4 和
IPv6,则应将 IPv4 和 IPv6 静态 IP 地址分配给该物理网络适配器的 IPv4 和 IPv6 属性。应对所有物理网络适配器执行此类静态 IP
地址分配,以便执行可靠的域名系统(DNS)操作。
Message Context RebootRequired Status
------- ------- -------------- ------
已成功完成操作 DCPromo.General.3 False Success
配置完成后自动重启,并自动加入到域网络,防火墙规则要允许域网络透过远程桌面服务这一项规则,否则无法进行远程桌面连接到你的独立服务器
配置 DNS 服务器
添加 DNS 转发器
添加 DNS 转发器 119.29.29.29 以及 223.5.5.5
# 添加 DNS 转发器
Add-DnsServerForwarder -IPAddress 119.29.29.29 -PassThru
Add-DnsServerForwarder -IPAddress 223.5.5.5 -PassThru
# 或者使用直接设置多个转发器
Set-DnsServerForwarder -IPAddress 119.29.29.29,223.5.5.5
配置 DNS 监听地址
默认配置 DNS 监听地址为全部,如需配置仅监听 192.168.137.1,则参考如下
# 获取当前 DNS 服务器的所有设置
$DnsServerSettings = Get-DnsServerSetting -ALL
# 设置监听 IP 地址为 192.168.137.1
$DnsServerSettings.ListeningIpAddress = @("192.168.137.1")
# 应用新设置
Set-DnsServerSetting $DnsServerSettings
检查 DNS 转发器设置并测试 DNS 解析
# 检查 DNS 转发器设置
PS C:\Users\Administrator> Get-DnsServerForwarder
UseRootHint : True
Timeout(s) : 3
EnableReordering : True
IPAddress : {119.29.29.29, 223.5.5.5}
ReorderedIPAddress : {119.29.29.29, 223.5.5.5}
# 测试 DNS 解析
PS C:\Users\Administrator> Resolve-DnsName www.bing.com -Server 192.168.137.1
Name Type TTL Section NameHost
---- ---- --- ------- --------
www.bing.com CNAME 40 Answer www-www.bing.com.trafficmanager.net
www-www.bing.com.trafficmanage CNAME 40 Answer cn-bing-com.cn.a-0001.a-msedge.net
r.net
cn-bing-com.cn.a-0001.a-msedge CNAME 40 Answer china.bing123.com
.net
Name : china.bing123.com
QueryType : A
TTL : 500
Section : Answer
IP4Address : 202.89.233.100
Name : china.bing123.com
QueryType : A
TTL : 500
Section : Answer
IP4Address : 202.89.233.101
Restart-Service DNS
重启 DNS 服务器使应用生效
创建正反向查找区域
正向区域将 DNS 名称转为 IP 地址,反向区域将 IP 转为 DNS 名称(PTR 记录)
正向区域:hyperv.local
反向区域: 137.168.192.in-addr.arpa.dns
# 在DNS管理器中创建正向查找区域,AD 集成主要区域
Add-DnsServerPrimaryZone -Name "hyperv.local" -ReplicationScope "Domain" -DynamicUpdate "Secure"
# 创建反向查找区域,AD 集成主要区域
Add-DnsServerPrimaryZone -NetworkID "192.168.137.0/24" -ReplicationScope "Domain" -DynamicUpdate "Secure"
配置 DHCP 服务器
创建 DHCP 安全组
创建 DHCP 服务器安全组,并在 AD 域控制器中授权 DHCP 服务器
Add-DhcpServerSecurityGroup -ComputerName "mSV2412250018-4.compute.microcharon.com"
Add-DhcpServerInDC -DnsName "mSV2412250018-4.compute.microcharon.com"
查看安全组
查看安全组 "DHCP Administrators" 和 "DHCP Users"
PS C:\Users\Administrator> Get-ADGroup "DHCP Administrators"
DistinguishedName : CN=DHCP Administrators,CN=Users,DC=compute,DC=microcharon,DC=com
GroupCategory : Security
GroupScope : DomainLocal
Name : DHCP Administrators
ObjectClass : group
ObjectGUID : ccfb6791-f0f2-4e19-a32f-926fbb0bdb7e
SamAccountName : DHCP Administrators
SID : S-1-5-21-3461284959-2398211348-1383175550-1007
PS C:\Users\Administrator> Get-ADGroup "DHCP Users"
DistinguishedName : CN=DHCP Users,CN=Users,DC=compute,DC=microcharon,DC=com
GroupCategory : Security
GroupScope : DomainLocal
Name : DHCP Users
ObjectClass : group
ObjectGUID : 2bbc32c4-dc33-49fb-be88-f0d1f46f5c5a
SamAccountName : DHCP Users
SID : S-1-5-21-3461284959-2398211348-1383175550-1006
新建作用域
新建作用域 192.168.137.0
- 默认网关(Router): 192.168.137.1
- DNS 服务器地址: 192.168.137.1
- DNS 域名: hyperv.local
# 添加 DHCP 作用域
Add-DhcpServerv4Scope -Name "Internal Virtual Switch" `
-StartRange 192.168.137.2 -EndRange 192.168.137.254 `
-SubnetMask 255.255.255.0 -State Active
# 设置 DHCP 选项
Set-DhcpServerv4OptionValue -ScopeId 192.168.137.0 `
-Router 192.168.137.1 `
-DnsServer 192.168.137.1 `
-DnsDomain "hyperv.local"
设置 DHCP 服务器 DNS 动态更新策略,并配置 DHCP 作用域参数,此处设置租期时间为 1 天
# 配置 DHCP 作用域的 DNS 设置
Set-DhcpServerv4DnsSetting -DynamicUpdates "Always" `
-DeleteDnsRRonLeaseExpiry $True `
-UpdateDnsRRForOlderClients $True
# 为特定作用域配置 DNS 设置
Set-DhcpServerv4Scope -ScopeId 192.168.137.0 -LeaseDuration 1.00:00:00 -State Active
查看 DHCP DNS 设置,作用域信息,以及作用域 192.168.137.0 的选项值
PS C:\Users\Administrator> Get-DhcpServerv4DnsSetting
DynamicUpdates : Always
DeleteDnsRROnLeaseExpiry : True
UpdateDnsRRForOlderClients : True
DnsSuffix :
DisableDnsPtrRRUpdate : False
NameProtection : False
PS C:\Users\Administrator> Get-DhcpServerv4Scope
ScopeId SubnetMask Name State StartRange EndRange LeaseDuration
------- ---------- ---- ----- ---------- -------- -------------
192.168.137.0 255.255.255.0 Internal Vi... Active 192.168.137.2 192.168.137.254 1.00:00:00
PS C:\Users\Administrator> Get-DhcpServerv4OptionValue -ScopeId 192.168.137.0
OptionId Name Type Value VendorClass UserClass PolicyName
-------- ---- ---- ----- ----------- --------- ----------
81 DWord {23}
15 DNS 域名 String {hyperv.local}
3 路由器 IPv4Add... {192.168.137.1}
6 DNS 服务器 IPv4Add... {192.168.137.1}
51 租约 DWord {86400}
若未更改则尝试 Restart-Service -Name DHCPServer
重启 DHCP Server (DHCPServer) 使应用生效
配置 DHCP-DNS 集成
新添加用户 "Dynamic DNS" 供 DHCP 有权限更新 DNS 记录的域用户
# 创建新用户并设置密码
$Password = ConvertTo-SecureString "YourComplexPassword123!@#" -AsPlainText -Force
New-ADUser -Name "Dynamic DNS" `
-SamAccountName "DDNS" `
-Description "供 DHCP 有权限更新 DNS 记录的域账户" `
-AccountPassword $Password `
-Enabled $true `
-PasswordNeverExpires $true `
-CannotChangePassword $true
# 将用户添加到 DnsUpdateProxy 组
Add-ADGroupMember -Identity "DnsUpdateProxy" -Members "DDNS"
设置 DHCP 服务器 DNS 凭据
# 启用DHCP-DNS集成
Set-DhcpServerDnsCredential -ComputerName "mSV2412250018-4.compute.microcharon.com" -Credential (Get-Credential)
查看 DHCP 服务器 DNS 凭据
PS C:\Users\Administrator> Get-DhcpServerDnsCredential
UserName DomainName
-------- ----------
DDNS COMPUTE
验证成效
以 fnOS 为例,创建一个虚拟机
# 设置变量
$VMName = "fnOS Test"
$VMGeneration = 2
$VMProcessorCount = 2
$VMMemory = 4096MB
$VMSwitchName = "Internal Virtual Switch"
$VHDPath = "D:\Hyper-V\Virtual Hard Disk\fnOS Test.vhdx"
$ISOPath = "D:\Image\fnOS-TRIM-0.8.39-685.iso"
$VHDSize = 127GB
# 创建虚拟硬盘
New-VHD -Path $VHDPath -SizeBytes $VHDSize -Dynamic
# 创建新的虚拟机
New-VM -Name $VMName -Generation $VMGeneration -MemoryStartupBytes $VMMemory -VHDPath $VHDPath -SwitchName $VMSwitchName
# 配置虚拟机处理器数量
Set-VMProcessor -VMName $VMName -Count $VMProcessorCount
# 禁用动态内存
Set-VMMemory -VMName $VMName -DynamicMemoryEnabled $false
# 挂载ISO文件
Add-VMDvdDrive -VMName $VMName -Path $ISOPath
# 设置从DVD启动
$DVDDrive = Get-VMDvdDrive -VMName $VMName
Set-VMFirmware -VMName $VMName -FirstBootDevice $DVDDrive
# 禁用安全启动
Set-VMFirmware -VMName $VMName -EnableSecureBoot Off
# 显示虚拟机信息
Get-VM -Name $VMName | Format-List Name, State, CPUUsage, MemoryAssigned, Uptime, Status
WebUI 上配置设备名称(hostname)和用户信息
重启后可见已在正反向查找区域添加 DNS 记录以及分配了 IP
PS C:\Users\Administrator> ssh [email protected]
The authenticity of host 'fnos-test.hyperv.local (192.168.137.11)' can't be established.
ED25519 key fingerprint is SHA256:U/ogenYRr1jwfvzK5/eu97j/d5v81gvoNhKJOuyLhGs.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'fnos-test.hyperv.local' (ED25519) to the list of known hosts.
[email protected]'s password:
Linux fnos-test 6.6.38-trim #80 SMP PREEMPT_DYNAMIC Tue Dec 10 20:45:43 CST 2024 x86_64
Last login: Mon Mar 3 15:31:21 2025
Could not chdir to home directory /home/microcharon: No such file or directory
microcharon@fnos-test:/$ nslookup bing.com 192.168.137.1
Server: 192.168.137.1
Address: 192.168.137.1#53
Non-authoritative answer:
Name: bing.com
Address: 13.107.21.200
Name: bing.com
Address: 204.79.197.200
Name: bing.com
Address: 2620:1ec:c11::200
microcharon@fnos-test:/$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "resolvectl status" to see details about the actual nameservers.
nameserver 192.168.137.1
search hyperv.local
参考资料
如何在 Windows 中配置 DNS 动态更新 - Windows Server | Microsoft Learn
Server2022\_DHCP-DNS-AD域环境搭建\_windows server 2022的dns服务-CSDN博客
Windows Server 2022 : Download : Server World
Active Directory: Best Practices for Internal Domain and Network Names | Microsoft Learn
命名计算机、域、站点和 OU - Windows Server | Microsoft Learn