SFTP命令
一、介紹
SFTP(Secure File Transfer Protocol,安全文件傳輸協議)是一種基於可靠數據流(data stream),提供文件存取和管理的網絡傳輸協議。
與 FTP 協議相比,SFTP 在客戶端與服務器間提供了一種更為安全的文件傳輸方式,如果你還在使用 FTP 來進行文件傳輸,強烈建議切換到更為安全的 SFTP 上來。
windows下可以使用此命令於Linux進行文件交互。
二、使用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
: buffer_size,制定傳輸 buffer 的大小,更大的 buffer 會消耗更多的內存,默認為 32768 bytes;-P
: port,制定連接的端口號;-R
: num_requests,制定一次連接的請求數,可以略微提升傳輸速度,但是會增加內存的使用量。
四、目錄管理
在 SFTP 解釋器中可以使用 help
命令來查看幫助文檔。
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 [-afPpRr] remote [local] Download file
reget [-fPpRr] remote [local] Resume download file
reput [-fPpRr] [local] remote Resume upload 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 [-afPpRr] 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
SFTP 解釋器中預置了常用的命令,但是沒有自帶的 Bash 來得豐富。
1)顯示當前的工作目錄:
sftp> pwd
Remote working directory: /
2)查看當前目錄的內容:
sftp> ls
Summary.txt info.html temp.txt testDirectory
3)使用 -la
參數可以以列表形式查看,並顯示隱藏文件:
sftp> ls -la
drwxr-xr-x 5 demouser demouser 4096 Aug 13 15:11 .
drwxr-xr-x 3 root root 4096 Aug 13 15:02 ..
-rw------- 1 demouser demouser 5 Aug 13 15:04 .bash_history
-rw-r--r-- 1 demouser demouser 220 Aug 13 15:02 .bash_logout
-rw-r--r-- 1 demouser demouser 3486 Aug 13 15:02 .bashrc
drwx------ 2 demouser demouser 4096 Aug 13 15:04 .cache
-rw-r--r-- 1 demouser demouser 675 Aug 13 15:02 .profile
. . .
4)切換目錄:
sftp> cd testDirectory
5)建立文件夾:
sftp> mkdir anotherDirectory
以上的命令都是用來操作遠程服務器的,如果想要操作本地目錄呢?只需要在每個命令前添加 l
即可,例如顯示本地操作目錄下的文件:
sftp> lls
localFiles
使用 !
可以直接(本地)運行 Shell 中的指令:
sftp> !df -h
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk1s1 466Gi 360Gi 101Gi 79% 3642919 9223372036851132888 0% /
devfs 336Ki 336Ki 0Bi 100% 1162 0 100% /dev
/dev/disk1s4 466Gi 4.0Gi 101Gi 4% 5 9223372036854775802 0% /private/var/vm
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /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]