Linux 之 SSH免密登陆之一 基础解析

来源:互联网 发布:mac重新安装cocoapods 编辑:程序博客网 时间:2024/06/11 04:25
  • Linux 基本操作
  • 基本操作分析

1. Linux基本操作


1.1. 基本操作

有2台电脑,之间会经常性的传输文件,或者是集群内经常性的传输文件,这个时候配置ssh免密登陆就非常重要了。因为多集群的话传递到10台机器之间,还需要输入密码就是非常痛苦的一件事情了。基本操作大致是一样了,但是有很多需要注意的地方,我讲其分成2篇进行概述了。

  • 有A,B两台机(Linux/unix), 要想从A用ssh远程登录到B上(假设各自的IP,A:192.168.1.100;B:192.168.1.104)。

  • 在A机上,用ssh-keygen -t rsa命令生成公钥,注意这里一直回车就是了(三次回车)。好了,这时在 ~/.ssh/下就已经生成id_rsaid_rsa.pub两个文件了。

  • 或者使用命令ssh-keygen -t rsa -P '' -f /etc/ssh/ssh_host_dsa_key-t表示key的类型,rsa表示key类型; -P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车; -f表示查找的私钥和公钥生成的位置。

  • 还在A机上,将刚才生成的id_rsa.pub文件复制到B机上的authorithy_keys文件内; 具体的命令如下:
    pub_key=$(cat "/root/.ssh/id_rsa.pub") ; ssh root@192.168.100.104 " mkdir -p /root/.ssh ; echo "$pub_key" >> /root/.ssh/authorithy_keys ; echo "The pub_key is configed."; "

  • 如果配置成功的话,那么就可以直接ssh免密登陆了;


1.2. 详细操作

上文讲的比较的抽象,下面将整个过程列出来;

# step1: 生成id_rsa 和 id_rsa.pub文件ssh-keygen -t rsa -P ''# step2: 2.1. 读取生成的文件 2.2. 传输生成好的文件pub_key=$(cat "/root/.ssh/id_rsa.pub") ; ssh root@192.168.100.67 " mkdir -p /root/.ssh ; echo "$pub_key" >> /root/.ssh/authorithy_keys ; echo "The pub_key is configed."; " ;# step3: 验证ssh root@192.168.100.67# 注意1 如果无法使用的话 一般是文件夹的权限问题,登陆目标服务器,更改文件的权限即可chmod 700 /root/.ssh chmod 600 /root/.ssh/authorized_keys# 注意2 以上的操作都是root配置root用户的免密登陆;如果需要配置其它用户,那么它们路径改成相应的的$HOME路径;(cd ~; pwd; 查看根路径)

1.3. 注意事项

  • ssh-keygen -t rsa 生成公钥时一直回车不要输入密码,就是空密码。

  • 要用哪个用户远程登录就把id_rsa.pub复制到用户对应路径下,如:root用户就复制到/root/下; andychen用户就复制到/home/andychen/下,不要混了。

  • B机上的文件权限: .ssh文件夹(700);authorized_keys(600/644)。
    可以通过chmod 700 /root/.sshchmod 600 /root/.ssh/authorized_keys 进行权限赋值;

  • 如果是正常的系统,例如ubuntu 16.04就可以成功了,那么如果是RedHat 6.5 或者 CenteOs 7 可以看一下这篇的解决方法;


2. 原理简析


2.1.1 基本原理

主要经历了如下的一系列过程:

图解,server A免登录到server B:

  • step1: 在A上生成公钥私钥。
  • step2: 将公钥拷贝给server B,要重命名成authorized_keys(从英文名就知道含义了)
  • step3: Server A向Server B发送一个连接请求。
  • step4: Server B得到Server A的信息后,在authorized_key中查找,如果有相应的用户名和IP,则随机生成一个字符串,并用Server A的公钥加密,发送给Server A。
  • step5: Server A得到Server B发来的消息后,使用私钥进行解密,然后将解密后的字符串发送给Server B。Server B进行和生成的对比,如果一致,则允许免登录。
  • 总之:A要免密码登录到B,B首先要拥有A的公钥,然后B要做一次加密验证。对于非对称加密,公钥加密的密文不能公钥解开,只能私钥解开。

  • 简单说就是 1. A登陆B ; 2.B获取A公钥加密,并将加密后的字符串发给A ; 3 A通过自己的私钥解密字符串,并发送给B ; 4. B将A发过来的解密结果和生成的结果进行对比,如果相同就允许免密登陆了;


2.1.2 log简析

配置ssh免密登陆后,我们可以通过ssh -v username@ip查看链接过程中的log,其log信息如下所示:

sh-3.2# ssh -v root@192.168.100.67OpenSSH_6.9p1, LibreSSL 2.1.8debug1: Reading configuration data /etc/ssh/ssh_configdebug1: /etc/ssh/ssh_config line 21: Applying options for *debug1: Connecting to 192.168.100.67 [192.168.100.67] port 22.debug1: Connection established.debug1: permanently_set_uid: 0/0debug1: identity file /var/root/.ssh/id_rsa type 1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_rsa-cert type -1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_dsa type -1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_dsa-cert type -1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_ecdsa type -1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_ecdsa-cert type -1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_ed25519 type -1debug1: key_load_public: No such file or directorydebug1: identity file /var/root/.ssh/id_ed25519-cert type -1debug1: Enabling compatibility mode for protocol 2.0debug1: Local version string SSH-2.0-OpenSSH_6.9debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000debug1: Authenticating to 192.168.100.67:22 as 'root'debug1: SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex: server->client aes128-ctr umac-64@openssh.com nonedebug1: kex: client->server aes128-ctr umac-64@openssh.com nonedebug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sentdebug1: got SSH2_MSG_KEX_DH_GEX_GROUPdebug1: SSH2_MSG_KEX_DH_GEX_INIT sentdebug1: got SSH2_MSG_KEX_DH_GEX_REPLYdebug1: Server host key: ssh-rsa SHA256:mxdRr3cfr86jDyAS8l7MoxfsM0GQqJkAlp0G2XwHBLMdebug1: Host '192.168.100.67' is known and matches the RSA host key.debug1: Found key in /var/root/.ssh/known_hosts:15debug1: SSH2_MSG_NEWKEYS sentdebug1: expecting SSH2_MSG_NEWKEYSdebug1: SSH2_MSG_NEWKEYS receiveddebug1: SSH2_MSG_SERVICE_REQUEST sentdebug1: SSH2_MSG_SERVICE_ACCEPT receiveddebug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,passworddebug1: Next authentication method: publickeydebug1: Offering RSA public key: /var/root/.ssh/id_rsadebug1: Server accepts key: pkalg ssh-rsa blen 279debug1: Authentication succeeded (publickey).Authenticated to 192.168.100.67 ([192.168.100.67]:22).debug1: channel 0: new [client-session]debug1: Requesting no-more-sessions@openssh.comdebug1: Entering interactive session.debug1: Sending environment.debug1: Sending env LANG = zh_CN.UTF-8Last login: Wed May 17 15:37:15 2017 from 192.168.100.63

参考文献

[1] SSH免密码远程登录Linux
[2] ssh免密码登录