Common operations of scp and sftp

 

Copy files offsite directly: scp

The full name of SCP is secure copy (remote file copy program). This command is attached to openssh-clients. Its function is to copy between machines, and the transmission between machines is completely encrypted.

The simplest scp usage is as follows:

copy code
[root@www ~]# scp [-pr] [-l rate] file [account@]host: directory name <== upload
[root@www ~]# scp [-pr] [-l rate] [account@]host:file directory name<==download
Options and parameters:
-p : keep the permission data of the original file;
-r : When the copy source is a directory, the entire directory (including subdirectories) can be copied
-l : The transmission speed can be limited, the unit is Kbits/s, for example [-l 800] means the transmission speed limit is 100Kbytes/s

# 1. Copy all the /etc/hosts* of this machine to the student's home directory above 127.0.0.1
[root@www ~]# scp /etc/hosts* [email protected]:~
[email protected]'s password: <==Enter student password
hosts                        100%  207         0.2KB/s   00:00
hosts.allow                  100%  161         0.2KB/s   00:00
hosts.deny                   100%  347         0.3KB/s   00:00
# File name display progress capacity (bytes) transfer speed remaining time
# You can look carefully, the message that appears has five fields, the meanings are as shown above.

# 2. Copy the /etc/bashrc of the remote host 127.0.0.1 to the /tmp of the local machine
[root@www ~]# scp [email protected]:/etc/bashrc /tmp
copy code

In fact, the point of uploading or downloading is the colon (:)! Concatenated after the colon is the remote host's file. Therefore, if the colon is before, it means downloading from the remote host, and if the colon is after, it means that the local data is uploaded! And if you want to copy the directory, you can add the -r option!

example:

Suppose there is a file named /root/dd_10mb_file on this machine, and the file is 10 MB in size. Suppose you want to upload to 127.0.0.1 under /tmp, and you have access to the root account on 127.0.0.1. But since bandwidth is precious, you only want to spend 100Kbyes/s for this action, so how do you order it?

answer:

Since this file does not exist by default, we have to use dd to create a large file first:
dd if=/dev/zero of=/root/dd_10mb_file bs=1M count=10
After the establishment is complete, since it is uploading data, observe the -l option, the rate is bit, and the bytes converted into the capacity need to be multiplied by 8 times, so the command should be issued like this:
scp -l 800 /root/dd_10mb_file [email protected]:/tmp

 

File transfer method that simulates FTP: sftp

ssh is to log in to the remote server to work, so what if you just want to download or upload files from the remote server? Then instead of using ssh, you must use sftp or scp. These two commands also use the ssh channel (port 22), just simulate the actions of FTP and copy. Let's talk about sftp first. The usage of this command is very similar to ssh, except that ssh is used for logging in and sftp is uploading/downloading files.

[root@www ~]# sftp student@localhost
Connecting to localhost...
student@localhost's password: <== Please enter your password here!
sftp> exit <== This is where you are waiting for your ftp-related commands!

After entering sftp, it is no different from the operation method in general FTP mode! Let's talk about the usage instructions under the sftp interface!

Behavior against the remote server host (Server)
Change directory to /etc/test or other directory cd /etc/test
cd PATH
List the file names in the current directory ls
you
create directory mkdir directory
delete directory rmdir directory
show the current directory pwd
Change file or directory group chgrp groupname PATH
Change file or directory owner chown username PATH
Change permissions on a file or directory chmod 644 PATH
Among them, 644 is related to permissions! Back to the basics!
create link file ln oldname newname
delete file or directory rm PATH
Change file or directory name rename oldname newname
leave the remote host exit (or) bye (or) quit
For the behavior of the machine (Client) (both with the lowercase of l, L)
Change the directory to the local PATH lcd PATH
List the file names in the directory where the machine is currently located lls
Create a directory locally lmkdir
Displays the current local directory lpwd
Behavior for data upload/download
Upload files from the local machine to the remote host put [local directory or file] [remote]
put [local directory or file]
If this format is used, the file will be placed in the directory of the current remote host!
Download the file from the remote host get [远程主机目录或档案] [本机]
get [远程主机目录或档案]
若是这种格式,则档案会放置在目前本机所在的目录当中!可以使用通配符,例如:
get *
get *.rpm
亦是可以的格式!

就整体而言, sftp 在 Linux 底下,如果不考虑图形接口,那么他已经可以取代 FTP 了呢!因为所有的功能都已经涵盖啦!因此,在不考虑到图形接口的 FTP 软件时,可以直接关掉 FTP 的服务,而改以 sftp-server 来提供 FTP 的服务吧! ^_^

例题:

假设 localhost 为远程服务器,且服务器上有 student 这个使用者。你想要 (1)将本机的 /etc/hosts 上传到 student 家目录,并 (2)将 student 的 .bashrc 复制到本机的 /tmp 底下,该如何透过 sftp 达成?

答:

copy code
[root@www ~]# sftp student@localhost
sftp> lls /etc/hosts   <==先看看本机有没有这个档案
/etc/hosts
sftp> put /etc/hosts   <==有的话,那就上传吧!
Uploading /etc/hosts to /home/student/hosts
/etc/hosts                        100%  243     0.2KB/s   00:00
sftp> ls               <==有没有上传成功?看远程目录下的文件名
hosts
sftp> ls -a            <==那有没有隐藏档呢?
.               ..              .bash_history   .bash_logout
.bash_profile   .bashrc         .mozilla        hosts
sftt> lcd /tmp         <==切换本机目录到 /tmp 
sftp> lpwd             <==只是进行确认而已!
Local working directory: /tmp
sftp> get .bashrc      <==没问题就下载吧!
Fetching /home/student/.bashrc to .bashrc
/home/student/.bashrc             100%  124     0.1KB/s   00:00
sftp> lls -a           <==看本地端档案档名
.        .font-unix   keyring-rNd7qX  .X11-unix
..       .gdm_socket  lost+found      scim-panel-socket:0-root
.bashrc  .ICE-unix    mapping-root    .X0-lock
sftp> exit             <==离开吧!
copy code

文件异地直接复制: scp

SCP的全称是secure copy (remote file copy program),此命令是openssh-clients附带的,它的作用就是在机器之间实现拷贝,且机器之间的传输完全是加密的。

最简单的 scp 用法如下:

copy code
[root@www ~]# scp [-pr] [-l 速率] file  [账号@]主机:目录名 <==上传
[root@www ~]# scp [-pr] [-l 速率] [账号@]主机:file  目录名 <==下载
选项与参数:
-p :保留原本档案的权限数据;
-r :复制来源为目录时,可以复制整个目录 (含子目录)
-l :可以限制传输的速度,单位为 Kbits/s ,例如 [-l 800] 代表传输速限 100Kbytes/s

# 1. 将本机的 /etc/hosts* 全部复制到 127.0.0.1 上面的 student 家目录内
[root@www ~]# scp /etc/hosts* [email protected]:~
[email protected]'s password: <==输入 student 密码
hosts                        100%  207         0.2KB/s   00:00
hosts.allow                  100%  161         0.2KB/s   00:00
hosts.deny                   100%  347         0.3KB/s   00:00
# 文件名显示                   进度  容量(bytes) 传输速度  剩余时间
# 你可以仔细看,出现的讯息有五个字段,意义如上所示。

# 2. 将 127.0.0.1 这部远程主机的 /etc/bashrc 复制到本机的 /tmp 底下
[root@www ~]# scp [email protected]:/etc/bashrc /tmp
copy code

其实上传或下载的重点是那个冒号 (:) 啰!连接在冒号后面的就是远程主机的档案。 因此,如果冒号在前,代表的就是从远程主机下载下来,如果冒号在后,则代表本机数据上传啦! 而如果想要复制目录的话,那么可以加上 -r 的选项!

例题:

假设本机有个档案档名为 /root/dd_10mb_file ,这个档案有 10 MB 这么大。假设你想要上传到 127.0.0.1 的 /tmp 底下去, 而且你在 127.0.0.1 上面有 root 这个账号的使用权。但由于带宽很宝贵,因此你只想要花费 100Kbyes/s 的传输量给此一动作, 那该如何下达指令?

答:

由于预设不存在这个档案,因此我们得先使用 dd 来建立一个大档案:
dd if=/dev/zero of=/root/dd_10mb_file bs=1M count=10
建立妥当之后,由于是上传数据,观察 -l 的选项中,那个速率用的是 bit ,转成容量的 bytes 需要乘上 8 倍,因此指令就要这样下达:
scp -l 800 /root/dd_10mb_file [email protected]:/tmp

 

模拟 FTP 的文件传输方式: sftp

ssh 是登入远程服务器进行工作,那如果你只是想要从远程服务器下载或上传档案呢? 那就不是使用 ssh 啦,而必须要使用 sftp 或 scp。这两个指令也都是使用 ssh 的通道 (port 22),只是模拟成 FTP 与复制的动作而已。我们先谈谈 sftp ,这个指令的用法与 ssh 很相似,只是 ssh 是用在登入而 sftp 在上传/下载文件而已。

[root@www ~]# sftp student@localhost
Connecting to localhost...
student@localhost's password: <== 这里请输入密码啊!
sftp> exit  <== 这里就是在等待你输入 ftp 相关指令的地方了!

进入到 sftp 之后,那就跟在一般 FTP 模式下的操作方法没有两样了!底下我们就来谈一谈, sftp 这个接口下的使用指令吧!

针对远方服务器主机 (Server) 之行为
变换目录到 /etc/test 或其他目录 cd /etc/test
cd PATH
列出目前所在目录下的文件名 ls
dir
建立目录 mkdir directory
删除目录 rmdir directory
显示目前所在的目录 pwd
更改档案或目录群组 chgrp groupname PATH
更改档案或目录拥有者 chown username PATH
更改档案或目录的权限 chmod 644 PATH
其中,644 与权限有关!回去看基础篇!
建立连结档 ln oldname newname
删除档案或目录 rm PATH
更改档案或目录名称 rename oldname newname
离开远程主机 exit (or) bye (or) quit
针对本机 (Client) 之行为(都加上 l, L 的小写 )
变换目录到本机的 PATH 当中 lcd PATH
列出目前本机所在目录下的文件名 lls
在本机建立目录 lmkdir
显示目前所在的本机目录 lpwd
针对资料上传/下载的行为
将档案由本机上传到远程主机 put [本机目录或档案] [远程]
put [本机目录或档案]
如果是这种格式,则档案会放置到目前远程主机的目录下!
将档案由远程主机下载回来 get [远程主机目录或档案] [本机]
get [远程主机目录或档案]
若是这种格式,则档案会放置在目前本机所在的目录当中!可以使用通配符,例如:
get *
get *.rpm
亦是可以的格式!

就整体而言, sftp 在 Linux 底下,如果不考虑图形接口,那么他已经可以取代 FTP 了呢!因为所有的功能都已经涵盖啦!因此,在不考虑到图形接口的 FTP 软件时,可以直接关掉 FTP 的服务,而改以 sftp-server 来提供 FTP 的服务吧! ^_^

例题:

假设 localhost 为远程服务器,且服务器上有 student 这个使用者。你想要 (1)将本机的 /etc/hosts 上传到 student 家目录,并 (2)将 student 的 .bashrc 复制到本机的 /tmp 底下,该如何透过 sftp 达成?

答:

copy code
[root@www ~]# sftp student@localhost
sftp> lls /etc/hosts   <==先看看本机有没有这个档案
/etc/hosts
sftp> put /etc/hosts   <==有的话,那就上传吧!
Uploading /etc/hosts to /home/student/hosts
/etc/hosts                        100%  243     0.2KB/s   00:00
sftp> ls               <==有没有上传成功?看远程目录下的文件名
hosts
sftp> ls -a            <==那有没有隐藏档呢?
.               ..              .bash_history   .bash_logout
.bash_profile   .bashrc         .mozilla        hosts
sftt> lcd /tmp         <==切换本机目录到 /tmp 
sftp> lpwd             <==只是进行确认而已!
Local working directory: /tmp
sftp> get .bashrc <== no problem download it!
Fetching /home/student/.bashrc to .bashrc
/home/student/.bashrc             100%  124     0.1KB/s   00:00
sftp> lls -a <== see local file name
.        .font-unix   keyring-rNd7qX  .X11-unix
..       .gdm_socket  lost+found      scim-panel-socket:0-root
.bashrc  .ICE-unix    mapping-root    .X0-lock
sftp> exit <== leave it!
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774642&siteId=291194637