linux系统scp远程传输命令

以下实验实现PC1主机和PC2主机之间的文件传输

PC1:192.168.10.10

PC2:192.168.10.20

 

1、测试PC2主机和PC1主机之间的网络连通性

[root@PC2 network-scripts]# ifconfig | head -n 3
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.20  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fe25:bb3e  prefixlen 64  scopeid 0x20<link>
[root@PC2 network-scripts]# ping -c 3 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.361 ms
64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.201 ms
64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.218 ms

--- 192.168.10.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.201/0.260/0.361/0.071 ms

 

2、在PC2主机和PC1主机中分别创建测试目录

[root@PC2 network-scripts]# mkdir -p /home/pc2dir
[root@PC2 network-scripts]# cd /home/pc2dir/
[root@PC2 pc2dir]# ls
[root@PC1 network-scripts]# mkdir -p /home/pc1dir
[root@PC1 network-scripts]# cd /home/pc1dir/
[root@PC1 pc1dir]# ls

 

3、在PC2测试目录/home/pc2dir目录中创建测试文件,并向PC1主机测试目录/home/pc1dir传输

[root@PC2 pc2dir]# ls
[root@PC2 pc2dir]# seq 3 > a.txt
[root@PC2 pc2dir]# ls
a.txt
[root@PC2 pc2dir]# cat a.txt 
1
2
3
[root@PC2 pc2dir]# scp a.txt root@192.168.10.10:/home/pc1dir
The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established.
ECDSA key fingerprint is 0d:69:cb:ad:61:42:f3:f7:7b:93:4b:b4:af:83:4d:8e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts.
root@192.168.10.10's password:        ## 此处输入PC1主机root密码
a.txt                                        100%    6     0.0KB/s   00:00    

 

4、PC1主机中验证传输效果

[root@PC1 pc1dir]# pwd
/home/pc1dir
[root@PC1 pc1dir]# ls
a.txt
[root@PC1 pc1dir]# cat a.txt 
1
2
3

 

5、删除PC2主机测试目录中测试文件,从PC1主机中测试目录/home/pc1dir远程传输至本地测试目录

[root@PC2 pc2dir]# rm -f *
[root@PC2 pc2dir]# ls
[root@PC2 pc2dir]# scp root@192.168.10.10:/home/pc1dir/a.txt ./
root@192.168.10.10's password: 
a.txt                                        100%    6     0.0KB/s   00:00    
[root@PC2 pc2dir]# ls
a.txt
[root@PC2 pc2dir]# cat a.txt 
1
2
3

 

实现远程免密传输

6、PC2主机中生成公钥、秘钥文件

[root@PC2 pc2dir]# ssh-keygen    ## 一路回车
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
a5:fb:90:f4:df:c7:8c:a0:6a:e1:1e:d7:96:c4:7a:61 root@PC2
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|          .      |
|         o .     |
|        S   E    |
|       ..+ =.o   |
|       .=.+.=. + |
|        o=.+ .. +|
|       oo.. . .. |
+-----------------+

 

7、将PC2主机中公钥文件传输至PC1

[root@PC2 pc2dir]# ssh-copy-id 192.168.10.10
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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.10.10's password:    ## 此处输入PC1主机root密码

Number of key(s) added: 1

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

 

8、清空PC1主机测试目录

[root@PC1 pc1dir]# pwd
/home/pc1dir
[root@PC1 pc1dir]# rm -f *
[root@PC1 pc1dir]# ls

 

9、PC2主机远程向PC1主机传输文件

[root@PC2 pc2dir]# scp a.txt root@192.168.10.10:/home/pc1dir   ## 可以免密传输
a.txt                                        100%    6     0.0KB/s   00:00    

 

10、清空PC2测试目录,从PC1主机传输文件至本地

[root@PC2 pc2dir]# rm -f *
[root@PC2 pc2dir]# ls
[root@PC2 pc2dir]# scp root@192.168.10.10:/home/pc1dir/a.txt ./        ## 可以免密传输
a.txt                                        100%    6     0.0KB/s   00:00    
[root@PC2 pc2dir]# ls
a.txt
[root@PC2 pc2dir]# cat a.txt 
1
2
3

 

以上实验实现了两台主机之间使用scp命令的文件传输以及免密传输。

posted @ 2020-12-19 15:30  小鲨鱼2018  阅读(347)  评论(0编辑  收藏  举报