zoukankan      html  css  js  c++  java
  • centos平台scp通过密钥远程复制文件(免密登录)

    一,说明:两台机器的平台和ip

    1,a服务器:

    centos8:ip:121.122.123.47

    版本

    [root@yjweb ~]# cat /etc/redhat-release
    CentOS Linux release 8.0.1905 (Core) 

    2,    j服务器:

    centos6:ip:121.122.7.134

    版本:

    [root@os3 ~]# cat /etc/redhat-release
    CentOS release 6.10 (Final)

    我们的目示是通过scp程序把a服务器上的文件复制到j服务器上

    说明:ip地址仅供演示,非真实

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/

     说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,在a服务器上生成密钥:

    1,生成密钥

    [root@yjweb ~]# ssh-keygen -t rsa

    说明:中间一路回车即可,

    不要设置密码

    说明:-t参数可以指定四种算法类型 

    [-t dsa | ecdsa | ed25519 | rsa]

    我们选择 rsa

    说明:查看参数可以使用通用的帮助命令:

    [root@yjweb ~]# man ssh-keygen 

    2,密钥生成后,可以从用户的home目录下.ssh目录看到

    [root@yjweb ~]# ls .ssh/
    authorized_keys  id_rsa  id_rsa.pub  known_hosts

    三,从a服务器复制公钥到j服务器

    1,用ssh-copy-id命令复制公钥到j服务器上

    [root@yjweb ~]# ssh-copy-id -i .ssh/id_rsa.pub root@121.122.7.134
    /bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
    /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@121.122.7.134's password:
    Number of key(s) added: 1
    Now try logging into the machine, with:   "ssh 'root@121.122.7.134'"
    and check to make sure that only the key(s) you wanted were added.

    说明:ssh-copy-id属于这个rpm包,如发现不存在可以通过yum安装

    [root@yjweb ~]# whereis ssh-copy-id
    ssh-copy-id: /usr/bin/ssh-copy-id /usr/share/man/man1/ssh-copy-id.1.gz
    [root@yjweb ~]# rpm -qf /usr/bin/ssh-copy-id
    openssh-clients-8.0p1-4.el8_1.x86_64

    2,登录到j服务器,查看authorized_keys

    [root@os3 ~]# more .ssh/authorized_keys
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDiZNKuLsJqi0M......

    可以看到我们在a服务器的公钥已经成功添加到了j服务器的 authorized_keys文件上

    四,从a服务器用scp复制一个文件到j服务器

          注意看是否会提示输入j服务器的密码

    1,检查j服务器上的目标文件夹

    [root@os3 test]# ll /data/dev/think_file/test/
    total 0

    2,在a服务器上执行scp命令

    [root@yjweb orig]# scp -P 22 /data/web/think_file/cover/orig/1/10.gif root@121.122.7.134:/data/dev/think_file/test/

    说明:

    -P 22:   在此处指定目标服务器的端口
    root 登录目标服务器用的账号
    121.122.7.134 目标服务器用的ip
    /data/dev/think_file/test/  在目标服务器上的路径

    3,回到j服务器,再次检查测试用的目标文件夹

    [root@os3 test]# ll /data/dev/think_file/test/
    total 1300
    -rw-r--r-- 1 root root 1330533 Mar 16 14:54 10.gif

    五,如何在远程服务器上创建目录

    [root@yjweb ~]# ssh -p 22 root@121.122.7.134 "mkdir -p /data/dev/think_file_test/cover/orig/1/"

    说明: 

    -p : 用来指定目标服务器的端口号

    六,如何复制一整个目录到目标机器?

    1,加参数 -r即可

    [root@yjweb web]# scp -P 22 -r /data/web/think_file/cover/orig/1/ root@121.122.7.134:/data/dev/think_file/test/
    7.gif                                                                        100%  548KB 489.4KB/s   00:01
    9.gif                                                                        100%  542KB 700.2KB/s   00:00
    11.png                                                                       100%  997KB   1.4MB/s   00:00
    10.gif                                                                       100% 1299KB   1.3MB/s   00:00
    8.gif                                                                        100%  440KB 448.9KB/s   00:00

    2,一点值得说明的知识:

    -r      Recursively copy entire directories.  Note that scp follows symbolic links encountered in the tree traversal.

    -r 用来复制整个目录

    需要注意的是scp会把符号链接下的文件也复制过来

    如何避免这个问题?

    可以先把包含符号链接的源目录打包成tar包,
    远程复制tar包过来后再解开
  • 相关阅读:
    预备作业02 : 体会做中学(Learning By Doing)
    7-1 货币转换
    秋季学校总结
    人生路上对我影响最大的三个老师
    自我介绍
    2019寒假作业3(抓老鼠啊~亏了还是赚了?)编程总结
    人生路上对我影响最大的三位老师
    自我介绍
    秋季学期学习总结
    7-1 抓老鼠啊~亏了还是赚了?
  • 原文地址:https://www.cnblogs.com/architectforest/p/12508919.html
Copyright © 2011-2022 走看看