Linux系统中远程传输命令scp

linux系统可以实现把文件通过网络从一台主机传输至另一台主机,而且所有的数据都进行加密处理。

准备两台测试主机pc1和pc2,均为RHEL7,网络连接方式为桥接方式,可以连接外网。

测试基本用法,从pc1向pc2中传输数据。

1、登录pc1,查看IP,测试外网,创建测试目录、测试数据

[root@pc1 ~]# ifconfig | head -n 3  ## 查看pc1IP地址
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.20  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::20c:29ff:fee4:f7b9  prefixlen 64  scopeid 0x20<link>
[root@pc1 ~]# ping -c 3 www.baidu.com  ## 测试外网,联通
PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data.
64 bytes from 39.156.66.14: icmp_seq=1 ttl=51 time=16.7 ms
64 bytes from 39.156.66.14: icmp_seq=2 ttl=51 time=16.3 ms
64 bytes from 39.156.66.14: icmp_seq=3 ttl=51 time=16.4 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 8041ms
rtt min/avg/max/mdev = 16.376/16.541/16.778/0.201 ms
[root@pc1 ~]# pwd
/root
[root@pc1 ~]# mkdir dir1
[root@pc1 ~]# cd dir1/
[root@pc1 dir1]# seq 5 > a.txt
[root@pc1 dir1]# mkdir testdir
[root@pc1 dir1]# seq 3 > testdir/b.txt
[root@pc1 dir1]# tree  ## 测试数据及目录
.
├── a.txt
└── testdir
    └── b.txt

1 directory, 2 files

 

2、登录pc2,查看IP地址,测试外网,创建测试目录

[root@pc2 ~]# ifconfig | head -n 3  ## 查看pc2IP地址
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.13  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::20c:29ff:feaa:2b29  prefixlen 64  scopeid 0x20<link>
[root@pc2 ~]# ping -c 3 www.baidu.com  ## 测试外网
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18: icmp_seq=1 ttl=51 time=14.6 ms
64 bytes from 39.156.66.18: icmp_seq=2 ttl=51 time=14.4 ms
64 bytes from 39.156.66.18: icmp_seq=3 ttl=51 time=14.4 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 8037ms
rtt min/avg/max/mdev = 14.413/14.482/14.604/0.086 ms
[root@pc2 ~]# mkdir dir2
[root@pc2 ~]# cd dir2/
[root@pc2 dir2]# pwd  ## 测试目录
/root/dir2
[root@pc2 dir2]# ls -a
.  ..

 

3、在pc1主机中,使用scp命令向pc2主机/root/dir2目录中传输文件a.txt及x.txt

 基本格式:scp    [参数]    本地文件    远程账户@远程IP地址:远程目录

[root@pc1 dir1]# ls
a.txt  testdir
[root@pc1 dir1]# scp a.txt root@192.168.3.13:/root/dir2/  ## 从pc1主机中向pc2主机/root/dir2/目录中传输文件a.txt
root@192.168.3.13's password: ## 此处输入pc2的root密码
a.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc1 dir1]# cp a.txt x.txt  ## 将a.txt复制为x.txt,作为另一份测试数据
[root@pc1 dir1]# ls
a.txt  testdir  x.txt
## root用户情况下,测试不输入远程用户能够传输,可以 [root@pc1 dir1]# scp x.txt
192.168.3.13:/root/dir2/ root@192.168.3.13's password: ## 此处输入pc2的root密码 x.txt 100% 10 0.0KB/s 00:00

 

4、在pc2/root/dir2/目录下检查是否接收到了传输的文件

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ll  ## 查看接收的文件
total 8
-rw-r--r--. 1 root root 10 Nov  4 15:07 a.txt
-rw-r--r--. 1 root root 10 Nov  4 15:08 x.txt

 

5、在pc1主机中向pc2主机/root/dir2目录中传输目录testdir,需要在正常传输的基础上加 -r参数,表示递归传输

[root@pc1 dir1]# ls
a.txt  testdir  x.txt
[root@pc1 dir1]# scp -r testdir/ root@192.168.3.13:/root/dir2/  ## 传输目录,在正常传输的基础上添加-r参数
root@192.168.3.13's password:   ## 此处输入pc2的root密码
b.txt                                                                                                              100%    6     0.0KB/s   00:00
[root@pc1 dir1]# cp -r testdir/ testdir2
[root@pc1 dir1]# ls
a.txt  testdir  testdir2  x.txt
[root@pc1 dir1]# scp -r testdir2/ 192.168.3.13:/root/dir2/  ## root情况下,忽略远程用户@也可以正常传输
root@192.168.3.13's password:   ## 此处输入pc2的root密码
b.txt                                                                                                              100%    6     0.0KB/s   00:00

 

6、在pc2主机/root/dir2/目录中的检查是否接收到目录testdir、testdir2

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ll  ## 接收到传输的目录
total 8
-rw-r--r--. 1 root root 10 Nov  4 15:07 a.txt
drwxr-xr-x. 2 root root 18 Nov  4 15:24 testdir
drwxr-xr-x. 2 root root 18 Nov  4 15:25 testdir2
-rw-r--r--. 1 root root 10 Nov  4 15:08 x.txt

 

测试利用scp从远程主机下载文件到本地

基本用法:scp   远程用户@远程用户IP地址:传输的文件   本地接收目录

 

1、删除pc1主机/root/dir1/目录下的所有文件及目录

[root@pc1 dir1]# pwd
/root/dir1
[root@pc1 dir1]# rm -rf *
[root@pc1 dir1]# ls -a
.  ..

 

2、查看pc2主机/root/dir2目录下的文件及目录

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ls
a.txt  testdir  testdir2  x.txt
[root@pc2 dir2]# tree
.
├── a.txt
├── testdir
│   └── b.txt
├── testdir2
│   └── b.txt
└── x.txt

2 directories, 4 files

 

3、在pc1主机中,从pc2主机/root/dir2/目录中下载a.txt和x.txt

[root@pc1 dir1]# pwd
/root/dir1
[root@pc1 dir1]# ls -a
.  ..
[root@pc1 dir1]# scp root@192.168.3.13:/root/dir2/a.txt ./  ##在pc1主机中,从pc2主机远程下载a.txt
root@192.168.3.13's password:  ## 此处输入pc2的root密码
a.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc1 dir1]# ls -a
.  ..  a.txt
[root@pc1 dir1]# scp 192.168.3.13:/root/dir2/x.txt ./   ## root登录下,可以忽略远程用户@
root@192.168.3.13's password:  ## 此处输入pc2的root密码
x.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc1 dir1]# ls -a
.  ..  a.txt  x.txt

 

4、在pc1主机中,从pc2主机/root/dir2/目录中远程下载目录testdir和testdir2,需要在正常传输的基础上加-r参数

[root@pc1 dir1]# pwd
/root/dir1
[root@pc1 dir1]# ls -a
.  ..  a.txt  x.txt
[root@pc1 dir1]# scp -r root@192.168.3.13:/root/dir2/testdir ./  ## 在正常传输的基础上,加-r参数,实现目录的递归传输
root@192.168.3.13's password:   ## 此处输入pc2的root密码
b.txt                                                                                                              100%    6     0.0KB/s   00:00
[root@pc1 dir1]# ls -a
.  ..  a.txt  testdir  x.txt
[root@pc1 dir1]# scp -r 192.168.3.13:/root/dir2/testdir2 ./
root@192.168.3.13's password:   ## 此处输入pc2的root密码
b.txt                                                                                                              100%    6     0.0KB/s   00:00
[root@pc1 dir1]# ls -a
.  ..  a.txt  testdir  testdir2  x.txt
[root@pc1 dir1]# tree
.
├── a.txt
├── testdir
│   └── b.txt
├── testdir2
│   └── b.txt
└── x.txt

2 directories, 4 files

 

普通用户使用scp远程传输及下载文件及目录?

 

1、在pc1主机中切换至普通用户linuxprobe,创建测试目录及文件

[root@pc1 dir1]# su - linuxprobe
Last login: Sun Nov 1 15:41:04 CST 2020 on :0
[linuxprobe@pc1 ~]$ cd ~
[linuxprobe@pc1 ~]$ mkdir dir1
[linuxprobe@pc1 ~]$ cd dir1/
[linuxprobe@pc1 dir1]$ pwd
/home/linuxprobe/dir1
[linuxprobe@pc1 dir1]$ ls -a
.  ..
[linuxprobe@pc1 dir1]$ seq 7 > a.txt
[linuxprobe@pc1 dir1]$ mkdir testdir
[linuxprobe@pc1 dir1]$ cp a.txt testdir/b.txt
[linuxprobe@pc1 dir1]$ ls -a
.  ..  a.txt  testdir
[linuxprobe@pc1 dir1]$ tree  ## 测试目录及数据
.
├── a.txt
└── testdir
    └── b.txt

1 directory, 2 files

 

2、在pc2中切换至普通用户,创建测试目录

[root@pc2 ~]# su - usertest
[usertest@pc2 ~]$ cd ~
[usertest@pc2 ~]$ mkdir dir2
[usertest@pc2 ~]$ cd dir2/
[usertest@pc2 dir2]$ pwd  ## 测试目录
/home/usertest/dir2
[usertest@pc2 dir2]$ ls -a
.  ..

 

3、在普通用户模式下,在pc1主机中,向pc2主机传输文件

[linuxprobe@pc1 dir1]$ pwd
/home/linuxprobe/dir1
[linuxprobe@pc1 dir1]$ ls -a
. .. a.txt testdir
[linuxprobe@pc1 dir1]$ scp a.txt usertest@192.168.3.13:/home/usertest/dir2
usertest@192.168.3.13's password:  ## 此处输入用户usertest的密码
a.txt 100% 14 0.0KB/s 00:00
[linuxprobe@pc1 dir1]$ cp a.txt x.txt
[linuxprobe@pc1 dir1]$ ls -a
.  ..  a.txt  testdir  x.txt
[linuxprobe@pc1 dir1]$ scp x.txt 192.168.3.13:/home/usertest/dir2  ## 在普通用户登录情况下,省略远程用户@,无论root密码和普通用户密码,不能实现传输
linuxprobe@192.168.3.13's password:
Permission denied, please try again.
linuxprobe@192.168.3.13's password:
Permission denied, please try again.
linuxprobe@192.168.3.13's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
lost connection

 

4、在pc2主机中,检测是否接收到传输文件

[usertest@pc2 dir2]$ pwd
/home/usertest/dir2
[usertest@pc2 dir2]$ ls -a
.  ..  a.txt

 

5、在pc1主机中,测试远程传输目录到pc2主机,需要在正常传输的基础上,加-r参数

[linuxprobe@pc1 dir1]$ pwd
/home/linuxprobe/dir1
[linuxprobe@pc1 dir1]$ ls -a
.  ..  a.txt  testdir  x.txt
[linuxprobe@pc1 dir1]$ scp -r testdir/ usertest@192.168.3.13:/home/usertest/dir2  ## 加 -r参数,从pc1向pc2中传输目录
usertest@192.168.3.13's password:
b.txt                                                                                                              100%   14     0.0KB/s   00:00
[linuxprobe@pc1 dir1]$ cp -r testdir/ testdir2
[linuxprobe@pc1 dir1]$ scp -r testdir2/ 192.168.3.13:/home/usertest/dir2   ## 普通用户登录情况下,不能省略远程用户@
linuxprobe@192.168.3.13's password:
Permission denied, please try again.
linuxprobe@192.168.3.13's password:
Permission denied, please try again.

 

6、在pc2主机中验证是否接收到目录

[usertest@pc2 dir2]$ pwd
/home/usertest/dir2
[usertest@pc2 dir2]$ ls -a
.  ..  a.txt  testdir

 

普通用户登录下,从远程下载文件及目录:

1、pc1删除/home/linuxprobe/dir1/下所有文件及目录

[linuxprobe@pc1 dir1]$ pwd
/home/linuxprobe/dir1
[linuxprobe@pc1 dir1]$ rm -rf *

 

2、查看pc2主机/home/usertest/dir2/目录下文件及目录

[usertest@pc2 dir2]$ pwd
/home/usertest/dir2
[usertest@pc2 dir2]$ ls -a
.  ..  a.txt  testdir

 

3、在pc1中,从pc2远程下载文件及目录

[linuxprobe@pc1 dir1]$ pwd
/home/linuxprobe/dir1
[linuxprobe@pc1 dir1]$ ls -a
.  ..
[linuxprobe@pc1 dir1]$ scp usertest@192.168.3.13:/home/usertest/dir2/a.txt ./
usertest@192.168.3.13's password:
a.txt                                                                                                              100%   14     0.0KB/s   00:00
[linuxprobe@pc1 dir1]$ ls -a
.  ..  a.txt
[linuxprobe@pc1 dir1]$ scp -r usertest@192.168.3.13:/home/usertest/dir2/testdir ./
usertest@192.168.3.13's password:
b.txt                                                                                                              100%   14     0.0KB/s   00:00
[linuxprobe@pc1 dir1]$ ls -a
.  ..  a.txt  testdir
[linuxprobe@pc1 dir1]$ tree
.
├── a.txt
└── testdir
    └── b.txt

1 directory, 2 files

 

4、删除pc1主机和pc2主机的测试目录及测试数据,均切换回root用户

[linuxprobe@pc1 ~]$ whoami
linuxprobe
[linuxprobe@pc1 ~]$ rm -rf /home/linuxprobe/dir1/
[linuxprobe@pc1 ~]$ su - root
Password:
Last login: Wed Nov  4 14:38:06 CST 2020 from 192.168.3.4 on pts/0
[usertest@pc2 ~]$ whoami
usertest
[usertest@pc2 ~]$ rm -rf /home/usertest/dir2/
[usertest@pc2 ~]$ su - root
Password:
Last login: Wed Nov  4 14:38:20 CST 2020 from 192.168.3.4 on pts/0

 

scp命令实现远程免密传输?

root模式下,测试pc2主机向pc1主机远程免密传输文件、目录;pc2免密从pc1主机下载文件及目录

1、查看测试目录及数据

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ls -a
.  ..  a.txt  testdir
[root@pc2 dir2]# tree
.
├── a.txt
└── testdir
    └── b.txt

1 directory, 2 files
[root@pc1 dir1]# pwd
/root/dir1
[root@pc1 dir1]# ls -a
.  ..

 

 

2、在pc2主机中,使用ssh-keygen -t rsa命令生成密钥对

[root@pc2 dir2]# cd ~
[root@pc2 ~]# ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  .config  .dbus    dir2       Downloads  .ICEauthority         .local  Pictures  .tcshrc    Videos
..  .bash_history    .bash_profile  .cache   .cshrc   Desktop  Documents  .esd_auth  initial-setup-ks.cfg  Music   Public    Templates  .viminfo
[root@pc2 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): ## 回车
Enter same passphrase again:  ## 回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:  ## 回车
3d:90:7e:cc:73:bc:d6:83:cf:d7:19:c5:ee:0c:18:60 root@pc2
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|         .E      |
|        o. .   . |
|       . = ..   o|
|        S B oo ..|
|         . +.+...|
|            + o++|
|           . o o=|
|              o. |
+-----------------+
[root@pc2 ~]# ls -a
.                .bash_history  .bashrc  .cshrc   dir2       .esd_auth             .local    Public   Templates
..               .bash_logout   .cache   .dbus    Documents  .ICEauthority         Music     .ssh     Videos
anaconda-ks.cfg  .bash_profile  .config  Desktop  Downloads  initial-setup-ks.cfg  Pictures  .tcshrc  .viminfo
[root@pc2 ~]# ls -a .ssh/
.  ..  id_rsa  id_rsa.pub

 

2、查看pc1主机root目录下文件及目录

[root@pc1 ~]# pwd
/root
[root@pc1 ~]# ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  .config  .dbus    dir1       Downloads  .ICEauthority         .local  Pictures  .tcshrc    Videos
..  .bash_history    .bash_profile  .cache   .cshrc   Desktop  Documents  .esd_auth  initial-setup-ks.cfg  Music   Public    Templates

 

3、在pc2主机中,将密钥对中的公钥传输至pc1主机

[root@pc2 ~]# ssh-copy-id 192.168.3.20
The authenticity of host '192.168.3.20 (192.168.3.20)' can't be established.
ECDSA key fingerprint is 55:84:57:c6:8d:66:a0:1d:72:35:24:5d:ba:7d:1d:50.
Are you sure you want to continue connecting (yes/no)? yes  ## 此处输入yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.3.20's password:  ## 此处输入pc1root密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.3.20'"
and check to make sure that only the key(s) you wanted were added.

 

4、查看pc1主机/root/目录下文件:

[root@pc1 ~]# pwd
/root
[root@pc1 ~]# ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  .config  .dbus    dir1       Downloads  .ICEauthority         .local  Pictures  .ssh     Templates
..  .bash_history    .bash_profile  .cache   .cshrc   Desktop  Documents  .esd_auth  initial-setup-ks.cfg  Music   Public    .tcshrc  Videos
[root@linuxprobe ~]# ls -a .ssh/  ## pc1主机已经接收到公钥文件
. .. authorized_keys

 

5、查看pc1测试目录

[root@pc1 dir1]# pwd
/root/dir1
[root@pc1 dir1]# ls -a
.  ..

 

6、测试从pc2主机远程免密向pc1主机传输文件及目录

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ls -a
.  ..  a.txt  testdir
[root@pc2 dir2]# scp a.txt root@192.168.3.20:/root/dir1/  ## 免密传输文件
a.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc2 dir2]# scp -r testdir/ 192.168.3.20:/root/dir1/  ## 免密传输目录
b.txt                                                                                                              100%   10     0.0KB/s   00:00

 

7、查看pc1主机 /root/dir1/目录是否接收到文件

[root@pc1 dir1]# pwd
/root/dir1
[root@pc1 dir1]# ls -a  ## 接收到免密传输的文件
.  ..  a.txt  testdir

 

8、删除pc2主机/root/dir2/目录文件及目录

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ls -a
.  ..  a.txt  testdir
[root@pc2 dir2]# rm -rf *
[root@pc2 dir2]# ls -a
.  ..

 

9、在pc2主机测试从pc1主机免密下载文件及目录

[root@pc2 dir2]# pwd
/root/dir2
[root@pc2 dir2]# ls -a
.  ..
[root@pc2 dir2]# scp root@192.168.3.20:/root/dir1/a.txt ./
a.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc2 dir2]# ls -a
.  ..  a.txt
[root@pc2 dir2]# scp -r root@192.168.3.20:/root/dir1/testdir ./
b.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc2 dir2]# ls -a  ## 可以实现免密下载文件及目录
.  ..  a.txt  testdir

 

注:后经过测试,主机在不联外网的情况下,但是两台主机之间可以ping通的情况下也可以实现scp传输文件,两台主机之间不能ping通的情况下,不能实现传输。

posted @ 2020-11-04 15:15  小鲨鱼2018  阅读(859)  评论(0编辑  收藏  举报