Force Logoff When Logon Hours Expire คืออะไร?
นโยบาย Group Policy ใน Windows ที่บังคับให้ผู้ใช้ Log off อัตโนมัติเมื่อเวลาที่อนุญาตให้ Logon (Logon Hours) หมดอายุ ตั้งอยู่ที่:
Computer Configuration → Windows Settings → Security Settings → Local Policies → Security Options → "Network security: Force logoff when logon hours expire"
สาเหตุที่ไม่ทำงาน (Common Causes)
| สาเหตุ | รายละเอียด | วิธีตรวจสอบ/แก้ไข |
|---|---|---|
| GPO ไม่ได้ Apply | GPO ไม่ Link ไป OU ที่ถูกต้อง, Block Inheritance, Enforced ไม่ถูกต้อง | รัน gpresult /h report.html บน Client ดู GPO ที่ Apply |
| Logon Hours ไม่ได้ตั้งค่า | User Property → Account → Logon Hours ยังไม่ได้จำกัดเวลา | Active Directory Users and Computers → User → Account → Logon Hours |
| Client ไม่ใช่ Domain Member | ทำงานได้เฉพาะ Domain-joined Computer | Workgroup ไม่ทำงาน ต้อง Join Domain |
| Kerberos/Time Sync Issue | Time Skew > 5 นาที ระหว่าง Client และ DC | w32tm /query /status, sync กับ PDC Emulator |
| Cached Credentials | User Login แบบ Offline (Cached) ไม่ตรวจสอบ Logon Hours | ต้อง Online กับ DC, หรือ Disable Cached Logons |
| Policy Setting: "Enabled" แต่ไม่ Force | บาง Version ต้อง Reboot หรือ gpupdate /force | gpupdate /force → Reboot |
| Local Policy Override | Local Security Policy (secpol.msc) ตั้งค่าขัดแย้ง | ดู Effective Policy ด้วย RSOP.msc |
| Service Account / System Account | บัญชี Service ไม่ได้รับผลกระทบ | Logon Hours ตั้งได้แค่ User Account |
ขั้นตอน Troubleshooting แบบเป็นขั้นตอน
- ตรวจสอบ GPO Apply: บน Client รัน
gpresult /h gpreport.htmlเปิดดูว่า Policy "Network security: Force logoff when logon hours expire" เป็น Enabled และ Winning GPO - ตรวจสอบ Logon Hours: ADUC → User → Account → Logon Hours → ต้องมีเวลาที่ Deny (สีแดง/ขีดทับ)
- Time Sync:
w32tm /query /statusต้อง Sync กับ DC, Offset < 1 วินาที - Force Update:
gpupdate /forceแล้ว Reboot - ทดสอบ: Login ด้วย User นั้น รอจนเวลา Logon Hours หมด → ควรถูก Logoff อัตโนมัติ
- ดู Event Log: Event Viewer → Security → Event ID 4647 (Logoff), 4625 (Failed logon)
การตั้งค่า Logon Hours ใน Active Directory
- เปิด Active Directory Users and Computers (dsa.msc)
- ขวาที่ User → Properties → แท็บ Account
- กด Logon Hours...
- เลือกช่วงเวลา → Logon Denied (สีแดง) สำหรับเวลาที่ไม่อนุญาต
- OK → Apply → OK
การ Force Logoff ผ่าน Group Policy (Script/Scheduled Task)
หาก GPO Built-in ไม่ทำงาน สามารถใช้ Script แทน:
# PowerShell Script รันเป็น Scheduled Task ทุก 15 นาที
$users = Get-ADUser -Filter {Enabled -eq $true} -Properties LogonHours
foreach ($user in $users) {
$logonHours = $user.LogonHours
if ($logonHours -and (Test-LogonHourDenied $logonHours)) {
# Force logoff: query session → logoff
$sessions = query session /server:$env:COMPUTERNAME
# Parse และ logoff session ของ user นั้น
}
}
หรือใช้ logoff.exe หรือ shutdown /l /f ใน Context ของ User Session
คำถามที่พบบ่อย
Logon Hours ทำงานกับ Local Account ได้ไหม?
ไม่ได้** Logon Hours เป็น Feature ของ Active Directory เท่านั้น Local Account ใช้ Local Policy: "Local Policies → Security Options → Network security: Force logoff when logon hours expire" แต่ต้องตั้ง Logon Hours ผ่าน net user username /time:M-F,8am-5pm
User อยู่ Logged In แล้ว เวลาหมด จะเกิดอะไร?
ถ้า Policy ทำงาน: Session จะถูก Force Logoff (Event ID 4647) ทุกอย่างปิด หาย Unsaved Work
Logoff vs Disconnect Remote Desktop
- Logoff (Sign out): จบ Session, ปิดทุกอย่าง, Unsaved Data หาย
- Disconnect: Session ค้างอยู่บน Server, กลับมาต่อได้ (ถ้าไม่หมด Idle Timeout)
Event ID สำคัญสำหรับ Audit
- 4647: User initiated logoff
- 4634: An account was logged off
- 4625: Logon failure (Logon Hours restriction = Substatus 0xC000006F)
- 4769/4770: Kerberos TGS Request/Renewal (Time check)
สรุป
Force Logoff When Logon Hours Expire ต้องการ: 1) Domain Environment 2) GPO Apply ถูกต้อง 3) Logon Hours ตั้งค่าบน User 4) Time Sync ถูกต้อง 5) User Online กับ DC หากไม่ทำงาน ตรวจ gpresult, RSOP, Time, Event Log แล้วแก้จุดที่ผิด