Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 檔案傳輸三大神器:sftp、scp、sz/rz 完整指令速查與實戰場景

編輯:Linux 教學

對比概覽

工具協定/機制加密功能範圍適用場景
sftpSSH 子系統✅ 全程加密互動式檔案管理 (ls, cd, mkdir, chmod...)需遠端目錄操作、權限管理、斷點續傳
scpSSH 通道✅ 全程加密單純檔案複製 (無互動管理)腳本自動化備份、單檔/目錄快速推拉
sz/rzZModem (需終端機支援)❌ 明文 (但有 CRC 校驗)終端機直接上傳/下載 (Xshell, SecureCRT, Termius)跳板機/堡壘機環境、圖形化介面不便時

sftp 實戰指令

# 連線
sftp user@host
# 或指定埠口/金鑰
sftp -P 2222 -i ~/.ssh/id_rsa user@host

# 常用內部指令
put local_file [remote_path]      # 上傳
get remote_file [local_path]      # 下載
mput *.log                        # 批量上傳
mget *.tar.gz                     # 批量下載
lcd /local/path                   # 切換本地目錄 (l 前綴 = local)
lls, lpwd, lmkdir                 # 本地操作
cd /remote/path                   # 切換遠端目錄
ls, mkdir, rm, chmod, rename      # 遠端檔案管理
bye / exit                        # 離開

scp 實戰指令

# 本地 → 遠端
scp /path/local_file user@host:/remote/path/
scp -r /path/local_dir user@host:/remote/path/   # 遞歸目錄
scp -P 2222 -i ~/.ssh/id_rsa file user@host:/path

# 遠端 → 本地
scp user@host:/remote/file /local/path/
scp -r user@host:/remote/dir /local/path/

# 遠端 → 遠端 (經本地中轉)
scp user1@host1:/file user2@host2:/path/

# 限制頻寬 (避免佔滿連線)
scp -l 8192 file user@host:/path/   # 限制 8 Mbps

sz/rz 實戰指令 (需終端機支援 ZModem)

# 伺服器端安裝 (CentOS)
yum install -y lrzsz

# 下載 (Server → Client)
sz filename1 filename2 ...          # 可多檔
sz /var/log/*.log                   # 萬用字元

# 上傳 (Client → Server)
rz -be                              # -b 二進位, -e 強制覆蓋
# 終端機會彈出檔案選擇視窗

安全與效能建議

  • 大量小檔案:先 tar -czf - dir/ | ssh user@host "cat > backup.tar.gz" 打包管道傳輸,避免建立大量 SSH 連線開銷。
  • 斷點續傳:rsync -avzP --partial 優於 scp/sftp。
  • 跳板機/堡壘機:sz/rz 不需額外開啟連線埠,極適合受限環境;但生產環境建議改用 scp -J jump_host (ProxyJump) 保持端到端加密。
  • 金鑰認證:停用密碼登入,強制 PubkeyAuthentication yes,配合 ssh-agent 管理金鑰。
Copyright © Windows教程網 All Rights Reserved