探索rsync远程同步和SSH免密登录的奥秘

03-08 1096阅读 0评论

目录

  • 集群分发脚本xsync
    • scp(secure copy)安全拷贝
    • rsync 远程同步工具
    • 集群分发脚本
    • SSH免密登录
      • 免密登录原理
      • SSH免密登录配置
        • 生成公钥和私钥
        • 授权
        • 测试

          在现代科技飞速发展的时代,数据的备份和迁移成为了一个重要的课题。其中,rsync远程同步和SSH免密登录成为了程序员们常用的工具和技巧。它们能够帮助我们高效地进行文件同步和管理,使数据的传输更加快速和安全。

          探索rsync远程同步和SSH免密登录的奥秘,探索rsync远程同步和SSH免密登录的奥秘,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,管理,第1张
          (图片来源网络,侵删)

          在本篇文章中,我们将深入探索rsync远程同步和SSH免密登录的奥秘。我们将介绍如何配置SSH免密登录,以及如何使用rsync来进行文件的远程同步。

          集群分发脚本xsync

          scp(secure copy)安全拷贝

          (1)定义:scp可以实现服务器与服务器之间的数据拷贝

          (2)基本语法:

          scp -r $pdir/$fname $user@$host:$pdir/$fname
          # scp 命令
          # -r 递归
          # $pdir/$fname 要拷贝的文件路径/名称
          # $user@$host:$pdir/$fname 目的地用户名@主机:目的地路径/名称
          

          (3)案例:

          在hadoop102上,把数据拷贝到hadoop103

          scp -r jdk1.8.0_371/ root@hadoop103:/opt/module

          探索rsync远程同步和SSH免密登录的奥秘,探索rsync远程同步和SSH免密登录的奥秘,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,管理,第2张
          (图片来源网络,侵删)

          在hadoop103上,拉取hadoop102的数据

          scp -r root@hadoop102:/opt/module/hadoop-3.2.4 ./

          rsync 远程同步工具

          rsync是一个功能强大的文件同步工具,它能够通过比较源和目标文件的内容差异,只传输差异部分,从而大幅度提升文件传输的效率。这种差异传输的机制,使得rsync在大规模数据备份和迁移中得到了广泛的应用。它能够通过多种传输协议,包括本地文件系统、SSH和RSYNC等,来实现跨平台和跨网络的文件同步。

          rsync主要用于备份和镜像,具有速度快、避免复制相同的内容和支持符号链接的特点。

          rsync比 scp 复制文件速度要快,rsync只对差异化文件做更新,scp是把所有文件都复制过去

          1. 基本语法
          rsync -av $pdir/$fname $user@host:$pdir/$fname
          # rsync 命令
          # -av   -a 归档拷贝   -v 显示复制过程
          # $pdir/$fname 要拷贝的文件路径/名称
          # $user@host:$pdir/$fname 目的地用户名@主机目的地路径/名称
          
          1. 案例:

            在hadoop102上,同步hadoop102上的数据到hadoop103

            探索rsync远程同步和SSH免密登录的奥秘,探索rsync远程同步和SSH免密登录的奥秘,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,管理,第3张
            (图片来源网络,侵删)

            rsync -av hadoop-3.2.4/ root@hadoop103:/opt/module/hadoop-3.2.4/

          集群分发脚本

          循环复制文件到所有节点的相同目录下

          rsync命令原始拷贝rsync -av /opt/module root@hadoop103:/opt

          期望脚本使用方式:xsync 要同步的文件名称

          期望脚本在任何路径都能使用(脚本放在声明了全局环境变量的路径)

          [amo@hadoop102 ~]$ echo $PATH
          /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_371/bin:/opt/module/hadoop-3.2.4/bin:/opt/module/hadoop-3.2.4/sbin:/home/amo/.local/bin:/home/amo/bin
          

          home目录下创建bin文件夹,并在该文件夹下创建xsync文件vim xsync (名字随便起)

          #!/bin/bash
          #1.判断参数个数
          if [ $# -lt 1 ]
          then
              echo Not Enough Arguement!
              exit;
          fi
          #2.遍历集群所有机器
          for host in hadoop102 hadoop103 hadoop104
          do
              echo ========= = $host =============
              #3.遍历所有目录,一个个发送
              for file in $@
              do
                  #4.判断文件是否存在
                  if [ -e $file ]
                      then
                          #5.获取父目录
                          pdir=$(cd -P $(dirname $file); pwd)
                          #6.获取当前文件的名称
                          filename=$(basename $file)
                          ssh $host "mkdir -p $pdir"
                          rsync -av $pdir/$filename $host:$pdir
                      else
                          echo $file does not exists!
                  fi
              done
          done
          

          修改脚本 xsync 具有执行权限

          chmod 777 xsync

          测试脚本

          xsync /bin

          将脚本复制到/bin中,以便全局调用

          sudo cp xsync /bin/

          同步环境变量配置(root所有者)

          sudo ./bin/xsync /etc/profile.d/my_env.sh

          环境变量生效source /etc/profile

          SSH免密登录

          SSH免密登录则是一种安全的通信协议,它能够建立起安全的连接,从而实现在远程服务器上执行命令,而无需手动输入密码。通过使用公钥和私钥的加密方式,SSH免密登录能够防止密码被窃取,并提供更高的安全性。

          免密登录原理

          1. A服务器通过ssh-keygen -t rsa命令生成密钥对(公钥和私钥)
          2. A服务器通过ssh-copy-id 服务器B命令将公钥拷贝到B服务器
          3. A服务器ssh访问B服务器(数据用私钥加密)
          4. B服务器接收到数据后,去授权的key中查找A服务器的公钥,并解密数据
          5. 将采用A公钥加密后的数据返回给A服务器

          SSH免密登录配置

          #切换到home目录下
          cd ~ 
          # 查看home目录下的所有文件(包括隐藏文件)
          ll -al 
          # 切换到.ssh文件夹下
          cd .ssh
          # 生成公钥和私钥
          ssh-keygen -t rsa
          # 授权给另一个服务器
          ssh-copy-id hadoop103
          

          生成公钥和私钥

          # 生成公钥和私钥
          [root@hadoop102 .ssh]# ssh-keygen -t rsa
          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:
          SHA256:pCATVCsPvIqYqZMz0KYnHxIE5V7gsqz+MU41co6SkNY root@hadoop102
          The key's randomart image is:
          +---[RSA 2048]----+
          |.o=..            |
          |.+ o .           |
          |..O +   .        |
          |o=.O . o         |
          |=+oE.+. S        |
          |B=+ * .          |
          |X*.= .           |
          |X.=.o            |
          |.Ooo             |
          +----[SHA256]-----+
          

          授权

          将生成的公钥通过命令拷贝到你要授权的服务器

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

          测试

          [amo@hadoop102 ~]$ ssh hadoop103
          Last login: Fri Mar  1 19:40:22 2024 from 192.168.1.1
          [amo@hadoop103 ~]$ 
          

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1096人围观)

还没有评论,来说两句吧...

目录[+]