Linux 命令列詳解:SFTP
「Linux 命令詳解」這個系列的文章將會介紹 Linux/Unix/macOS 系統中使用到的常用命令,包括該命令的簡單介紹、常用引數解釋以及最佳實踐,如果需要快速使用這個命令,直接跳到最佳實踐部分即可。
「Linux 命令詳解」系列文章:
一、介紹
SFTP(Secure File Transfer Protocol,安全檔案傳輸協議)是一種基於可靠資料流(data stream),提供檔案存取和管理的網路傳輸協議,它在網路協議層的結構如下圖所示:
與 FTP 協議相比,SFTP 在客戶端與伺服器間提供了一種更為安全的檔案傳輸方式,如果你還在使用 FTP 來進行檔案傳輸,強烈建議切換到更為安全的 SFTP 上來。
本篇文章將會介紹 SFTP 的連結,以及在互動式命令列中的一些常用命令,並對一些引數進行解釋,最後給出實際使用中的最佳實踐。目前已經有很多 GUI 客戶端支援 SFTP 協議,但是不在本篇文章的討論範圍之內。
二、使用 SFTP 進行連線
因為 SFTP 是基於 SSH 協議的,所以預設的身份認證方法與 SSH 協議保持一致。通常我們使用 SSH Key 來進行連線,如果你已經可以使用 SSH 連線到遠端伺服器上,那麼可以使用以下命令來連線 SFTP:
sftp user_name@remote_server_address[:path]
如果遠端伺服器自定義了連線的埠,可以使用 -P
引數:
sftp -P remote_port user_name@remote_server_address[:path]
連線成功後將進入一個 SFTP 的直譯器,可以發現命令列提示符變成了 sftp>
,使用 exit
命令可以退出連線。
如果連線地址存在 path
並且 path
不是一個目錄,那麼 SFTP 會直接從伺服器端取回這個檔案。
三、連線引數詳解
-B -P -R
四、目錄管理
在 SFTP 直譯器中可以使用 help
命令來檢視幫助文件。
sftp> help Available commands: byeQuit sftp cd pathChange remote directory to 'path' chgrp grp pathChange group of file 'path' to 'grp' chmod mode pathChange permissions of file 'path' to 'mode' chown own pathChange owner of file 'path' to 'own' df [-hi] [path]Display statistics for current directory or filesystem containing 'path' exitQuit sftp get [-afPpRr] remote [local]Download file reget [-fPpRr] remote [local]Resume download file reput [-fPpRr] [local] remoteResume upload file helpDisplay this help text lcd pathChange local directory to 'path' lls [ls-options [path]]Display local directory listing lmkdir pathCreate local directory ln [-s] oldpath newpathLink remote file (-s for symlink) lpwdPrint local working directory ls [-1afhlnrSt] [path]Display remote directory listing lumask umaskSet local umask to 'umask' mkdir pathCreate remote directory progressToggle display of progress meter put [-afPpRr] local [remote]Upload file pwdDisplay remote working directory quitQuit sftp rename oldpath newpathRename remote file rm pathDelete remote file rmdir pathRemove remote directory symlink oldpath newpathSymlink remote file versionShow SFTP version !commandExecute 'command' in local shell !Escape to local shell ?Synonym for help
SFTP 直譯器中預置了常用的命令,但是沒有自帶的 Bash 來得豐富。
1)顯示當前的工作目錄:
sftp> pwd Remote working directory: /
2)檢視當前目錄的內容:
sftp> ls Summary.txtinfo.htmltemp.txttestDirectory
3)使用 -la
引數可以以列表形式檢視,並顯示隱藏檔案:
sftp> ls -la drwxr-xr-x5 demouserdemouser4096 Aug 13 15:11 . drwxr-xr-x3 rootroot4096 Aug 13 15:02 .. -rw-------1 demouserdemouser5 Aug 13 15:04 .bash_history -rw-r--r--1 demouserdemouser220 Aug 13 15:02 .bash_logout -rw-r--r--1 demouserdemouser3486 Aug 13 15:02 .bashrc drwx------2 demouserdemouser4096 Aug 13 15:04 .cache -rw-r--r--1 demouserdemouser675 Aug 13 15:02 .profile . . .
4)切換目錄:
sftp> cd testDirectory
5)建立資料夾:
sftp> mkdir anotherDirectory
以上的命令都是用來操作遠端伺服器的,如果想要操作本地目錄呢?只需要在每個命令前新增 l
即可,例如顯示本地操作目錄下的檔案:
sftp> lls localFiles
使用 !
可以直接執行 Shell 中的指令:
sftp> !df -h FilesystemSizeUsedAvail Capacity iusedifree %iusedMounted on /dev/disk1s1466Gi360Gi101Gi79% 3642919 92233720368511328880%/ devfs336Ki336Ki0Bi100%11620100%/dev /dev/disk1s4466Gi4.0Gi101Gi4%5 92233720368547758020%/private/var/vm map -hosts0Bi0Bi0Bi100%00100%/net map auto_home0Bi0Bi0Bi100%00100%/home
五、傳輸檔案
5.1 從遠端伺服器拉取檔案
使用 get
命令可以從遠端伺服器拉取檔案到本地:
sftp> get remoteFile [newName]
如果不指定 newName
,將使用和遠端伺服器相同的檔名。
使用 -r
引數可以拉取整個目錄:
sftp> get -r remoteDirectory
5.2 從本地上傳檔案到伺服器
使用 put
命令可以從本地上傳檔案到伺服器:
sftp> put localFile
同樣的,可以使用 -r
引數來上傳整個目錄,但是有一點要注意, 如果伺服器上不存在這個目錄需要首先新建 :
sftp> mkdir folderName sftp> put -r folderName
六、最佳實踐
1)連線遠端伺服器
sftp remote_user@remote_host
2)使用埠進行連線
sftp -P remote_port remote_user@remote_host
3)從遠端伺服器拉取檔案
get /path/remote_file
4)上傳本地檔案到伺服器
put local_file
5)檢視遠端伺服器目錄內容
ls
6)檢視本地目錄內容
lls
7)執行本地 Shell 命令
![command]