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

  • 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.67
OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /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/0
debug1: identity file /var/root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /var/root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: Authenticating to 192.168.100.67:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr umac-64@openssh.com none
debug1: kex: client->server aes128-ctr umac-64@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:mxdRr3cfr86jDyAS8l7MoxfsM0GQqJkAlp0G2XwHBLM
debug1: Host '192.168.100.67' is known and matches the RSA host key.
debug1: Found key in /var/root/.ssh/known_hosts:15
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /var/root/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: 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.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = zh_CN.UTF-8
Last login: Wed May 17 15:37:15 2017 from 192.168.100.63

参考文献

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值