一般来说,使用ssh远程登录服务器,只需要输入账号和密码,显然这种方式不是很安全。为了安全着想,可以使用GoogleAuthenticator(谷歌身份验证器),以便在账号和密码之间再增加一个验证码,只有输入正确的验证码之后,再输入密码才能登录。这样就增强了ssh登录的安全性。账号、验证码、密码三者缺一个都不能登录,即使账号和密码正确,验证码错误,同样登录失败。其中,验证码是动态验证码,并且是通过手机客户端自动获取(默认每隔30秒失效一次)

Google Authenticator开源版主页 https://github.com/google/google-authenticator

一、Ubuntu安装

1、安装Google Authenticator

apt-get install libpam-google-authenticator

也可以通过源码编译安装

git clone https://github.com/google/google-authenticator.git
cd google-authenticator/libpam/
./bootstrap.sh
./configure
make
make install

注意:在Debian7中执行./configure时可能存在以下错误提示

configure: error: Unable to find the PAM library or the PAM header files

在此需要安装libpam0g-devlibtool

apt-get -y install libpam0g-dev libtool

2、设置Google Authenticator

在手机端搜索安装 Google Authenticator

google-authenticator
Do you want authentication tokens to be time-based (y/n) y

然后出现二维码,使用手机端Google Authenticator扫码,接下来服务器端继续设置

如果没有出现二维码,可以将提示的以 https://www.google.com/chart? 开头的连接复制到浏览器中,就可以看到二维码了

Do you want me to update your "/root/.google_authenticator" file (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) y

修改配置文件

root@test:~# vim /etc/pam.d/sshd        #在第一行(即account    required     pam_nologin.so的下一行)增加以下代码
auth required pam_google_authenticator.soroot@test:~#  vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes          #修改no为yesroot@test:~#  service ssh restart

3、手机安装Google身份验证器

见Centos安装步骤

4、登录

再次登录的话输入用户名后就跟着提示两部验证码,然后才输入用户密码,如下:

root@test1:~$ ssh root@120.17.144.148 -p65322
The authenticity of host '[120.17.144.148]:65322 ([120.17.144.148]:65322)' can't be established.
RSA key fingerprint is 6f:d1:19:41:af:25:8d:ee:b9:54:11:c0:cc:c0:6e:cd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[120.17.144.148]:65322' (RSA) to the list of known hosts.
Verification code:
Password:
Last login: Tue Jan  8 16:55:15 2019 from 121.157.126.116Welcome to aliyun Elastic Compute Service![root@test2 ~]#  

二、Centos安装

1、关闭SELINUX

[root@test ~]# vim /etc/selinux/config      #永久关闭。需要reboot重启后生效
SELINUX=enforcing 改为 SELINUX=disabled[root@test ~]# setenforce 0   #临时性关闭。不需要reboot重启

2、安装编辑工具包

[root@test ~]# yum install wget gcc make  pam-devel libpng-devel
[root@test ~]# yum -y install google-authenticator  qrencode mercurial

3、配置ssh服务调用google authenticator PAM插件

[root@test ~]# vim /etc/pam.d/sshd       #在第一行(即auth       required pam_sepermit.so的下一行)增加以下代码
auth required pam_google_authenticator.so[root@test ~]# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes          #修改no为yes[root@test ~]# service sshd restart

4、使用google authenticator PAM插件为ssh登录账号生成动态验证码

注意:哪个账号需要动态验证码,请切换到该账号下操作。(可以在不同用户下执行这个命令以生成各自的二次验证码)

[root@test ~]# google-authenticator

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@iZ23e3f2jdcZ%3Fsecret%3DD76NMSG26PAUJGAR

Your new secret key is: D76NMSG26PAUJGAR
Your verification code is 829676
Your emergency scratch codes are:
51322579
63010483
39007873
32360318
54935046

Do you want me to update your "~/.google_authenticator" file (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) y

5、手机安装Google身份验证器

通过此工具扫描上一步生成的二维码图形,获取动态验证码。
在App Store里直接可以下载Authenticator

然后扫描上面在服务器上生成的二维码,每个用户都会有一个单独的二维码

接着在ssh的客户端里设置,如下,设置"Keyboard Interactive"方式登录

然后再次连接的时候,就会提示先输入二次身份验证码,再输入用户密码。

在Linux客户机上远程登录,效果一样:

root@test1:~$ ssh root@120.17.144.148 -p65322
The authenticity of host '[120.17.144.148]:65322 ([120.17.144.148]:65322)' can't be established.
RSA key fingerprint is 6f:d1:19:41:af:25:8d:ee:b9:54:11:c0:cc:c0:6e:cd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[120.17.144.148]:65322' (RSA) to the list of known hosts.
Verification code:
Password:
Last login: Tue Jan  8 16:55:15 2019 from 121.157.126.116Welcome to aliyun Elastic Compute Service![root@test2 ~]#

  

转载于:https://www.cnblogs.com/chenpingzhao/p/10240227.html

【Linux】使用Google Authenticator 实现ssh登录双因素认证相关推荐

  1. Linux下使用Google Authenticator配置SSH登录动态验证码

    说明: 1.一般ssh登录服务器,只需要输入账号和密码. 2.本教程的目的:在账号和密码之间再增加一个 验证码,只有输入正确的验证码之后,再输入 密码才能登录.这样就增强了ssh登录的安全性. 3.账 ...

  2. SSH 登录添加 Google Authenticator 两步验证 双因子认证

    SSH 登录添加 Google Authenticator 两步验证 双因子认证 安装 Google Authenticator # 时间与客户端进行校验,确保服务器时间与手机时间一致,避免时差导致严 ...

  3. 配置 SSH 双因素认证

    ​​​​​​运维工程师必备的认证[红帽liunx-RHCE 8]_XMWS_IT的博客-CSDN博客_运维工程师考什么证书RHCE试听课程:linux系统下,用这个命令可以提高60%的工作效率http ...

  4. 登录的双因素认证-Golang实现

    需求 实现登录双因素认证 开源库使用 框架:github.com/gin-gonic/gin 验证码:github.com/dchest/captcha session管理:github.com/go ...

  5. Linux 之 利用Google Authenticator实现用户双因素认证

    一.介绍:什么是双因素认证 双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产 ...

  6. 谷歌Google Authenticator实现双因素认证

    参考: https://www.cnblogs.com/hanyifeng/p/kevin4real.html 介绍:什么是双因素认证 双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到 ...

  7. xshell下载mysql到本地文件_在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)-Go语言中文社区...

    在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...

  8. 基于Google 验证器 实现内网的双因素认证

    最近的一个项目需要实现双因素强认证,平常我们都是采用 静态密码+动态短信这样的方式来实现,但用户侧并没有相应的短信接口. 后来决定采用 google身份验证器来实现.在网上找了一些资料和代码片段,经过 ...

  9. 提升云桌面登录账号安全,宁盾双因素认证“护航”齐鲁制药移动办公

    客户介绍: 齐鲁制药,是中国大型综合性现代制药企业,中国医药工业百强企业,专业从事治疗肿瘤.心脑血管.抗感染.精神系统.神经系统.眼科疾病的制剂及其原料药的研制.生产与销售.齐鲁制药销售总公司是齐鲁制 ...

最新文章

  1. iOS常用第三方开源框架和优秀开发者博客等
  2. 我爸和在我们小区里的一位老大爷
  3. Ext JS学习第十六天 事件机制event(一)
  4. Redis数据持久化机制AOF原理分析一---转
  5. Spring JdbcTemplate+JdbcDaoSupport实例
  6. asp sql 导出 excel_Mysql数据导出到excel基于python
  7. Hyper-V 3.0服务器虚拟化:打造坚固的云
  8. 浅谈WebView利用localStore websql和IndexDB 来存储数据
  9. ps制作计算机考试证件照,如何通过PS制作一寸证件照(超详细流程)?
  10. SVN 忽略不需要提交的文件
  11. con和com开头单词
  12. localhost已拒绝连接
  13. 《Spring实战》读书笔记-第5章 构建Spring Web应用程序
  14. 用 Hypothesis 来自动化单元测试
  15. 从多臂老虎机开始学习强化学习中的探索与利用
  16. 2021国家网络安全等级保护工作协调小组办公室推荐测评机构名单(未删减版)
  17. 基于vb的mysql管理系统代码_基于VB+MySQL的简单图书管理系统
  18. 服务器的CPLD的上下电控制时序及源代码实现
  19. Vmware 自适应分辨率设置
  20. 有向图强连通分量的Tarjan算法——转自BYVoid

热门文章

  1. Linux公平队列FQ接口实现
  2. List集合 查询重复数据,查询重复次数
  3. 牛客-模拟、枚举与贪心-2022.10.18
  4. 微信小程序仿手机淘宝新增地址自动识别地址,姓名,手机号
  5. python编程:从入门到实践-2变量和简单数据类型
  6. 长期不能落地的区块链突破口在这里
  7. 网易云音视频多人通话webRTC的实现(接)。
  8. 蛋花花谈程序员年过四十该何去何从
  9. ctfshow SQL注入Web171-174
  10. 随心造型 助她从容出彩 戴森聚焦新生儿妈妈群体,以颠覆科技时刻呵护她的头发健康