mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-10 22:21:11 +08:00
commit
c22e0db431
@ -1,121 +0,0 @@
|
||||
translating---geekpi
|
||||
|
||||
How to Upload or Download Files/Directories Using sFTP in Linux
|
||||
============================================================
|
||||
|
||||
Download Your Free eBooks NOW - [10 Free Linux eBooks for Administrators][10] | [4 Free Shell Scripting eBooks][11]
|
||||
|
||||
[sFTP (secure File Transfer Program)][1] is a secure and interactive file transfer program, which works in a similar way as FTP (File Transfer Protocol). However, sFTP is more secure than FTP; it handles all operations over an encrypted SSH transport.
|
||||
|
||||
It can be configured to use several useful SSH features, such as [public key authentication][2] and compression. It connects and logs into the specified remote machine, and switches to an interactive command mode where user can execute various commands.
|
||||
|
||||
In this article, we will show you how to upload/download a whole directory (including it’s subdirectories and subfiles) using sFTP.
|
||||
|
||||
### How to Use sFTP to Transfer Files/Directories in Linux
|
||||
|
||||
By default, SFTP adopts the same SSH transport for establishing a secure connection to a remote server. Although, passwords are used to authenticate users similar to the default SSH settings, but, it is recommended to [create and use SSH passwordless login][3] for simplified and more secure connection to remote hosts.
|
||||
|
||||
#### Connect to Remote FTP Server Securely
|
||||
|
||||
To connect to a remote sftp server, first establish an secure SSH connection and then create an SFTP session as shown.
|
||||
|
||||
```
|
||||
$ sftp tecmint@192.168.56.10
|
||||
```
|
||||
|
||||
Once you have logged into the remote host, you can run interactive sFTP commands as in the examples below:
|
||||
|
||||
```
|
||||
sftp> ls #list directory
|
||||
sftp> pwd #print working directory on remote host
|
||||
sftp> lpwd #print working directory on local host
|
||||
sftp> mkdir uploads #create a new directory
|
||||
```
|
||||
[
|
||||
![Run sFTP Commands on Remote Linux](http://www.tecmint.com/wp-content/uploads/2017/02/Run-sFTP-Commands-on-Remote-Linux.png)
|
||||
][4]
|
||||
|
||||
Run sFTP Commands on Remote Linux
|
||||
|
||||
#### How to Upload a Directory Using sFTP
|
||||
|
||||
In order to upload a whole directory to a remote Linux host, use the put command. However, you will get an error if the directory name doesn’t exist in the working directory on the remote host as shown in the screenshot below.
|
||||
|
||||
Therefore, first create a directory with the same name on the remote host, before uploading it from the local host, the `-r` does the magic, enabling subdirectories and subfile to be copied as well:
|
||||
|
||||
```
|
||||
sftp> put -r Tecmint.com-articles
|
||||
sftp> mkdir Tecmint.com-articles
|
||||
sftp> put -r Tecmint.com-articles
|
||||
```
|
||||
[
|
||||
![Upload Directory using SFTP](http://www.tecmint.com/wp-content/uploads/2017/02/Upload-Directory-using-SFTP.png)
|
||||
][5]
|
||||
|
||||
Upload Directory using SFTP
|
||||
|
||||
To preserve the modification times, access times, and modes from the original files transferred, use the `-p` flag.
|
||||
|
||||
```
|
||||
sftp> put -pr Tecmint.com-articles
|
||||
```
|
||||
|
||||
#### How to Download a Directory Using sFTP
|
||||
|
||||
To download a whole directory called fstools-0.0 from remote Linux host to local machine, use the getcommand with the `-r` flag as follows:
|
||||
|
||||
```
|
||||
sftp> get -r fstools-0.0
|
||||
```
|
||||
[
|
||||
![Download Directory using sFTP](http://www.tecmint.com/wp-content/uploads/2017/02/Download-Directory-using-sFTP.png)
|
||||
][6]
|
||||
|
||||
Download Directory using sFTP
|
||||
|
||||
Then check in the current working directory on the local host, if the directory was downloaded with all the contents in it.
|
||||
|
||||
To quite the sFTP shell, type:
|
||||
|
||||
```
|
||||
sftp> bye
|
||||
OR
|
||||
sftp> exit
|
||||
```
|
||||
|
||||
Additionally, read through the [sFTP commands and usage tips][7].
|
||||
|
||||
Note that to prevent users from accessing the whole file system on the remote host, for security reasons, you can [restrict sFTP users to their home directories][8] using chroot Jail.
|
||||
|
||||
That’s it! In this article, we’ve showed you how to upload/download a whole directory using sFTP. Use the comment section below to offer us your thoughts about this article/topic.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
||||
作者简介:
|
||||
|
||||
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/sftp-upload-download-directory-in-linux/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
[1]:http://www.tecmint.com/sftp-command-examples/
|
||||
[2]:http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
|
||||
[3]:http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
|
||||
[4]:http://www.tecmint.com/wp-content/uploads/2017/02/Run-sFTP-Commands-on-Remote-Linux.png
|
||||
[5]:http://www.tecmint.com/wp-content/uploads/2017/02/Upload-Directory-using-SFTP.png
|
||||
[6]:http://www.tecmint.com/wp-content/uploads/2017/02/Download-Directory-using-sFTP.png
|
||||
[7]:http://www.tecmint.com/sftp-command-examples/
|
||||
[8]:http://www.tecmint.com/restrict-sftp-user-home-directories-using-chroot/
|
||||
[9]:http://www.tecmint.com/author/aaronkili/
|
||||
[10]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
|
||||
[11]:http://www.tecmint.com/free-linux-shell-scripting-books/
|
@ -0,0 +1,118 @@
|
||||
如何在 Linux 中使用 sFTP 上传或下载文件/文件夹
|
||||
============================================================
|
||||
|
||||
现在下载你的免费电子书 - [10 本对 Linux 管理员的免费电子书][10] | [4 本免费 Shell 脚本电子书][11]
|
||||
|
||||
[sFTP(安全文件传输程序)][1]是一种安全和交互式文件传输程序,其工作方式与 FTP(文件传输协议)类似。 然而,sFTP 比 FTP 更安全; 它通过加密 SSH 传输处理所有操作。
|
||||
|
||||
它可以配置为使用几个有用的 SSH 功能,如[公钥认证][2]和压缩。 它连接并登录到指定的远程机器,并切换到交互式命令模式,在该模式下用户可以执行各种命令。
|
||||
|
||||
在本文中,我们将向你展示如何使用 sFTP 上传/下载整个目录(包括其子目录和子文件)。
|
||||
|
||||
### 如何在 Linux 中使用 sFTP 传输文件/文件夹
|
||||
|
||||
默认情况下,SFTP 协议采用相同的 SSH 传输协议建立到远程服务器的安全连接。虽然,密码用于验证类似于默认 SSH 设置的用户,但是,建议[创建和使用 SSH 无密码登录][3],以简化和更安全地连接到远程主机。
|
||||
|
||||
|
||||
要连接到远程 sftp 服务器,首先建议一个安全 SSH 连接,接着创建一个如下所示 SFTP 会话。
|
||||
|
||||
```
|
||||
$ sftp tecmint@192.168.56.10
|
||||
```
|
||||
|
||||
登录到远程主机后,你可以如下运行交互式的 sFTP 命令:
|
||||
|
||||
```
|
||||
sftp> ls #list directory
|
||||
sftp> pwd #print working directory on remote host
|
||||
sftp> lpwd #print working directory on local host
|
||||
sftp> mkdir uploads #create a new directory
|
||||
```
|
||||
[
|
||||
![Run sFTP Commands on Remote Linux](http://www.tecmint.com/wp-content/uploads/2017/02/Run-sFTP-Commands-on-Remote-Linux.png)
|
||||
][4]
|
||||
|
||||
Linux 主机上运行 sFTP 命令
|
||||
|
||||
#### 如何使用 sFTP 上传文件夹
|
||||
|
||||
要将整个目录上传到远程 Linux 主机中,请使用 put 命令。但是,如果目录名称不存在于远程主机上的工作目录中,你将收到错误,如下面的屏幕截图所示。
|
||||
|
||||
因此,首先在远程主机上创建一个具有相同名称的目录,然后从本地主机上传它,`-r` 参数允许拷贝子目录和子文件:
|
||||
|
||||
```
|
||||
sftp> put -r Tecmint.com-articles
|
||||
sftp> mkdir Tecmint.com-articles
|
||||
sftp> put -r Tecmint.com-articles
|
||||
```
|
||||
[
|
||||
![Upload Directory using SFTP](http://www.tecmint.com/wp-content/uploads/2017/02/Upload-Directory-using-SFTP.png)
|
||||
][5]
|
||||
|
||||
使用 SFTP 上传文件夹
|
||||
|
||||
要保留修改时间、访问时间以及被传输的文件的模式,请使用 `-p` 标志。
|
||||
|
||||
```
|
||||
sftp> put -pr Tecmint.com-articles
|
||||
```
|
||||
|
||||
#### 如何使用 sFTP 下载文件夹
|
||||
|
||||
要从远程 Linux 主机下载整个 fstools-0.0 文件夹到本机中,如下所示使用 get 命令带上 `-r` 标志:
|
||||
|
||||
```
|
||||
sftp> get -r fstools-0.0
|
||||
```
|
||||
[
|
||||
![Download Directory using sFTP](http://www.tecmint.com/wp-content/uploads/2017/02/Download-Directory-using-sFTP.png)
|
||||
][6]
|
||||
|
||||
使用 sFTP 下载目录
|
||||
|
||||
如果文件夹已经下载完成了,接着查看本机的工作目录。
|
||||
|
||||
要退出 sFTP shell,输入:
|
||||
|
||||
```
|
||||
sftp> bye
|
||||
或者
|
||||
sftp> exit
|
||||
```
|
||||
|
||||
此外,阅读这篇[ sFTP 命令和使用技巧][7]。
|
||||
|
||||
请注意,为了防止用户访问远程主机上的整个文件系统,出于安全原因,你可以使用 chroot Jail [将 sFTP 用户限制到其主目录][8]中。
|
||||
|
||||
就是这样了!在本文中,我们向你展示了如何使用 sFTP 上传/下载整个目录。使用下面的评论栏向我们提供你对本文/主题的看法。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
||||
作者简介:
|
||||
|
||||
Aaron Kili 是 Linux 和 F.O.S.S 爱好者,将来的 Linux 系统管理员和网络开发人员,目前是 TecMint 的内容创作者,他喜欢用电脑工作,并坚信分享知识。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/sftp-upload-download-directory-in-linux/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
[1]:http://www.tecmint.com/sftp-command-examples/
|
||||
[2]:http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
|
||||
[3]:http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
|
||||
[4]:http://www.tecmint.com/wp-content/uploads/2017/02/Run-sFTP-Commands-on-Remote-Linux.png
|
||||
[5]:http://www.tecmint.com/wp-content/uploads/2017/02/Upload-Directory-using-SFTP.png
|
||||
[6]:http://www.tecmint.com/wp-content/uploads/2017/02/Download-Directory-using-sFTP.png
|
||||
[7]:http://www.tecmint.com/sftp-command-examples/
|
||||
[8]:http://www.tecmint.com/restrict-sftp-user-home-directories-using-chroot/
|
||||
[9]:http://www.tecmint.com/author/aaronkili/
|
||||
[10]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
|
||||
[11]:http://www.tecmint.com/free-linux-shell-scripting-books/
|
Loading…
Reference in New Issue
Block a user