日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

詳解SFTP命令_linux shell

作者:哈士奇說(shuō)騙你是小狗 ? 更新時(shí)間: 2022-11-18 編程語(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

欄目分類
最近更新