FTP 諸君大概都用過,SFTP 用過的估計比較少。簡單說,它就是 ftp 前加個 secure,通過 ssh 通道在本地及遠程伺服器間進行檔案傳輸,更為安全。
串連
ssh 的串連通常是這樣:
ssh sam@zfanw.com
然後輸入密碼。
sftp 基本就是把 ssh 換作 sftp:
sftp sam@zfanw.com
如果配置了 ssh 的 config 檔案,使用私密金鑰/公開金鑰的形式串連遠程伺服器,則更簡單了:
sftp linode
串連完成後,終端顯示:
sftp >
下載檔案
ftp 主要的作用就是檔案傳輸,sftp 提供的下載檔案命令是 get:
get -r picture
這個命令把 picture 目錄下載到本地。
get sam.png
這個命令把 sam.png 圖片下載到本地。
本地的哪裡??
我們可以藉助 lpwd 命令查看本地的當前工作目錄 – 就是在 pwd 命令前加個 l 表示 local。可以想知,我們同樣可以使用 lcd 來切換本地的工作目錄。
上傳檔案
sftp 提供的上傳命令是 put。
put sam.png
這個命令把本地當前工作目錄下的 sam.png 上傳到遠程伺服器的工作目錄下。
put -r picture
上傳整個目錄檔案。
更多命令
在 sftp > 終端輸入 help 即可以查看 sftp 提供的所有命令:
sftp> help
Available commands:
bye Quit sftp
cd path Change remote directory to 'path'
chgrp grp path Change group of file 'path' to 'grp'
chmod mode path Change permissions of file 'path' to 'mode'
chown own path Change owner of file 'path' to 'own'
df [-hi] [path] Display statistics for current directory or
filesystem containing 'path'
exit Quit sftp
get [-Ppr] remote [local] Download file
help Display this help text
lcd path Change local directory to 'path'
lls [ls-options [path]] Display local directory listing
lmkdir path Create local directory
ln [-s] oldpath newpath Link remote file (-s for symlink)
lpwd Print local working directory
ls [-1afhlnrSt] [path] Display remote directory listing
lumask umask Set local umask to 'umask'
mkdir path Create remote directory
progress Toggle display of progress meter
put [-Ppr] local [remote] Upload file
pwd Display remote working directory
quit Quit sftp
rename oldpath newpath Rename remote file
rm path Delete remote file
rmdir path Remove remote directory
symlink oldpath newpath Symlink remote file
version Show SFTP version
!command Execute 'command' in local shell
! Escape to local shell
? Synonym for help