技術文章 · IT 指令工具箱

Windows DHCP Server 設定:Scope、Option、Reservation 與備份

Windows DHCP 怎麼設定?整理授權、IPv4 Scope、排除範圍、Gateway、DNS、Reservation、Lease、備份與用量查詢 PowerShell。

作者 Steve Chen · 發布  · 約 9 分鐘閱讀 · 更新

IT 指令工具箱 — 廷皓技術專欄插圖

本頁以代號表示合成測試位址。操作前請依欄位要求填入隔離環境的測試值。

演練值如何代入。以 LAB_ 開頭的代號代表合成測試值。命令或設定中的代號,請先一致代換成隔離測試環境的值。前綴代號依欄位規格填入前綴長度,需要位址型別的欄位請填入合法的合成位址。代換後再驗證設定語法與預期結果。

建 DHCP scope 前先畫清楚 VLAN、gateway、relay、可發範圍、固定設備與 DNS。Scope 建好不代表用戶端拿得到 IP;跨 VLAN 還要有正確 DHCP relay。

23 組範例Windows Server 2019/2022/2025DhcpServer PowerShell module查核日期:2026-11-14
動手前:Scope、Option 003/006、Reservation 與授權錯誤會讓整個 VLAN 斷線或拿錯 DNS。新 scope 先保持 InActive,核對後再啟用;匯入與移轉前一定備份。

伺服器與 Scope 盤點

列出 AD 已授權 DHCP系統管理員 PowerShell

Get-DhcpServerInDC

網域環境中未授權 DHCP 可能停止服務。

授權 DHCP ServerDomain Admin PowerShell

Get-DhcpServerInDC | Select-Object DnsName,IPAddress

僅盤點已授權伺服器。有授權權限的人另走正式變更與隔離驗收。

列出 IPv4 ScopePowerShell

Get-DhcpServerv4Scope -ComputerName $LabDhcpServer | Format-Table ScopeId,Name,State,StartRange,EndRange,SubnetMask

核對測試伺服器、Scope 及位址池。

查看 Scope 用量PowerShell

Get-DhcpServerv4ScopeStatistics -ComputerName $LabDhcpServer | Sort-Object PercentageInUse -Descending

區分滿載、保留、排除與拒絕租約。

查看 Server-level optionPowerShell

Get-DhcpServerv4OptionValue -ComputerName $LabDhcpServer

站點或裝置還可能有範圍、保留、policy 或 class 指派。

查看 Scope optionPowerShell

Get-DhcpServerv4OptionValue -ComputerName $LabDhcpServer -ScopeId $LabScope

請先將 LabScope 設為實際核准的合成網段 ID。

建立 Scope、排除與 Option

建立停用中的 Scope系統管理員 PowerShell

Add-DhcpServerv4Scope -ComputerName $LabDhcpServer -Name 'Lab-Workstations' -StartRange $LabStart -EndRange $LabEnd -SubnetMask $LabMask -State InActive -WhatIf

先核對不與其他池重疊並留回復。

加入排除範圍系統管理員 PowerShell

Add-DhcpServerv4ExclusionRange -ComputerName $LabDhcpServer -ScopeId $LabScope -StartRange $LabExcludedStart -EndRange $LabExcludedEnd -WhatIf

先確認範圍內沒有依賴的既有租約。

設定 Gateway、DNS 與 suffix系統管理員 PowerShell

Set-DhcpServerv4OptionValue -ComputerName $LabDhcpServer -ScopeId $LabScope -Router $LabGateway -DnsServer $LabDnsServers -DnsDomain $LabDnsSuffix -WhatIf

LabDnsServers 用核准的測試位址陣列,不以未加引號的多個字面量代入。

設定租約時間系統管理員 PowerShell

Set-DhcpServerv4Scope -ComputerName $LabDhcpServer -ScopeId $LabScope -LeaseDuration ([TimeSpan]::FromDays(1)) -WhatIf

在變更前先盤點舊期限與測試裝置。

啟用 Scope系統管理員 PowerShell

Set-DhcpServerv4Scope -ComputerName $LabDhcpServer -ScopeId $LabScope -State Active -WhatIf

僅在確認已隔離、沒有重疊的測試網段操作。

暫停 Scope系統管理員 PowerShell

Set-DhcpServerv4Scope -ComputerName $LabDhcpServer -ScopeId $LabScope -State InActive -WhatIf

改變地址服務,需先準備回復。

Reservation、Lease 與備份

列出 ReservationPowerShell

Get-DhcpServerv4Reservation -ComputerName $LabDhcpServer -ScopeId $LabScope

用測試條件核對名稱、位址及 ClientId。

新增 Reservation系統管理員 PowerShell

Add-DhcpServerv4Reservation -ComputerName $LabDhcpServer -ScopeId $LabScope -IPAddress $LabReservedAddress -ClientId $LabClientId -Name 'Lab-Printer' -WhatIf

先從隔離用戶端的現有租約確定 ClientId。Windows 常見是 MAC;若測試客戶端確實以 MAC 識別,可將頁內 LAB_MAC 代入 LabClientId。其他裝置可能送不同 DHCP Client Identifier。

列出所有 LeasePowerShell

Get-DhcpServerv4Lease -ComputerName $LabDhcpServer -ScopeId $LabScope -AllLeases | Sort-Object IPAddress

結果可包含活動及不同狀態。

找 Declined 地址PowerShell

Get-DhcpServerv4Lease -ComputerName $LabDhcpServer -ScopeId $LabScope -BadLeases

分辨 IP 衝突與真正可用的地址,不批次刪除。

依 MAC 找 LeasePowerShell

Get-DhcpServerv4Lease -ComputerName $LabDhcpServer -ScopeId $LabScope -ClientId $LabClientId

核對租約送出的 ClientId,不能一律以網卡 MAC 判定。

備份 DHCP 資料庫系統管理員 PowerShell

Backup-DhcpServer -ComputerName $LabDhcpServer -Path $LabBackupLocation

核對伺服器側目的與權限,用只包含合成租約的服務測試。

匯出設定與租約系統管理員 PowerShell

Export-DhcpServer -ComputerName $LabDhcpServer -File $LabExportFile -Leases

明確核對檔案位置、保護及是否已存在;練習使用新檔,避免 -Force。

查看資料庫與稽核路徑PowerShell

Get-DhcpServerDatabase -ComputerName $LabDhcpServer

用副本核對實際備份位置;另查服務的稽核設定。

用戶端驗證

查看用戶端 DHCP 資訊Windows CMD(用戶端)

ipconfig /all

確認 DHCP Enabled、Server、Lease、Gateway、DNS。

重新取址系統管理員 CMD(用戶端)

ipconfig /renew "%LAB_ADAPTER%"

僅對測試介面,在 CMD 定義名稱,保留獨立管理通道。

查看 DHCP Client 事件PowerShell(用戶端)

Get-WinEvent -LogName 'Microsoft-Windows-Dhcp-Client/Admin' -MaxEvents 50

對照取得 169.254、NACK 與 renew 時間。

驗收與排錯

以核准測試裝置核對 ClientId、選項和取址

  1. 查合成 Scope、已授權服務、relay 及取得前後的 Lease;比對實際 ClientId/必要選項。LAB_MAC 僅代表合成的網卡識別,是否能當 ClientId 必須以該裝置的租約驗收。
  2. 測試介面 renew 後核對實際 Scope、租約、閘道與 DNS。若用戶端與伺服器都正常但無租約,查 VLAN、relay 和地址衝突。
  3. 比較備份及 XML 匯出能否依正式復原流程使用,將輸出留在受控演練環境。

常見錯誤

  • Scope 一建立就 Active。
  • Option 006 混用內部 DNS 與公用 DNS。
  • 把固定 IP 放進動態池,沒有 exclusion 或 reservation。
  • 只看 DHCP Server,不查交換器 VLAN、relay/IP helper 與 ACL。

常見問題

電腦拿到 169.254 是 DHCP 壞掉嗎?

表示這次沒有拿到 IPv4 租約,但原因可能在網卡、VLAN、relay、ACL、scope 滿載、server 未授權或 DHCP service;要沿封包路徑查。

印表機應該手動固定 IP 還是 reservation?

能由 DHCP 管理時通常優先 reservation,方便集中記錄與改 DNS/gateway;若設備必須手動固定,也要放在動態池之外並寫入 IP 清冊。

延伸閱讀

版本與官方文件

參數會隨工具版本與作業系統實作改變。正式環境先用 --help、-h 或系統內建說明確認,再以當版官方文件為準。

查核資料

  1. Microsoft DHCP Server 模組
  2. Scope 設定
  3. Microsoft Add-DhcpServerv4Reservation
  4. Microsoft Export-DhcpServer 指令
  5. Microsoft Backup-DhcpServer 指令
  6. Microsoft ipconfig

常見問題

電腦拿到 169.254 是 DHCP 壞掉嗎?

表示這次沒有拿到 IPv4 租約,但原因可能在網卡、VLAN、relay、ACL、scope 滿載、server 未授權或 DHCP service;要沿封包路徑查。

印表機應該手動固定 IP 還是 reservation?

能由 DHCP 管理時通常優先 reservation,方便集中記錄與改 DNS/gateway;若設備必須手動固定,也要放在動態池之外並寫入 IP 清冊。

聯絡廷皓討論 看更多文章