---
id: linux-windows
title: "Windows Server"
shortTitle: "Windows Server"
track: linux
trackTitle: "Linux & Shell Scripting"
order: 9
level: important
status: published
estimatedMinutes: 30
prerequisites:
  - linux-editors
next: logs-files
reviewedAgainst: 2026-10-12
sources:
  - title: "Windows Server documentation"
    url: https://learn.microsoft.com/en-us/windows-server/
  - title: "OpenSSH Server configuration for Windows"
    url: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration
---

# Windows Server

Lộ trình này lấy Linux làm môi trường chính. Bạn sẽ gặp Windows Server khi app .NET, IIS hoặc Active Directory đang chạy trên đó. Bài này đủ để bạn đọc được tình trạng của máy, chưa phải để quản trị domain.

## Để làm gì

Sau bài này, bạn xem được service nào đang chạy, mở shell từ xa bằng OpenSSH nếu tính năng đó đã bật, và không còn lỡ gõ `systemctl` trên Windows.

## Gồm những gì

- Service và `Get-Service`.
- Event Log, thứ thay cho `journalctl`.
- OpenSSH trên Windows, với file `sshd_config` riêng.

Bài này không nói về Active Directory, Group Policy hay IIS. IIS có bài riêng.

## Dùng khi nào

Bạn cần bài này khi một server trong inventory chạy Windows, khi pipeline phải chạy `powershell` thay vì `bash`, hoặc khi phải tìm hiểu vì sao site IIS không lên. Các bài khác vẫn mặc định dùng Linux.

Đừng dùng module `systemd` trong playbook Ansible cho host Windows. Inventory cần `ansible_shell_type=powershell` hoặc connection WinRM. Bài Ansible chỉ chạy trên localhost Linux.

## Kiến thức cốt lõi

Trên Windows, phần mềm server thường chạy dưới dạng service. `Get-Service W3SVC` xem World Wide Web Publishing, tức service của IIS. `Get-Service sshd` xem OpenSSH Server. Status `Running` nghĩa là service đang chạy, còn `Stopped` nghĩa là nó không nghe port nào.

Log nằm trong Event Log. `Get-WinEvent -LogName System -MaxEvents 5` đọc vài sự kiện mới nhất. Windows thuần không có `/var/log/syslog`.

OpenSSH Server là một capability của Windows, cấu hình nằm ở `C:\ProgramData\ssh\sshd_config`. Sửa xong thì restart service `sshd`. Đừng copy nguyên file cấu hình từ Ubuntu sang, vì directive và đường dẫn user trên Windows khác.

Shell ở đây là PowerShell. Gõ `ls` đôi khi vẫn chạy nhờ alias, nhưng script đưa lên server nên gọi `Get-ChildItem` để hành vi ổn định. `bash` chỉ có khi bạn cài riêng.

## Lệnh và code mẫu

Trên PowerShell:

```powershell
Get-Service sshd, W3SVC -ErrorAction SilentlyContinue |
  Select-Object Name, Status, StartType
Get-WinEvent -LogName System -MaxEvents 3 |
  Select-Object TimeCreated, Id, ProviderName
```

Nhờ `SilentlyContinue`, service nào không tồn tại thì chỉ vắng dòng của nó, lệnh không báo lỗi.

## Thực hành

1. Mở PowerShell trên máy Windows bạn đang dùng. Windows 11 không phải Windows Server, nhưng `Get-Service` có cùng cú pháp.
2. Chạy khối lệnh trên và ghi lại những service có mặt.
3. Đừng bật OpenSSH Server hay đổi firewall chỉ để làm xong bài.

## Kết quả mong đợi

Bạn thấy một bảng gồm Name, Status, StartType. Máy không cài IIS thì không có dòng `W3SVC`. Phần Event Log in ra ba sự kiện của log System.

## Checklist

- [ ] Nhận ra host Windows trong inventory trước khi chạy lệnh Bash lên nó.
- [ ] Khi service không start, đọc Event Log thay vì đi tìm `/var/log`.
- [ ] Restart `sshd` sau khi sửa `C:\ProgramData\ssh\sshd_config`.

## Thẻ ôn

### linux-windows-service
kind: trap
cue: Trên Windows Server không có `systemctl`. Xem SSH bằng `Get-Service sshd`, khởi động lại bằng `Restart-Service sshd`. Service web của IIS là `W3SVC`.

Q: Bạn đang ở PowerShell trên Windows Server và gõ `systemctl status ssh` theo thói quen Linux. Lệnh không chạy. Xem và khởi động lại SSH bằng gì, còn site IIS thì service tên gì?

A: Windows không dùng systemd, nên không có `systemctl`. OpenSSH Server là một Windows service tên `sshd`. Xem trạng thái bằng `Get-Service sshd`, khởi động lại bằng `Restart-Service sshd`. Nhầm sang tên service của Linux thì bạn sẽ tưởng SSH không tồn tại trong khi nó đang chạy dưới tên khác. Site IIS không nằm trong service `sshd`. Service web của IIS là `W3SVC`.

### linux-windows-sshd
kind: command
cue: OpenSSH Server trên Windows đọc `C:\ProgramData\ssh\sshd_config`. Sửa file trong `System32` thì service vẫn đọc file kia.

Q: Cần đổi cấu hình OpenSSH Server trên Windows. File nào là file service đang đọc, và sau khi sửa thì làm gì để nó có hiệu lực?

A: File là `C:\ProgramData\ssh\sshd_config`. Sửa xong phải restart service `sshd`, vì tiến trình đang chạy không tự nhặt file mới trên đĩa. Nếu bạn sửa một bản `sshd_config` nằm dưới `C:\Windows\System32`, service vẫn đọc file trong ProgramData. Lần restart sau cấu hình không đổi, và bạn dễ kết luận nhầm là restart không ăn.

## Lỗi thường gặp

**Gõ `systemctl status ssh` trên Windows.** Windows không có systemd. Dùng `Get-Service sshd` và `Restart-Service sshd`.

**Làm theo bài hướng dẫn Linux và sửa `sshd_config` trong `C:\Windows\System32`.** Bản OpenSSH cho Windows đọc `C:\ProgramData\ssh\sshd_config`. Sửa nhầm file thì restart xong service vẫn đọc file kia.

## Nguồn

1. [Windows Server documentation](https://learn.microsoft.com/en-us/windows-server/) — vai trò của hệ điều hành server.
2. [OpenSSH Server configuration for Windows](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration) — đường dẫn `sshd_config` và service.

## Tiếp theo

[Đọc log trong /var/log](../logs/01-files.md)
