網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
SFTP命令
一、介紹
SFTP(Secure File Transfer Protocol,安全文件傳輸協(xié)議)是一種基于可靠數(shù)據(jù)流(data stream),提供文件存取和管理的網(wǎng)絡(luò)傳輸協(xié)議。
與 FTP 協(xié)議相比,SFTP 在客戶端與服務(wù)器間提供了一種更為安全的文件傳輸方式,如果你還在使用 FTP 來(lái)進(jìn)行文件傳輸,強(qiáng)烈建議切換到更為安全的 SFTP 上來(lái)。
windows下可以使用此命令于Linux進(jìn)行文件交互。
二、使用SFTP進(jìn)行連接
因?yàn)?SFTP 是基于 SSH 協(xié)議的,所以默認(rèn)的身份認(rèn)證方法與 SSH 協(xié)議保持一致。通常我們使用 SSH Key 來(lái)進(jìn)行連接,如果你已經(jīng)可以使用 SSH 連接到遠(yuǎn)程服務(wù)器上,那么可以使用以下命令來(lái)連接 SFTP:
sftp user_name@remote_server_address[:path]
如果遠(yuǎn)程服務(wù)器自定義了連接的端口,可以使用 -P
參數(shù):
sftp -P remote_port user_name@remote_server_address[:path]
連接成功后將進(jìn)入一個(gè) SFTP 的解釋器,可以發(fā)現(xiàn)命令行提示符變成了 sftp>
,使用 exit
命令可以退出連接。
如果連接地址存在 path
并且 path
不是一個(gè)目錄,那么 SFTP 會(huì)直接從服務(wù)器端取回這個(gè)文件。
三、連接參數(shù)詳解
-
-B
: buffer_size,制定傳輸 buffer 的大小,更大的 buffer 會(huì)消耗更多的內(nèi)存,默認(rèn)為 32768 bytes; -
-P
: port,制定連接的端口號(hào); -
-R
: num_requests,制定一次連接的請(qǐng)求數(shù),可以略微提升傳輸速度,但是會(huì)增加內(nèi)存的使用量。
四、目錄管理
在 SFTP 解釋器中可以使用 help
命令來(lái)查看幫助文檔。
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 解釋器中預(yù)置了常用的命令,但是沒(méi)有自帶的 Bash 來(lái)得豐富。
1)顯示當(dāng)前的工作目錄:
sftp> pwd Remote working directory: /
2)查看當(dāng)前目錄的內(nèi)容:
sftp> ls Summary.txt info.html temp.txt testDirectory
3)使用 -la
參數(shù)可以以列表形式查看,并顯示隱藏文件:
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ái)操作遠(yuǎn)程服務(wù)器的,如果想要操作本地目錄呢?只需要在每個(gè)命令前添加 l
即可,例如顯示本地操作目錄下的文件:
sftp> lls localFiles
使用 !
可以直接(本地)運(yùn)行 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 從遠(yuǎn)程服務(wù)器拉取文件
使用 get
命令可以從遠(yuǎn)程服務(wù)器拉取文件到本地:
sftp> get remoteFile [newName]
如果不指定 newName
,將使用和遠(yuǎn)程服務(wù)器相同的文件名。
使用 -r
參數(shù)可以拉取整個(gè)目錄:
sftp> get -r remoteDirectory
5.2 從本地上傳文件到服務(wù)器
使用 put
命令可以從本地上傳文件到服務(wù)器:
sftp> put localFile
同樣的,可以使用 -r
參數(shù)來(lái)上傳整個(gè)目錄,但是有一點(diǎn)要注意,如果服務(wù)器上不存在這個(gè)目錄需要首先新建:
sftp> mkdir folderName sftp> put -r folderName
六、最佳實(shí)踐
1)連接遠(yuǎn)程服務(wù)器
sftp remote_user@remote_host
2)使用端口進(jìn)行連接
sftp -P remote_port remote_user@remote_host
3)從遠(yuǎn)程服務(wù)器拉取文件
get /path/remote_file
4)上傳本地文件到服務(wù)器
put local_file
5)查看遠(yuǎn)程服務(wù)器目錄內(nèi)容
ls
6)查看本地目錄內(nèi)容
lls
7)執(zhí)行本地 Shell 命令
![command]
原文鏈接:https://www.cnblogs.com/dogha/p/16081635.html
相關(guān)推薦
- 2022-06-11 python?針對(duì)在子文件夾中的md文檔實(shí)現(xiàn)批量md轉(zhuǎn)word_python
- 2022-07-03 Python基礎(chǔ)教程之錯(cuò)誤和異常的處理方法_python
- 2023-01-29 React基于路由的代碼分割技術(shù)詳解_React
- 2022-08-25 C語(yǔ)言詳細(xì)分析結(jié)構(gòu)體的內(nèi)存對(duì)齊規(guī)則_C 語(yǔ)言
- 2022-06-12 Python語(yǔ)法學(xué)習(xí)之線程的創(chuàng)建與常用方法詳解_python
- 2022-09-25 自動(dòng)微分----pytorch中的梯度運(yùn)算與反向傳播函數(shù)(預(yù)備知識(shí))
- 2022-06-20 正則表達(dá)式之字符串模式匹配實(shí)例詳解_正則表達(dá)式
- 2022-08-18 C#實(shí)現(xiàn)從位圖到布隆過(guò)濾器的方法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支