zoukankan      html  css  js  c++  java
  • Linux私钥公钥对认证身份,实现免密登陆

      文章目录

    一、情景
    问题描述:
    二、前提
    三、举例
    四、步骤
    1、登录 192.168.1.151
    2、将公钥追加到远程机器上
    3、这时候 从192.168.3.151 登录到192.168.3.152 就不需要密码了。
    4、若需要实现双向的,则继续执行以下步骤:
    5、注意事项

    一、情景
    问题描述:

    在使用java 或者 直接在linux系统中登录时,会有提示消息:是否保存地址信息,和输入密码。
    认证身份有“私钥和公钥对”“用户名和密码”完成两台主机之间的身份认证。
    为了解除这些繁琐的交互式命令和提高安全性,实现免密登录。下面来总结一下。
    二、前提

    所有的服务器 /etc/ssh/sshd_config文件以下三行都需要未被注释。

    1:RSAAuthentication yes
    2:PubkeyAuthentication yes
    3:AuthorizedKeysFile .ssh/authorized_keys
    三、举例
    两台服务器
    192.168.3.151
    192.168.3.152
    免密登录是分用户的,以下是以root用为例,.ssh目录是每个用户的家目录下。
    四、步骤
    1、登录 192.168.1.151

    执行命令 ssh-keygan -t rsa,一路回车到底。
    [root@localhost ~]# ssh-keygen
    将会在家目录 /root/.ssh下生成私钥 id_rsa和公钥id_rsa.pub。
    2、将公钥追加到远程机器上

    将本机(192.168.1.151)的公钥(id_rsa.pub)追加到 192.168.3.152中的authorized_keys(/root/.ssh/authorized_keys)文件中。
    命令如下:

    [root@host1 ~]# ls /root/.ssh/
    authorized_keys id_rsa id_rsa.pub known_hosts

    1:// 在本地151上执行,将文件传输到152上
    2:scp /root/.ssh/id_rsa.pub root@192.168.3.152:/root

    3:// 在152机器上执行,/root/目录下,将文件追加到authorized_keys 中
    4:cat id_rsa.pub>>/root/.ssh/authorized_keys
    在151上登录152,已经免密了
    [root@localhost ~]# ssh 192.168.3.152
    3、这时候 从192.168.3.151 登录到192.168.3.152 就不需要密码了。
    4、若需要实现双向的,则继续执行以下步骤:

    登录到192.168.3.152 执行上面相似的步骤即可。
    5、注意事项

    修改完/etc/ssh/sshd_config文件需要重启sshd服务。(systemctl restart sshd)
    追加公钥到服务器authorized_keys中,千万不要覆盖。
    .ssh目录的权限为700,authorized_keys权限为600。
    ---------------------

    普通用户向root 用户传公钥:
    [alice@nas ~]$ ssh-keygen
    [alice@nas ~]$ ls -a
    . .. .bash_history .bash_logout .bash_profile .bashrc .ssh
    [alice@nas ~]$ ls -a .ssh/
    . .. id_rsa id_rsa.pub known_hosts

    [alice@nas ~]$ ssh-copy-id root@192.168.40.12
    /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@192.168.40.12's password:

    Number of key(s) added: 1

    Now try logging into the machine, with: "ssh 'root@192.168.40.12'"
    and check to make sure that only the key(s) you wanted were added.
    [alice@nas ~]$ ssh root@192.168.40.12
    Last login: Thu Oct 24 09:10:58 2019 from 192.168.40.1

    使用另一个没有免密得普通用户登录 ,需要输入密码
    [hehe@nas script]$ ssh root@192.168.40.12
    The authenticity of host '192.168.40.12 (192.168.40.12)' can't be established.
    ECDSA key fingerprint is f0:fd:39:de:c7:b2:b1:4f:9c:f5:b7:f0:ed:5c:fe:28.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.40.12' (ECDSA) to the list of known hosts.
    root@192.168.40.12's password:
    我是用root登陆的,显示不行,那是因为我关闭了root的登陆权限,如果你要使用root登陆可以执行
    vi /etc/ssh/sshd_config


    修改为PubkeyAuthentication yes(如果前面有#,就去掉#)
    file://C:UsersADMINI~1AppDataLocalTempct_tmp/1.pngfile://C:UsersADMINI~1AppDataLocalTempct_tmp/2.png








  • 相关阅读:
    GIT DIFF生成.PATCH文件
    C++中static关键字作用总结
    模版与泛型编程
    模版以及全特化,偏特化
    重载运算与类型转换
    GDB调试工具(待整理)
    面向对象(primer)
    7种获取高度的区别
    把页面主体内容限定在安全区内
    ios浏览器下载,apple-itunes-app
  • 原文地址:https://www.cnblogs.com/huangjiaxuan/p/12850334.html
Copyright © 2011-2022 走看看