如何使用 SFTP 安全地與遠端伺服器進行檔案傳輸


在本文中,您可以瞭解如何設定 SFTP(安全檔案傳輸協議),這將有助於我們將檔案從本地計算機安全地傳輸到遠端伺服器。FTP(檔案傳輸協議)是一種非常流行的方法,用於將檔案從一臺機器傳輸到另一臺機器或從遠端伺服器傳輸。

SFTP 代表安全檔案傳輸協議,是一種獨立的協議,它使用 SSH 來保護連線並進行檔案傳輸,這些檔案傳輸遍歷遠端伺服器和本地計算機上的檔案系統。

如何使用 SFTP 連線

SFTP 使用 SSH 協議進行連線並建立安全連線以進行身份驗證。儘管密碼非常易於使用並設定為預設值,但我們建議建立 SSH 金鑰並將公鑰複製到任何需要訪問的系統。這不僅可以保護連線,還可以幫助節省長期執行的時間。

在進一步使用 SFTP 之前,我們需要在不使用任何密碼的情況下設定對遠端機器的 ssh 訪問。

建立 SSH 金鑰

# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
71:de:c6:b4:c4:8b:31:70:5f:bd:96:60:56:8e:74:b9 root@centos
The key's randomart image is:
+--[ RSA 2048]----+
|         . . .o+.|
|         o o.=+..|
|        . + B...+|
|         + O o E |
|         S o * . |
|               . |
|                 |
|                 |
|                 |
+-----------------+
For security the key itself is a protected using a strong passphrase, if a passphrase is used to protect their key,
the SSH-agent can be used to cache the passphrase.

現在我們需要將 ssh 金鑰複製到遠端主機

# ssh-copy-id root@192.168.1.89
root@192.168.1.89's password:
Now try logging into the machine, with "ssh 'root@192.168.1.89'", and check in:
.ssh/authorized_keys
To make sure we haven't added extra keys that you weren't expecting.

要測試連線,請使用 ssh 登入到遠端伺服器

# ssh root@192.168.1.89
Last login: Fri Apr 15 15:18:42 2016 from 192.168.1.125

然後退出伺服器並使用 SFTP 連線到伺服器

# sftp root@192.168.1.89
Connecting to 192.168.1.89...
sftp>

然後提示符從 # 更改為 sftp>

瞭解環境的簡單命令

瞭解遠端伺服器上的當前工作目錄

sftp>pwd
Remote working directory: /root

列出遠端伺服器上的檔案和資料夾

sftp>ls
Desktop               Documents
Downloads             Music
Pictures              Public
Templates             Videos

列印本地計算機的工作目錄

sftp> lpwd
本地工作目錄:/root

列出本地計算機檔案

將遠端檔案傳輸到本地計算機

語法

sftp> get <remote file >

用法

sftp> get pdfflyer.sql
Fetching /root/pdfflyer.sql to pdfflyer.sql
/root/pdfflyer.sql 100% 1765KB 1.7MB/s 00:01

這會將遠端檔案複製到本地計算機的當前工作目錄中

sftp> get <remotefile> <localfile>

這會將遠端檔案複製到當前工作目錄,並將其重新命名為我們在命令中指定的不同名稱 <本地檔案>

sftp>get -r <Directory>

要遞迴地複製 <目錄> 中的所有檔案和資料夾,我們可以使用 ' -r ' 選項

將本地檔案傳輸到遠端計算機

General Syntax:
sftp> put <localfile>
Usage:
sftp> put svn_backup.sh
Uploading svn_backup.sh to /root/svn_backup.sh
svn_backup.sh 100% 489 0.5KB/s 00:00
sftp>

要遞迴地將本地檔案複製到遠端伺服器,我們可以使用 ' -r ' 選項。

sftp> put -r <Local directory>

SFTP 的簡單檔案操作

SFTP 允許您執行在處理檔案系統時有用的所有型別的基本檔案維護。我們可以從遠端計算機獲取檔案並在本地計算機中檢視檔案,只需在命令前面新增 ! 。

sftp> get /etc/passwd
sftp> !less passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
......

要列出本地組,我們可以透過在開頭新增 ! 來執行命令。

sftp>!less /etc/group
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon
mem:x:8:
kmem:x:9:
wheel:x:10:
....

沒有用於操作本地檔案許可權的命令,但您可以設定本地 umask,以便複製到本地系統的任何檔案都具有適當的許可權。

這可以透過 “lumask” 命令完成

sftp>lumask 022
Local umask: 022

SFTP 允許您使用 “lmkdir” 和 “mkdir” 在遠端和本地計算機上建立目錄。

sftp> bye

要退出 SFTP 會話,請使用 “exit” 或 “bye” 關閉連線。

透過以上操作,我們瞭解瞭如何使用 SFTP 將檔案從本地計算機傳輸到遠端計算機,SFTP 是一款簡單但功能強大的工具,用於在遠端伺服器和本地計算機之間傳輸檔案。

更新於: 2020年1月20日

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.