zoukankan      html  css  js  c++  java
  • 用公共key实现无密码ssh

    1、在主服务器master下生成密钥

    $ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa

    2、将密钥放在 ~/.ssh/authorized_keys

    $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

    3、对生成的文件authorized_keys授权

      chmod 600 ~/.ssh/authorized_keys

    4、编辑sshd_config文件,将下面三列#去掉

     vim /etc/ssh/sshd_config

     RSAAuthentication yes

    PubkeyAuthentication yes

    AuthorizedKeysFile     .ssh/authorized_keys

    5、重启sshd 服务(可省略)

       service  sshd  restart

    6、验证无验证登陆

    ssh localhost

    7  配置master无密钥登陆slave(以此可以配置master无密钥登陆slaveX

    注释:root为root用户,master为主机名

    [root@slaver ~]# scp -r root@master :/root/.ssh/id_dsa.pub /root/.ssh/slaver.pub

     [root@slaver ~]# cat ~/.ssh/slaver.pub >> ~/.ssh/authorized_keys

    chmod 600~/.ssh/authorized_keys

    vim /etc/ssh/sshd_config

    RSAAuthentication yes

    PubkeyAuthentication yes

    AuthorizedKeysFile     .ssh/authorized_keys

    service sshd restart

     

    更好的自动ssh登录,用公共key实现无密码ssh

    解决~/.ssh/known_hosts 过期问题。
    bash + expect

    bash:ssh.sh

    1. #!/bin/bash
    2. help(){
    3.     echo "usage: [password]"
    4. }
    5. -z "$1" ] &{ help; exit 1|{
    6.     case $1 in
    7.         -*) help; exit ;;
    8.         *) ip=$1;;
    9.     esac
    10. }
    11. shift
    12. ssh.exp "$ip" "$@"
    13. [ "$?" = "1" ] && ssh-keygen -R $ip && ssh_ivie.exp "$ip" "$@"


    expec
    ssh.exp

    1. #!/usr/bin/expect -f
    2. proc help {{
    3. puts {usage<ivie_ip[password]}
    4. }
    5. if {$argc<1{ help ; exit}
    6. set ip [ lindex $argv 0 ]
    7. set password rootroot
    8. if {$argc==2{ set password [lindex $argv 1] }
    9. # close the output
    10. log_user 0
    11. set timeout 30
    12. spawn ssh -XY root@$ip
    13. expect {
    14.     -re ".*:~ # " {}
    15.     "Password: " { send "$password " }
    16.     "(yes/no)?" {send "yes "; exp_continue}
    17.     "Host key verification failed" { send_user "run: ssh-keygen -R $ip" ; exit 1 }
    18.     timeout {puts "check your ip: $ip"; exit 2}
    19.     eof { puts "check your ip: $ip" ;exit 3 }
    20. }
    21. interact


    我测试过在一个expect中完成,但是没有成功。还望有谁能够完成,给予指教。

    用公共key实现无密码ssh
    在HPC上,从server登陆到各个node都需要输密码,很麻烦,能不能不输密码呢。
    
    需要用到的技术就是ssh中public key的authorized。 具体做法分如下几步:
    
    用ssh-keygen生成一个publc key;
    ssh-keygen -t rsa
    中间会提醒输入id_rsa文件的位置和pass-phrase,可以一路回车。id_rsa在~/.ssh/中产生两个文件id_rsa和id_rsa.pub,一个是私有密钥,一个是公共密钥。
    
    由于HPC中共享硬盘,直接将其中的public key(一般默认为~/.ssh/id_rsa.pub)在复制一份并命名为authorized_keys
    
    如果运气好,做完第二步就可以实现不输入密码登陆了。不过实际实施过程中,还有很多种发生意外的可能(经亲身验证)。尤其需要注意的问题是各个文件/文件夹权限的问题。
    
    合理的权限列表如下:
    
    id_rsa 600
    id_rsa.pub 644
    ~/.ssh/ 755
    同时要注意的就是,$HOME,也就是~不能是777的权限,在这个问题上卡了三四个小时啊。
  • 相关阅读:
    vue store获取值时 Computed property "activeTag" was assigned to but it has no setter.
    深拷贝实现方法以及问题
    mac 中git操作账号的保存与删除
    解决Ubuntu编译内核uImage出现问题“mkimage” command not found
    ubuntu修改主机名后无法解析主机
    多行宏定义中的注释问题
    uboot启动阶段修改启动参数方法及分析
    ubuntu nfs服务器配置
    mount/umount命令详解
    Ubuntu下vsftp安装和配置
  • 原文地址:https://www.cnblogs.com/timssd/p/4180701.html
Copyright © 2011-2022 走看看