SCP 與 SFTP
- UtilsSCPSFTPUbuntu作者:VincentCorgi約 1 分鐘閱讀
功能
示範 SCP 在 本機與遠端 間複製 檔案/目錄(含自訂 port),以及用互動式 SFTP 瀏覽與傳檔的指令。
情境
- 沒有圖形化 FTP、但有 SSH 時,快速上傳設定檔、拉 log、或批次同步目錄;比純
ssh貼內容更適合二進位與大檔。
SCP
Code
# 本機 → 遠端(檔案)
scp -P<port> -v <local_path>/<file> <user>@<host>:<remote_path>/
# 本機 → 遠端(資料夾)
scp -P<port> -v -r <local_path>/<folder> <user>@<host>:<remote_path>/
# 遠端 → 本機(檔案)
scp -P<port> -v <user>@<host>:<remote_path>/<file> <local_path>/
# 遠端 → 本機(資料夾)
scp -P<port> -v -r <user>@<host>:<remote_path> <local_path>/<folder>/
chmod +x <remote_path>/<file>
SFTP
Flow
Code
sudo groupadd sftpusers
sudo useradd -g sftpusers -d /home/<sftp_user> -s /sbin/nologin <sftp_user>
sudo passwd <sftp_user>
配置(/etc/ssh/sshd_config 或獨立檔)
- Port 2022、Protocol 2、HostKey
- PermitRootLogin no、PubkeyAuthentication yes、PasswordAuthentication no
- Subsystem sftp internal-sftp
- Match Group sftpusers:ChrootDirectory
chroot_path/%u、ForceCommand internal-sftp
安裝與群組
Code
sudo apt update && sudo apt install openssh-server
sudo groupadd sftpusers
sudo groupadd sftpadmins