一般使用SSH进行远程登录时需要提供密码,这也是我们所熟知的一种方式。

  另外,就是通过公钥登录的方式,本文将简要介绍公钥登录的两种方法,建议使用方法二。本文也将简单演示公钥登录过程,以及强制使用公钥和密码的双因子认证。

公钥登录:法一

Step 1:创建公钥/私钥对ssh-keygen

$ ssh-keygen
Generating public/private rsa key pair.
...
$ ls
id_rsa id_rsa.pub known_hosts

Step 2:将id_rsa.pub上传到要远程登录到的机器上

$ scp id_rsa.pub root@142.93.198.56:/tmp
root@142.93.198.56's password:
id_rsa.pub % .5KB/s :

Step 3:将公钥添加到authorized_keys中

  首先,远程登录到目标机器,在远程进行操作。

$ ssh root@142.93.198.56
...
root@ubuntu-s-1vcpu-1gb-nyc1-:~# cd /tmp/
root@ubuntu-s-1vcpu-1gb-nyc1-:/tmp# cat id_rsa.pub >> ~/.ssh/authorized_keys

Step 4:更改文件权限

root@ubuntu-s-1vcpu-1gb-nyc1-:/tmp# chmod  ~/.ssh/authorized_keys

Step 5:查看配置

  查看和更改配置文件:/etc/ssh/sshd_config

root@ubuntu-s-1vcpu-1gb-sfo2-:~# vim /etc/ssh/sshd_config
PasswordAuthentication yes      # 口令登录
RSAAuthentication yes         # RSA认证
PubkeyAuthentication yes       # 公钥登录

  然后重启sshd服务。如果不想使用口令登录,可以修改PasswordAuthentication 为no。不过还是建议保留这项配置,如果一不下心执行了一下ssh-keygen命令,那这台远程服务器就真的离你有点远了。

Step 6:ssh公钥登录

  现在便能使用私钥登录到远程机器了。

$ ssh -i id_rsa root@142.93.198.56
Welcome to Ubuntu 16.04. LTS (GNU/Linux 4.4.--generic x86_64) * Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud packages can be updated.
updates are security updates. New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

  vps :142.93.198.56仅供测试,已销毁。

公钥登录:法二

  在接触Hadoop环境搭建的过程中,由于Hadoop集群之间是使用公钥直接进行数据传输。接触和使用了ssh-copy-id命令,该命令可轻松完成上述方法一的所有步骤。

root@kali:~# ssh-keygen                      # 生成公钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+E2PL7KFGu9pdzg9gEkg5OhMToGQxvMipMkXgBNub/k root@kali
The key's randomart image is:
+---[RSA ]----+
|*=o.. |
|*= =. . |
|==* o. . |
|=O.o. .. |
|. *+ ..So. |
| . . .o+.o |
| E. o ++. |
| +oo=.+ |
| .o=+ +.. |
+----[SHA256]-----+
root@kali:~#
root@kali:~# ssh-copy-id root@172.16.82.136          # ssh-copy-id 命令
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.82.136 (172.16.82.136)' can't be established.
ECDSA key fingerprint is SHA256:buanLhYcZbfmeZ2rRECFo5K1v2EcfUAutraLAIQH/yU.
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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.82.136's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@172.16.82.136'"
and check to make sure that only the key(s) you wanted were added. root@kali:~# ssh root@172.16.82.136          # 可直接公钥登录,无需输入密码
Last failed login: Mon Mar :: CST from 172.16.83.136 on ssh:notty
There was failed login attempt since the last successful login.
Last login: Mon Mar ::
[root@hadoop ~]#

强制需要同时使用公钥和密码登录

  在公钥登录的基础之上,需要增加如下配置:

[lz@mail ~]$ sudo vim /etc/ssh/sshd_config
...
AuthenticationMethods publickey,password

  重启SSHD服务:

[lz@mail ~]$ sudo service sshd restart

  具体展示如下如所示:

  需要公钥和输入密码才能登录。

  以上!

Reference:

  linux下ssh公钥验证的设置和远程登录

SSH 公钥登录的更多相关文章

  1. [linux]SSH公钥登录

    由于口令密码容易泄露,SSH公钥登录相比口令登录更加安全.SSH可以轻松使用非对称加密技术给两台机子订立契约,步骤如下: 第一步 本地机生成秘钥对 指令:ssh-keygen 功能:在本地(~/.ss ...

  2. SSH公钥登录原理

    在平时工作中我们经常要远程登录服务器,这就要用到SSH协议: $ ssh user@host 主要有两种登录方式:第一种为密码口令登录,第二种为公钥登录 密码口令登录 通过密码进行登录,主要流程为: ...

  3. Linux建立互信关系(ssh公钥登录)

    Linux有多种登录方式,比如telnet.ssh.支持ssh登录方式:口令登录和公钥登录 ssh登录方式:ssh [-l login_name] [-p port] [user@]hostname ...

  4. SSH公钥登录和RSA非对称加密

    SSH登录方式 接触过Linux服务器的同学肯定用过SSH协议登录系统,通常SSH协议都有两种登录方式:密码口令登录和公钥登陆. 一.密码口令(类似于账号密码登录) 1.客户端连接服务器,服务器把公钥 ...

  5. ssh 公钥登录远程主机 并禁止密码登录

    https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-centos7 如果在新的机器上,得先用密码登录一 ...

  6. SSH公钥登录且禁止密码登录及更改默认端口

    1.ssh生成公私钥 ssh-keygen -t rsa -C "zhangsan@qq.com" 生成密钥的位置如下,id_rsa是私钥.id_rsa.pub是公钥: ➜ .ss ...

  7. 配置github SSH公钥登录

    git的安装见https://www.cnblogs.com/liliyang/p/9829931.html 配置git使用ssh密钥 git支持https和git两种传输协议,github分享链接时 ...

  8. ssh 公钥登录远程主机

    ssh-keygen 然后一路回车就可以了 ssh-copy-id user@host user代表用户名,host代表主机地址 然后根据提示输入远程主机的密码,成功,再登录就不用输入密码了

  9. linux ssh公钥免密码登录

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 一.SSH公钥登录原理 在平时工作中我们经常要远程登录服务器,这就要用到SSH ...

随机推荐

  1. (第十二周)新功能WBS

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 新增加的功能:背景音乐 功能 子功能 二级子功能 预计花费时间(小时) 实际花费时间(小时) 背景音乐 界面组 ...

  2. A+B Format 思路及解题过程结果

    A+B Format 思路及解题过程结果 github链接 题目 解题思路 这个题目的难点在于每三位用逗号隔开,以及带不带负号的问题.第一个问题,我的解决办法是先通过取整来取数,再通过取余来去数.第二 ...

  3. 【Leetcode】378. Kth Smallest Element in a Sorted Matrix

    Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...

  4. php四排序-选择排序

    原理: 在一列数字中,选出最小数与第一个位置的数交换.然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止.(以下都是升序排列,即从小到大排列) 举例说明: $ ...

  5. Windows 2019 激活教程

    Study From https://blog.csdn.net/cssxn/article/details/83743662 1. 原文里面提到了 几个序列号 Windows Server Data ...

  6. robotframework运行时后台报错UnicodeDecodeError

    win10环境下报错: Traceback (most recent call last): File "C:\Python27\lib\site-packages\robotide\con ...

  7. 轻松学JVM

    轻松学JVM(一)——基本原理 前言 JVM一直是java知识里面进阶阶段的重要部分,如果希望在java领域研究的更深入,则JVM则是如论如何也避开不了的话题,本系列试图通过简洁易读的方式,讲解JVM ...

  8. sqlbulkcopy 批量更新 数据库

    转载: http://blog.csdn.net/wangzh300/article/details/7382506 private static void DataTableToSQLServer( ...

  9. 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)

    洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...

  10. SpringBoot基础篇AOP之基本使用姿势小结

    一般来讲,谈到Spring的特性,绕不过去的就是DI(依赖注入)和AOP(切面),在将bean的系列中,说了DI的多种使用姿势:接下来看一下AOP的玩法 <!-- more --> I. ...