🔧 Windows FTP Server 從零開始:虛擬目錄、權限、TLS 設定與 PowerShell 快速建置流程
FTP(File Transfer Protocol)仍是許多企業內部交換檔案的重要管道。 Windows Server 內建 IIS FTP 服務,搭配 FTPS(SSL/TLS)可以兼具相容性與安全性。 本文提供完整建置方式,包含 GUI、PowerShell、自動化部署、虛擬目錄、TLS 憑證、Firewall 等設定。
一、安裝 FTP Server(GUI)
- 開啟 Server Manager → Add Roles and Features
- 選擇「Web Server (IIS)」
- 勾選「FTP Server」底下兩項:
- FTP Service
- FTP Extensibility
- Install 完成
二、使用 PowerShell 安裝 FTP(建議)
# 安裝 IIS + FTP 服務
Install-WindowsFeature Web-Server, Web-Ftp-Server, Web-Mgmt-Tools
# 確認功能
Get-WindowsFeature Web-Ftp-Server
三、建立 FTP 根目錄與本機帳號
此帳號可用於 FTP 認證,大多企業會建立「專用 FTP 帳戶」。
# 建立資料夾
New-Item -Path "D:\FTP\" -ItemType Directory
# 建立 FTP 專用帳號
New-LocalUser -Name "ftpuser" -Password (Read-Host -AsSecureString) -Description "FTP Account"
# 賦予 NTFS 權限
icacls "D:\FTP" /grant "ftpuser:(OI)(CI)(M)"
四、建立 FTP Site(GUI)
- 開啟 IIS Manager
- 右鍵 Sites → Add FTP Site
- 填入 Site Name:例如「FTP-Site」
- Physical Path:D:\FTP
- Binding:
- IP:伺服器 IP(建議固定)
- Port:21
- SSL:初期可選「No SSL」→ 後續再設定 TLS
- Authentication:Basic
- Authorization:指定使用者 ftpuser
五、使用 PowerShell 建立 FTP Site(自動化)
Import-Module WebAdministration
# 建立 IIS FTP Site
New-WebFtpSite -Name "FTP-Site" `
-Port 21 `
-PhysicalPath "D:\FTP" `
-Force
# 啟用 Basic 認證
Set-WebConfigurationProperty `
-Filter /system.ftpServer/security/authentication/basicAuthentication `
-Name enabled -Value true `
-PSPath 'IIS:\Sites\FTP-Site'
# 指定允許使用者
Add-WebConfiguration `
-Filter /system.ftpServer/security/authorization `
-Value @{accessType="Allow";users="ftpuser";permissions="Read,Write"} `
-PSPath 'IIS:\Sites\FTP-Site'
六、設定虛擬目錄(企業交換資料常用)
可將不同部門目錄映射到 FTP Site 底下,以「邏輯名稱」呈現。
# 建立虛擬目錄
New-WebVirtualDirectory `
-Site "FTP-Site" `
-Name "public" `
-PhysicalPath "D:\FTP\Public"
七、啟用 FTPS(SSL/TLS 加密)
1. 憑證要求方法
- 使用企業 CA(AD CS)
- 使用 Let's Encrypt(可搭配 win-acme 自動更新)
- 使用自簽憑證(測試環境)
2. 建立自簽憑證示範(PowerShell)
$cert = New-SelfSignedCertificate `
-DnsName "ftp.domain.local" `
-CertStoreLocation Cert:\LocalMachine\My
3. 將憑證套用至 FTP Site
Set-WebConfigurationProperty `
-Filter /system.ftpServer/security/ssl `
-PSPath "IIS:\Sites\FTP-Site" `
-Name serverCertHash `
-Value $cert.Thumbprint
Set-WebConfigurationProperty `
-Filter /system.ftpServer/security/ssl `
-PSPath "IIS:\Sites\FTP-Site" `
-Name controlChannelPolicy -Value "SslAllow"
Set-WebConfigurationProperty `
-Filter /system.ftpServer/security/ssl `
-PSPath "IIS:\Sites\FTP-Site" `
-Name dataChannelPolicy -Value "SslAllow"
八、防火牆設定(被動模式)
1. 啟用防火牆規則(GUI)
IIS FTP 需開啟:
- TCP 21
- 被動模式:例如 50000–51000
2. PowerShell 設定防火牆範例
# FTP 控制通道
New-NetFirewallRule -DisplayName "FTP 21" -Direction Inbound -Protocol TCP -LocalPort 21 -Action Allow
# FTP 被動通道
New-NetFirewallRule -DisplayName "FTP Passive" -Direction Inbound -Protocol TCP -LocalPort 50000-51000 -Action Allow
九、FTP 被動模式設定(IIS)
在 IIS → FTP Firewall Support 設定:
- Data Channel Port Range:50000-51000
- External IP Address:填入伺服器對外 IP(若 NAT)
十、FTP Log、監控與故障排查
查看 FTP Log
C:\inetpub\logs\LogFiles\FTPSVC2\
檢查 IIS FTP 狀態
Get-WebSiteState -Name "FTP-Site"
重新啟動 FTP Site
Restart-WebItem "IIS:\Sites\FTP-Site"
📘 結語
Windows FTP 搭配 IIS 是企業中最常見且相容性最佳的檔案交換平台。 透過基本安裝、虛擬目錄、帳號隔離、FTPS 與防火牆設定,便能快速建置安全可靠的 FTP 服務。 PowerShell 提供完整自動化能力,非常適合大量伺服器部署與跨環境同步。
🔗 延伸閱讀
- Windows Update 深度解析:USOClient、WaaS、WSUS 行為差異
- WSUS Client 強制回報(SYSTEM + COM + PsExec)
- Windows PE 完整指南(架構篇)
— WWFandy・Windows Server 部署筆記
沒有留言: