FTP (File Transfer Protocol ) 是很常用的檔案傳輸通訊協定, 但由於 FTP 沒有經過加密, 連接遠端主機會有安全性問題, 所以 FTP 現在常用於沒有敏感資料的檔案傳輸。
SFTP (Secure File Transfer Protocol) 是在 SSH 通訊協定上執行, 實現在安全的通道上傳輸檔案, 對系統管理十分有用, 而且很多 FTP 軟體把 SFTP 整合進去, 例如 FileZilla, WinSCP, FireFTP 等。
連接 SFTP
要建立 SFTP 連接, 執行以下指令, sftp 後面的是 username 及 hostname:
-
$ sftp ltsplus@192.168.1.100
Connecting to 192.168.1.100…
ltsplus@27.48.137.6’s password:
sftp>
如上面所見, 輸入密碼後, 便會看到 sftp> 的提示。
sftp 的使用跟 Linux 其他指令差不多, 可以在 sftp> 下輸入問題 “?” 或 “help”, 那便會顯示使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
sftp Available commands: cd path Change remote directory to 'path' lcd path Change local 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' help Display this help text get remote-path [local-path] Download file lls [ls-options [path]] Display local directory listing ln oldpath newpath Symlink remote file lmkdir path Create local directory lpwd Print local working directory ls [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory put local-path [remote-path] Upload file pwd Display remote working directory exit Quit sftp quit Quit sftp rename oldpath newpath Rename remote file rmdir path Remove remote directory rm path Delete remote file symlink oldpath newpath Symlink remote file version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell ? Synonym for help |
上傳檔案
上傳檔案用 put 指令:
- sftp> put filename.txt
上傳多個檔案
上傳多個檔案用 mput 指令:
- sftp> mput *.jpg
下載檔案
下載檔案用 get 指令:
- sftp> get remote-file.txt
下載多個檔案
下載多個檔案用 mput 指令:
- sftp> mget *.xls
切換目錄
切換遠端目錄:
- sftp> cd dir-name
切換本機目錄:
- sftp> lcd Documents
建立目錄
建立遠端目錄:
- sftp> mkdir dir-name
建立本機目錄:
- sftp> lmkdir local-dir
剛除檔案及目錄
刪除遠端檔案:
- sftp> rm filename
刪除遠端目錄:
- sftp> rmdir dir-name
p.s. 要用 rmdir 刪除遠端目錄, 那個目錄必須是空白, 即沒有任何內容。
離開 SFTP
輸入 “!” 或 “exit” 離開 SFTP