背景:

  有一台主机,安装了windows7,在其安装了virtualbox,然后安装了ubuntu虚拟机。在windows7上安装SecureCRT来ssh连接ubuntu虚拟机。一般在windows上面下载软件,然后通过vsftp上传到ubuntu虚拟机上。但是这样非常麻烦,现在考虑使用sftp协议来直接传输。

方案:

  使用SecureCRT软件ssh连接到ubuntu虚拟机。然后在SecureCRT上面打开SFTP会话,如下图所示:  
 

SFTP命令

  进入到sftp命令窗口以后,就可以通过sftp命令来进行文件互传的操作。下面主要介绍一下sftp命令的含义。
  1. sftp> help
  2. Available commands:
  3. ascii Set transfer mode to ASCII
  4. binary Set transfer mode to binary
  5. cd path Change remote directory to 'path'
  6. lcd path Change local directory to 'path'
  7. detail remote-path Display system information about remote
  8.                                  file or folder
  9. ldetail local-path Display system information about local
  10.                                  file or folder
  11. chgrp group path Change group of file 'path' to 'group'
  12. chmod mode path Change permissions of file 'path' to 'mode'
  13. chown owner path Change owner of file 'path' to 'owner'
  14. exit Quit sftp
  15. help Display this help text
  16. include filename Include commands from 'filename'
  17.                                  Alternate: < filename
  18. get [-r][-a | -b] remote-path Download file
  19.                                  -r downloads directory recursively
  20.                                  force ascii (-a) or binary (-b) mode
  21. ln [-s] existingpath linkpath Hardlink / symlink remote file
  22. ls [options] [path] Display remote directory listing
  23. lls [options] [path] Display local directory listing
  24. mkdir path Create remote directory
  25. lmkdir path Create local directory
  26. mv oldpath newpath Move remote file
  27. lmv oldpath newpath Move local file
  28. open [user@]host[:port] Connect to remote host
  29. put [-r][-a | -b] local-path Upload file
  30.                                  -r uploads directory recursively
  31.                                  force ascii (-a) or binary (-b) mode
  32. pwd Display remote working directory
  33. lpwd Print local working directory
  34. quit Quit sftp
  35. rename oldname newname Rename remote file
  36. lrename oldname newname Rename local file
  37. rmdir path Remove remote directory
  38. lrmdir path Remove local directory
  39. rm path Delete remote file
  40. lrm path Delete local file
  41. su username Substitutes the current user
  42.                                  This is only supported with VShell for
  43.                                  Windows 3.5 or later.
  44. type [transfer-mode] Display or set file transfer mode
  45. view remote-path Download and open file
  46. version Display protocol version

翻译一下就是:

  1. sftp-- help
  2. 可用命令:
  3. cd 路径 更改远程目录到“路径”
  4. lcd 路径 更改本地目录到“路径”
  5. chgrp group path 将文件“path”的组更改为“group”
  6. chmod mode path 将文件“path”的权限更改为“mode”
  7. chown owner path 将文件“path”的属主更改为“owner”
  8. exit 退出 sftp
  9. help 显示这个帮助文本 get 远程路径 下载文件
  10. ln existingpath linkpath 符号链接远程文件
  11. ls [选项] [路径] 显示远程目录列表
  12. lls [选项] [路径] 显示本地目录列表
  13. mkdir 路径 创建远程目录
  14. lmkdir 路径 创建本地目录
  15. mv oldpath newpath 移动远程文件
  16. open [用户@]主机[:端口] 连接到远程主机
  17. put 本地路径 上传文件
  18. pwd 显示远程工作目录
  19. lpwd 打印本地工作目录
  20. quit 退出 sftp
  21. rmdir 路径 移除远程目录
  22. lrmdir 路径 移除本地目录
  23. rm 路径 删除远程文件
  24. lrm 路径 删除本地文件
  25. symlink existingpath linkpath 符号链接远程文件
  26. version 显示协议版本


 

我们主要用到的就是一下六个命令

  1. cd 路径 更改远程目录到“路径”
  2. lcd 路径 更改本地目录到“路径”
  3. ls [选项] [路径] 显示远程目录列表
  4. lls [选项] [路径] 显示本地目录列表
  5. put 本地路径 上传文件 get 远程路径 下载文件



这里在详细说明一下远程目录本地目录

远程目录:ls查看到的是ubuntu虚拟机上面的目录

本地目录:lls查看到的是windows实体机上面的目录。

实例:

假设我需要实现windows下的e:\sftp\test1.txt和ubuntu下/home/enadmin/test2.txt的互换,可以进行一下操作:

put操作:windows->linux

查看目录权限

  1. root@BJCGNMON01:/home# ll
  2. total 20
  3. drwxr-xr-x 5 root root 4096 Apr 18 15:21 ./
  4. drwxr-xr-x 24 root root 4096 Jan 3 14:14 ../
  5. drwxr-xr-x 9 enadmin enadmin 4096 Apr 18 15:22 enadmin/
  6. drwxr-xr-x 2 root root 4096 Apr 18 15:21 sftp/--可以看到sftp的权限是root的
  7. drwxr-xr-x 2 root root 4096 Apr 18 14:57 test/
  8. root@BJCGNMON01:/home# chown -R enadmin.enadmin sftp/
  9. root@BJCGNMON01:/home# ll
  10. total 20
  11. drwxr-xr-x 5 root root 4096 Apr 18 15:21 ./
  12. drwxr-xr-x 24 root root 4096 Jan 3 14:14 ../
  13. drwxr-xr-x 9 enadmin enadmin 4096 Apr 18 15:22 enadmin/
  14. drwxr-xr-x 2 enadmin enadmin 4096 Apr 18 15:21 sftp/--修改以后权限变成enadmin的了
  15. drwxr-xr-x 2 root root 4096 Apr 18 14:57 test/


 

拷贝文件

  1. sftp> put test* --初始想将文件从windows系统put到ubuntu系统,发现失败,提示拒绝访问
  2. Uploading test1.txt to /home/test1.txt
  3. put: failed to upload e:/sftp/test1.txt. 拒绝访问。
  4. Uploading test2.txt to /home/test2.txt
  5. put: failed to upload e:/sftp/test2.txt. 拒绝访问。
  6. Uploading test3.txt to /home/test3.txt
  7. put: failed to upload e:/sftp/test3.txt. 拒绝访问。
  8. sftp> pwd --查看当前目录是/home目录,权限是root的
  9. /home
  10. sftp> cd /home/enadmin/ --我切换到enadmin目录下
  11. sftp> ls
  12. Graphs.pm msmtp ntp
  13. sent smokeping.tar.gz smokepingrtt.tar.gz
  14. smokepingrtt2.tar.gz Target wqy
  15. wqy-zenhei-0.8.38-1.deb
  16. sftp> lls
  17. test1.txt test2.txt test3.txt
  18. sftp> put test* -再次尝试将windows下的文件put到/homg/enadmin目录下,这次成功
  19. Uploading test1.txt to /home/enadmin/test1.txt
  20.   100% 4 bytes 4 bytes/s 00:00:00
  21. e:/sftp/test1.txt: 4 bytes transferred in 0 seconds (4 bytes/s)
  22. Uploading test2.txt to /home/enadmin/test2.txt
  23.   100% 4 bytes 4 bytes/s 00:00:00
  24. e:/sftp/test2.txt: 4 bytes transferred in 0 seconds (4 bytes/s)
  25. Uploading test3.txt to /home/enadmin/test3.txt
  26.   100% 5 bytes 5 bytes/s 00:00:00
  27. e:/sftp/test3.txt: 5 bytes transferred in 0 seconds (5 bytes/s)

  28. sftp> cd /home/sftp/--在修改sftp目录权限以后,再次切换到sftp目录下
  29. sftp> ls
  30. sftp> lls
  31. test1.txt test2.txt test3.txt
  32. sftp> put test* --这回put成功。所以跟权限相关
  33. Uploading test1.txt to /home/sftp/test1.txt
  34.   100% 4 bytes 4 bytes/s 00:00:00
  35. e:/sftp/test1.txt: 4 bytes transferred in 0 seconds (4 bytes/s)
  36. Uploading test2.txt to /home/sftp/test2.txt
  37.   100% 4 bytes 4 bytes/s 00:00:00
  38. e:/sftp/test2.txt: 4 bytes transferred in 0 seconds (4 bytes/s)
  39. Uploading test3.txt to /home/sftp/test3.txt
  40.   100% 5 bytes 5 bytes/s 00:00:00
  41. e:/sftp/test3.txt: 5 bytes transferred in 0 seconds (5 bytes/s)


 

注意点:这里需要注意权限的问题。加入我是使用enadmin账户初始连接ubuntu的,那么我put的文件只能放到文件夹权限是enadmin的目录下,不能放到文件夹权限是root的目录下。如果想放到文件夹权限是root的目录下,那么初始就使用root账户连接ubunut,然后再创建sftp会话,这样就可以了。

get操作:linux->windows

get操作没有权限设定

  1. sftp> pwd
  2. /home/enadmin
  3. sftp> lpwd
  4. e:/Documents
  5. sftp> lcd e:/sftp --切换目录
  6. sftp> lls
  7. rc.local test1.txt test2.txt
  8. test3.txt
  9. sftp> ls
  10. Graphs.pm msmtp ntp
  11. sent smokeping.tar.gz smokepingrtt.tar.gz
  12. smokepingrtt2.tar.gz Target test1.txt
  13. test2.txt test3.txt wqy
  14. wqy-zenhei-0.8.38-1.deb
  15. sftp> get Graphs.pm --下载文件到windows
  16. Downloading Graphs.pm from /home/enadmin/Graphs.pm
  17.   100% 13KB 13KB/s 00:00:00
  18. /home/enadmin/Graphs.pm: 13503 bytes transferred in 0 seconds (13 KB/s)
  19. sftp> lls --查看windows目录
  20. Graphs.pm rc.local test1.txt
  21. test2.txt test3.txt
  22. sftp>


02-02 08:21