http://ice-k.iteye.com/blog/1068275html
http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.htmllinux
有一臺主機,安裝了windows7,在其安裝了virtualbox,而後安裝了ubuntu虛擬機。在windows7上安裝SecureCRT來ssh鏈接ubuntu虛擬機。通常在windows上面下載軟件,而後經過vsftp上傳到ubuntu虛擬機上。可是這樣很是麻煩,如今考慮使用sftp協議來直接傳輸。ubuntu
使用SecureCRT軟件ssh鏈接到ubuntu虛擬機。而後在SecureCRT上面打開SFTP會話,以下圖所示: windows
進入到sftp命令窗口之後,就能夠經過sftp命令來進行文件互傳的操做。下面主要介紹一下sftp命令的含義。ssh
sftp> help Available commands: ascii Set transfer mode to ASCII binary Set transfer mode to binary cd path Change remote directory to 'path' lcd path Change local directory to 'path' detail remote-path Display system information about remote file or folder ldetail local-path Display system information about local file or folder chgrp group path Change group of file 'path' to 'group' chmod mode path Change permissions of file 'path' to 'mode' chown owner path Change owner of file 'path' to 'owner' exit Quit sftp help Display this help text include filename Include commands from 'filename' Alternate: < filename get [-r][-a | -b] remote-path Download file -r downloads directory recursively force ascii (-a) or binary (-b) mode ln [-s] existingpath linkpath Hardlink / symlink remote file ls [options] [path] Display remote directory listing lls [options] [path] Display local directory listing mkdir path Create remote directory lmkdir path Create local directory mv oldpath newpath Move remote file lmv oldpath newpath Move local file open [user@]host[:port] Connect to remote host put [-r][-a | -b] local-path Upload file -r uploads directory recursively force ascii (-a) or binary (-b) mode pwd Display remote working directory lpwd Print local working directory quit Quit sftp rename oldname newname Rename remote file lrename oldname newname Rename local file rmdir path Remove remote directory lrmdir path Remove local directory rm path Delete remote file lrm path Delete local file su username Substitutes the current user This is only supported with VShell for Windows 3.5 or later. type [transfer-mode] Display or set file transfer mode view remote-path Download and open file version Display protocol version
翻譯一下就是:ui
sftp-- help 可用命令: cd 路徑 更改遠程目錄到「路徑」 lcd 路徑 更改本地目錄到「路徑」 chgrp group path 將文件「path」的組更改成「group」 chmod mode path 將文件「path」的權限更改成「mode」 chown owner path 將文件「path」的屬主更改成「owner」 exit 退出 sftp help 顯示這個幫助文本 get 遠程路徑 下載文件 ln existingpath linkpath 符號連接遠程文件 ls [選項] [路徑] 顯示遠程目錄列表 lls [選項] [路徑] 顯示本地目錄列表 mkdir 路徑 建立遠程目錄 lmkdir 路徑 建立本地目錄 mv oldpath newpath 移動遠程文件 open [用戶@]主機[:端口] 鏈接到遠程主機 put 本地路徑 上傳文件 pwd 顯示遠程工做目錄 lpwd 打印本地工做目錄 quit 退出 sftp rmdir 路徑 移除遠程目錄 lrmdir 路徑 移除本地目錄 rm 路徑 刪除遠程文件 lrm 路徑 刪除本地文件 symlink existingpath linkpath 符號連接遠程文件 version 顯示協議版本
咱們主要用到的就是一下六個命令this
cd 路徑 更改遠程目錄到「路徑」 lcd 路徑 更改本地目錄到「路徑」 ls [選項] [路徑] 顯示遠程目錄列表 lls [選項] [路徑] 顯示本地目錄列表 put 本地路徑 上傳文件 get 遠程路徑 下載文件
這裏在詳細說明一下遠程目錄和本地目錄。spa
遠程目錄:ls查看到的是ubuntu虛擬機上面的目錄翻譯
本地目錄:lls查看到的是windows實體機上面的目錄。3d
假設我須要實現windows下的e:\sftp\test1.txt和ubuntu下/home/enadmin/test2.txt的互換,能夠進行一下操做:
查看目錄權限
root@BJCGNMON01:/home# ll total 20 drwxr-xr-x 5 root root 4096 Apr 18 15:21 ./ drwxr-xr-x 24 root root 4096 Jan 3 14:14 ../ drwxr-xr-x 9 enadmin enadmin 4096 Apr 18 15:22 enadmin/ drwxr-xr-x 2 root root 4096 Apr 18 15:21 sftp/--能夠看到sftp的權限是root的 drwxr-xr-x 2 root root 4096 Apr 18 14:57 test/ root@BJCGNMON01:/home# chown -R enadmin.enadmin sftp/ root@BJCGNMON01:/home# ll total 20 drwxr-xr-x 5 root root 4096 Apr 18 15:21 ./ drwxr-xr-x 24 root root 4096 Jan 3 14:14 ../ drwxr-xr-x 9 enadmin enadmin 4096 Apr 18 15:22 enadmin/ drwxr-xr-x 2 enadmin enadmin 4096 Apr 18 15:21 sftp/--修改之後權限變成enadmin的了 drwxr-xr-x 2 root root 4096 Apr 18 14:57 test/
拷貝文件
sftp> put test* --初始想將文件從windows系統put到ubuntu系統,發現失敗,提示拒絕訪問 Uploading test1.txt to /home/test1.txt put: failed to upload e:/sftp/test1.txt. 拒絕訪問。 Uploading test2.txt to /home/test2.txt put: failed to upload e:/sftp/test2.txt. 拒絕訪問。 Uploading test3.txt to /home/test3.txt put: failed to upload e:/sftp/test3.txt. 拒絕訪問。 sftp> pwd --查看當前目錄是/home目錄,權限是root的 /home sftp> cd /home/enadmin/ --我切換到enadmin目錄下 sftp> ls Graphs.pm msmtp ntp sent smokeping.tar.gz smokepingrtt.tar.gz smokepingrtt2.tar.gz Target wqy wqy-zenhei-0.8.38-1.deb sftp> lls test1.txt test2.txt test3.txt sftp> put test* -再次嘗試將windows下的文件put到/homg/enadmin目錄下,此次成功 Uploading test1.txt to /home/enadmin/test1.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test1.txt: 4 bytes transferred in 0 seconds (4 bytes/s) Uploading test2.txt to /home/enadmin/test2.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test2.txt: 4 bytes transferred in 0 seconds (4 bytes/s) Uploading test3.txt to /home/enadmin/test3.txt 100% 5 bytes 5 bytes/s 00:00:00 e:/sftp/test3.txt: 5 bytes transferred in 0 seconds (5 bytes/s) sftp> cd /home/sftp/--在修改sftp目錄權限之後,再次切換到sftp目錄下 sftp> ls sftp> lls test1.txt test2.txt test3.txt sftp> put test* --這回put成功。因此跟權限相關 Uploading test1.txt to /home/sftp/test1.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test1.txt: 4 bytes transferred in 0 seconds (4 bytes/s) Uploading test2.txt to /home/sftp/test2.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test2.txt: 4 bytes transferred in 0 seconds (4 bytes/s) Uploading test3.txt to /home/sftp/test3.txt 100% 5 bytes 5 bytes/s 00:00:00 e:/sftp/test3.txt: 5 bytes transferred in 0 seconds (5 bytes/s)
注意點:這裏須要注意權限的問題。加入我是使用enadmin帳戶初始鏈接ubuntu的,那麼我put的文件只能放到文件夾權限是enadmin的目錄下,不能放到文件夾權限是root的目錄下。若是想放到文件夾權限是root的目錄下,那麼初始就使用root帳戶鏈接ubunut,而後再建立sftp會話,這樣就能夠了。
get操做沒有權限設定
sftp> pwd /home/enadmin sftp> lpwd e:/Documents sftp> lcd e:/sftp --切換目錄 sftp> lls rc.local test1.txt test2.txt test3.txt sftp> ls Graphs.pm msmtp ntp sent smokeping.tar.gz smokepingrtt.tar.gz smokepingrtt2.tar.gz Target test1.txt test2.txt test3.txt wqy wqy-zenhei-0.8.38-1.deb sftp> get Graphs.pm --下載文件到windows Downloading Graphs.pm from /home/enadmin/Graphs.pm 100% 13KB 13KB/s 00:00:00 /home/enadmin/Graphs.pm: 13503 bytes transferred in 0 seconds (13 KB/s) sftp> lls --查看windows目錄 Graphs.pm rc.local test1.txt test2.txt test3.txt sftp>