ssh配置免密登入+rsync免密同步文件

 

ssh配置免密登入

1、root用户登入,并创建test账号

        useradd test -d /home/userhome/test
        修改密码
        passwd test

2、test账号登入生成SSH密钥

        [test@xxxxxxxxxxx ~]$ ssh-keygen 
        Generating public/private rsa key pair.
        Enter file in which to save the key (/home/userhome/test/.ssh/id_rsa): 
        Created directory '/home/userhome/test/.ssh'.
        Enter passphrase (empty for no passphrase): 
        Enter same passphrase again: 
        Your identification has been saved in /home/userhome/test/.ssh/id_rsa.
        Your public key has been saved in /home/userhome/test/.ssh/id_rsa.pub.
        The key fingerprint is:
        SHA256:xxxxxxxxxCkBXX/i9JQMHGioopTrI test@xxxxxxxxxxx
        The key's randomart image is:
        +---[RSA 2048]----+
        | . o....o.o*..oxx|
        |  = ... .+..=  =-|
        | P o .  o..  .oo*|
        |    .    +  .o----|
        |        S +...+o+|
        |         o oo o+.|
        |          o. o . |
        |         . ..    |
        |          .      |
        +----[SHA256]-----+

3、将SSH公钥复制到客户机        

        说明:客户机192.168.1.81上面也需要创建test账号
        [test@xxxxxxxxxxx ~]$ ssh-copy-id [email protected] -p 22
        /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/userhome/test/.ssh/id_rsa.pub"
        The authenticity of host '192.168.1.81 (192.168.1.81)' can't be established.
        ECDSA key fingerprint is SHA256:+aRrV/sdfsdfsdfsdfsdfmhHNhbKb7g.
        ECDSA key fingerprint is MD5:xxxxxxx.
        Are you sure you want to continue connecting (yes/no)? yes
        /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
        [email protected]'s password: 
        
        Number of key(s) added: 1
        
        Now try logging into the machine, with:   "ssh -p '22' '[email protected]'"
        and check to make sure that only the key(s) you wanted were added.

4、免密登入192.168.1.81客户机

        [test@xxxxxxxxxxx ~]$ ssh [email protected] -p 22 -v
          Welcome to xx xx xx Compute Service !

         加上-v 可以调试ssh,尤其遇到登入不了的问题时

5、本机也可以配置免密登入 

    
        5.1、拷贝公钥
            ssh-copy-id [email protected] -p 22
        5.2、直接登入
            ssh [email protected] -p 22 


  6、退出登入       

        exit

rsync 通过ssh免密码方式同步文件    

    rsync -avz -e "ssh -p 22"  ~/test.txt  [email protected]:/home/userhome/test/
   
    rsync -avz -e "ssh -p 22" ~/test.txt  [email protected]:/home/userhome/test/

猜你喜欢

转载自blog.csdn.net/pengwupeng2008/article/details/108732667
今日推荐