当前位置: 代码网 > it编程>编程语言>Java > 探索rsync远程同步和SSH免密登录的奥秘

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

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


集群分发脚本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

在hadoop103上,拉取hadoop102的数据
scp -r root@hadoop102:/opt/module/hadoop-3.2.4 ./

rsync 远程同步工具

  1. 基本语法
rsync -av $pdir/$fname $user@host:$pdir/$fname
# rsync 命令
# -av   -a 归档拷贝   -v 显示复制过程
# $pdir/$fname 要拷贝的文件路径/名称
# $user@host:$pdir/$fname 目的地用户名@主机目的地路径/名称
  1. 案例:
    在hadoop102上,同步hadoop102上的数据到hadoop103
    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免密登录

免密登录原理

  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 ~]$ 
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com