SCP (Secure Copy) 與 SFTP (SSH File Transfer Protocol) 透過 SSH 通道傳輸檔案,雖方便但也增加資料外洩風險。在無檔案傳輸需求的環境 (如跳板機、堡壘機、高安全區),應停用這兩項功能。

| 特性 | SCP | SFTP |
|---|---|---|
| 協議 | 基於 SSH (SCP 協議) | SSH 子系統 (SFTP 協議) |
| 操作 | 單檔/目錄複製 | 互動式檔案管理 (ls, cd, get, put) |
| 斷點續傳 | 不支援 | 不支援 (需 rsync) |
| 通用性 | 極高 | 極高 |
移除 OpenSSH 客戶端套件 (含 scp, ssh, ssh-keygen 等):
# CentOS/RHEL
rpm -qa | grep openssh
yum remove openssh-clients -y
# 驗證
scp
# -bash: scp: command not found
副作用: ssh 指令同被移除,無法發起 SSH 連線。若需保留 SSH 登入功能,請改用下方 SFTP 停用法,並以 ForceCommand internal-sftp 限制用戶僅能 SFTP (但此處目標為完全停用)。
vim /etc/ssh/sshd_config
# 找到並註解:
# Subsystem sftp /usr/libexec/openssh/sftp-server
# 或改為內建實作 (仍啟用 SFTP):
# Subsystem sftp internal-sftp
# 重啟 SSH 服務
service sshd restart
# 或 systemctl restart sshd
# sshd_config 末尾加入
Match Group sftponly
ForceCommand internal-sftp
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
此設定讓 sftponly 群組用戶只能 SFTP 且被限制在家目錄,無法 SSH 登入 Shell。
sftp user@host → 應提示 subsystem request failed 或連線失敗。scp file user@host: → command not found (若移除 openssh-clients) 或 permission denied。若不想動系統套件,可在防火牆封鎖 SSH 埠 (22) 的出向/入向連線,但會同時阻擋正常 SSH 管理。
根據安全基線需求,選擇移除客戶端套件 (停用 SCP/SSH) 或停用 SFTP 子系統 (保留 SSH Shell),實現最小權限原則。