文档章节

sftp get or put file

Oscarfff
 Oscarfff
发布于 2016/07/04 18:39
字数 2252
阅读 49
收藏 0
sfp

sftp用法http://blog.sciencenet.cn/blog-803390-632223.html

1. 用sftp如何登录服务器 

     sftp 是一个交互式文件传输程式。它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性。下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作。

举例,如远程主机的 IP 是 192.168.0.1,用户名是kyu,在命令行模式下:

   sftp kyu@192.168.0.1

   默认端口号为22,如果是其他端口号,例如22806,则使用命令:

   sftp -oPort=22806 kyu@192.168.0.1

   回车提示输入密码。

   进入提示符

   sftp>

   如果登陆远程机器不是为了上传下载文件,而是要修改远程主机上的某些文件。可以

   ssh kyu@192.168.0.1  -p 22806(其实sftp就是ssh 的一个程式,-p指定端口号)

2. sftp从服务器上下载文件:

   sftp> get /var/www/kyu/index.php  /home/kyu/
   这条语句将从远程主机的  /var/www/kyu/目录下将 index.php 下载到本地  /home/kyu/目录下。

3. sftp将本地文件上传到服务器

   sftp> put /home/fuyatao/downloads/Linuxgl.pdf /var/www/fuyatao/
   这条语句将把本地 /home/fuyatao/downloads/目录下的 linuxgl.pdf文件上传至远程主机/var/www/fuyatao/ 目录下。

4. 查看登录时的服务器和本机的当前目录:

   你如果不知道远程主机的目录是什么样, pwd命令可以帮您查询远程主机的当前路径。查询本机当前工作目录 lpwd.

5. 其他命令:改变路径可以用cd ,改变本机路径可以用 lcd;

  ls rm rmdir mkdir 这些命令都可以使用。同理调用本机都是加 l , 即 lls lrm.

  要离开sftp,用exit 或quit、 bye 均可。详细情况可以查阅 man  sftp.

   如果觉得在命令行模式下不太方便,可以 sudo apt-get install gftp。在图形界面下操作就简便多了。

What is SFTP?

FTP, or "File Transfer Protocol" is a popular method of transferring files between two remote systems.

SFTP, which stands for SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate protocol packaged with SSH that works in a similar way over a secure connection. The advantage is the ability to leverage a secure connection to transfer files and traverse the filesystem on both the local and remote system.

In almost all cases, SFTP is preferable to FTP because of its underlying security features and ability to piggy-back on an SSH connection. FTP is an insecure protocol that should only be used in limited cases or on networks you trust.

Although SFTP is integrated into many graphical tools, this guide will demonstrate how to use it through its interactive command line interface.

How to Connect with SFTP

By default, SFTP uses the SSH protocol to authenticate and establish a secure connection. Because of this, the same authentication methods are available that are present in SSH.

Although passwords are easy to use and set up by default, we recommend you create SSH keys and transfer your public key to any system that you need to access. This is much more secure and can save you time in the long run.

Please see this guide to set up SSH keys in order to access your server if you have not done so already.

If you can connect to the machine using SSH, then you have completed all of the necessary requirements necessary to use SFTP to manage files. Test SSH access with the following command:

ssh username@remote_hostname_or_IP

If that works, exit back out by typing:

exit

We can establish an SSH connection and then open up an SFTP session using that connection by issuing the following command:

sftp username@remote_hostname_or_IP

You will connect the the remote system and your prompt will change to an SFTP prompt.

Getting Help in SFTP

The most useful command to learn first is the help command. This gives you access to a summary of the SFTP help. You can call it by typing either of these in the prompt:

help
?

This will display a list of the available commands:

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 [-Ppr] remote [local]          Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
. . .

We will explore some of the commands you see in the following sections.

Navigating with SFTP

We can navigate through the remote system's file hierarchy using a number of commands that function similarly to their shell counterparts.

First, let's orient ourselves by finding out which directory we are in currently on the remote system. Just like in a typical shell session, we can type the following to get the current directory:

pwd
Remote working directory: /home/demouser

We can view the contents of the current directory of the remote system with another familiar command:

ls
Summary.txt     info.html       temp.txt        testDirectory

Note that the commands within the SFTP interface are not the normal shell commands and are not as feature-rich, but they do implement some of the more important optional flags:

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
. . .

To get to another directory, we can issue this command:

cd testDirectory

We can now traverse the remote file system, but what if we need to access our local file system? We can direct commands towards the local file system by preceding them with an "l" for local.

All of the commands discussed so far have local equivalents. We can print the local working directory:

lpwd
Local working directory: /Users/demouser

We can list the contents of the current directory on the local machine:

lls
Desktop			local.txt		test.html
Documents		analysis.rtf		zebra.html

We can also change the directory we wish to interact with on the local system:

lcd Desktop

Transferring Files with SFTP

Navigating the remote and local filesystems is of limited usefulness without being able to transfer files between the two.

Transferring Remote Files to the Local System

If we would like download files from our remote host, we can do so by issuing the following command:

get remoteFile
Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile                       100%   37KB  36.8KB/s   00:01

As you can see, by default, the "get" command downloads a remote file to a file with the same name on the local file system.

We can copy the remote file to a different name by specifying the name afterwards:

get remoteFile localFile

The "get" command also takes some option flags. For instance, we can copy a directory and all of its contents by specifying the recursive option:

get -r someDirectory

We can tell SFTP to maintain the appropriate permissions and access times by using the "-P" or "-p" flag:

get -Pr someDirectory

Transferring Local Files to the Remote System

Transferring files to the remote system is just as easily accomplished by using the appropriately named "put" command:

put localFile
Uploading localFile to /home/demouser/localFile
localFile                                     100% 7607     7.4KB/s   00:00

The same flags that work with "get" apply to "put". So to copy an entire local directory, you can issue:

put -r localDirectory

Note

There is currently a bug in the versions of OpenSSH shipped with current Ubuntu releases (at least 14.04 to 15.10) that prevents the above command from operating correctly. Upon issuing the command above to transfer content to a server using the buggy version of OpenSSH, the following error will be given: Couldn't canonicalise: No such file or directory.

To work around this issue, create the destination directory on the remote end first by typing mkdir localDirectory. Afterwards, the above command should complete without error.

One familiar tool that is useful when downloading and uploading files is the "df" command, which works similar to the command line version. Using this, you can check that you have enough space to complete the transfers you are interested in:

df -h
Size     Used    Avail   (root)    %Capacity
  19.9GB   1016MB   17.9GB   18.9GB           4%

Please note, that there is no local variation of this command, but we can get around that by issuing the "!" command.

The "!" command drops us into a local shell, where we can run any command available on our local system. We can check disk usage by typing:

!
df -h
Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   595Gi   52Gi  544Gi     9%    /
devfs          181Ki  181Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home

Any other local command will work as expected. To return to your SFTP session, type:

exit

You should now see the SFTP prompt return.

Simple File Manipulations with SFTP

SFTP allows you to perform the type of basic file maintenance that is useful when working with file hierarchies.

For instance, you can change the owner of a file on the remote system with:

chown userID file

Notice how, unlike the system "chmod" command, the SFTP command does not accept usernames, but instead uses UIDs. Unfortunately, there is no easy way to know the appropriate UID from within the SFTP interface.

An involved work around could be accomplished with:

get /etc/passwd
!less passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .

Notice how instead of giving the "!" command by itself, we've used used it as a prefix for a local shell command. This works to run any command available on our local machine and could have been used with the local "df" command earlier.

The UID will be in the third column of the file, as delineated by colon characters.

Similarly, we can change the group owner of a file with:

chgrp groupID file

Again, there is no easy way to get a listing of the remote system's groups. We can work around it with the following command:

get /etc/group
!less group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
. . .

The third column holds the ID of the group associated with name in the first column. This is what we are looking for.

Thankfully, the "chmod" command works as expected on the remote file system:

chmod 777 publicFile
Changing mode on /home/demouser/publicFile

There is no command for manipulating local file permissions, but you can set the local umask, so that any files copied to the local system will have the appropriate permissions.

That can be done with the "lumask" command:

lumask 022
Local umask: 022

Now all regular files downloaded (as long as the "-p" flag is not used) will have 644 permissions.

SFTP allows you to create directories on both local and remote systems with "lmkdir" and "mkdir" respectively. These work as expected.

The rest of the file commands target only the remote filesystem:

ln
rm
rmdir

These commands replicate the basic behavior of the shell versions. If you need to perform these actions on the local file system, remember that you can drop into a shell by issuing this command:

!

Or execute a single command on the local system by prefacing the command with "!" like so:

!chmod 644 somefile

When you are finished with your SFTP session, use "exit" or "bye" to close the connection.

bye

Conclusion

Although SFTP is a simple tool, it is very useful for administrating servers and transferring files between them.

If you are used to using FTP or SCP to accomplish your transfers, SFTP is a good way to leverage the strengths of both. While it is not appropriate for every situation, it is a flexible tool to have in your repertoire.

本文转载自:https://www.digitalocean.com/community/tutorials/how-to-use-sftp-to-securely-transfer-files-with-...

Oscarfff
粉丝 74
博文 816
码字总数 97116
作品 0
崇明
后端工程师
私信 提问
java使用sfp上传后,删除临时文件删除不了

在sftp上传后,需要删除临时文件。发现总是删除不了。 代码如下: System.out.println(file1.isFile() +" "+file1); System.out.println("是否存在"+file1.exists()); boolean b = false; i......

凯哥java
2015/12/01
283
0
Xshell中如何如何使用SFTP转移文件

想必大家都知道,SFTP是基于SSH的文件传输协议,与ZMODEM相比具有更加安全且更为快速的文件传输功能。您可以使用sftp的命令从本地Xshell提示转移文件,下面就一起来学习具体操作技巧。 Xshe...

软件贴士
2018/04/10
0
0
Linux 下上传下载命令,SCP,SFTP,FTP

scp 帮助命令: man scp scp功能: 下载远程文件或者目录到本地, 如果想上传或者想下载目录,最好的办法是采用tar压缩一下,是最明智的选择. 从远程主机 下载东西到 本地电脑 拷贝文件命令 ...

jydd
2018/08/06
0
0
linux安装python常用的软件包和模块安装

一、安装软件pip yum -y install epel-release yum install python-pip pip install --upgrade pip 二、安装paramiko模块 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认...

漫然
2018/07/12
0
0
Linux命令之sftp - 安全文件传输命令行工具

用途说明 sftp命令可以通过ssh来上传和下载文件,是常用的文件传输工具,它的使用方式与ftp类似,但它使用ssh作为底层传输协议,所以安全性比ftp要好得多。 常用方式 格式:sftp 通过sftp连接...

898009427
2017/12/14
0
0

没有更多内容

加载失败,请刷新页面

加载更多

高并发场景下的缓存有哪些常见的问题?

一、缓存一致性问题 当数据时效性要求很高时,需要保证缓存中的数据与数据库中的保持一致,而且需要保证缓存节点和副本中的数据也保持一致,不能出现差异现象。 这就比较依赖缓存的过期和更新...

别打我会飞
38分钟前
3
0
List list = new ArrayList()为何父类引用指向子类对象(多态)

态:要有继承,方法的重写,父类引用指向子类对象 疑问一:父类引用指向子类对象 与指向父类对象 Animal cat = new Cat(); //向上转型。 父类引用指向子类对象,该引用不能再访问子类新增加的...

architect刘源源
39分钟前
3
0
分而治之-快速排序

快速排序的思想: 快速排序首先在数组中确定1个枢纽项(比如数组中的第一个元素),将大于该枢纽项的元素放到右侧,小于该枢纽项的元素放到左侧,这样枢纽项将数组划分成两部分。接着继续对划...

万山红遍
今天
5
0
Qt编写自定义控件9-导航按钮控件

前言 导航按钮控件,主要用于各种漂亮精美的导航条,我们经常在web中看到导航条都非常精美,都是html+css+js实现的,还自带动画过度效果,Qt提供的qss其实也是无敌的,支持基本上所有的CSS2属...

飞扬青云
今天
4
0
Python开发工具:pyJasper

原文:https://www.oschina.net/p/pyjasper 前言 pyJasper是 JasperReports 网络服务器的 Python 客户端。 pyJasper 是一组 Python 基础工具,可以用来处理 JasperReports 报表 。因为 Jasper...

A_裙232550246
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部