CentOS下Google Authenticator配置SSH登录动态验证码配置

说明:

1、一般ssh登录服务器,只需要输入账号和密码。

2、本教程的目的:在账号和密码之间再增加一个验证码,只有输入正确的验证码之后,再输入密码才能登录。这样就增强了ssh登录的安全性。

3、账号、验证码、密码三者缺一个都不能登录,即使账号和密码正确,验证码错误,同样登录失败。

4、验证码:是动态验证码,并且是通过手机客户端自动获取(默认每隔30秒失效一次)。

5、最终目的:远程ssh登录一台服务器,需要正确的账号、密码、及一个可以获取到动态验证码的手机(目前支持Android和ios手机系统)。

具体操作:

操作系统:CentOS

一、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

二、安装编辑工具包

1、使用CentOS默认yum源安装

yum install wget gcc make

yum install pam-devel libpng-devel

2、配置repoforge第三方yum源安装mercurial包

CentOS各个版本,请选择正确版本

CentOS 5.x

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.i386.rpm

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.x86_64.rpm

CentOS 6.x

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

yum install mercurial #安装

 三、安装google authenticator PAM插件 

 

cd /usr/local/src

wget https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2

注意:打开google需要翻墙,可以先想办法下载好libpam-google-authenticator-1.0-source.tar.bz2上传到/usr/local/src目录进行安装

tar jxvf libpam-google-authenticator-1.0-source.tar.bz2 #解压

cd libpam-google-authenticator-1.0 #进入目录

make #编译

make install #安装

四、安装QrenCode,此工具可以在Linux命令行下生成二维码

cd /usr/local/src

wget http://fukuchi.org/works/qrencode/qrencode-3.4.4.tar.gz #下载

tar zxf qrencode-3.4.4.tar.gz #解压

cd qrencode-3.4.4 #进入目录

./configure --prefix=/usr #配置

make #编译

make install #安装

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

vi /etc/pam.d/sshd #编辑,在第一行增加以下代码

auth required pam_google_authenticator.so

:wq! #保存退出

vi /etc/ssh/sshd_config #编辑

ChallengeResponseAuthentication yes #修改no为yes

:wq! #保存退出

service sshd restart #重启ssh服务,使配置生效

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

注意:哪个账号需要动态验证码,请切换到该账号下操作

google-authenticator #运行此命令

Do you want authentication tokens to be time-based (y/n) y #提示是否要基于时间生成令牌,选择y

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

Your new secret key is: AXNRWARYQPHI5WYU

Your verification code is 199255

Your emergency scratch codes are:

21767982

60638828

24009000

44681673

28015662

#上面的网址为生成的二维码图形地址(需要翻墙才能打开),还会生成密钥,以及5个紧急验证码(当无法获取动态验证码时使用,注意:这5个验证码用一个就会少一个!请保存好!)

Do you want me to update your "/home/jss/.google_authenticator" file (y/n) y #提示是否要更新验证文件,选择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) n

#默认动态验证码在30秒内有效,由于客户端和服务器可能会存在时间差,可将时间增加到最长4分钟,是否要这么做:这里选择是n,继续默认30秒

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

#是否限制尝试次数,每30秒只能尝试最多3次,这里选择y进行限制

七、手机安装Google身份验证器,通过此工具扫描上一步生成的二维码图形,获取动态验证码

Android手机下载:

https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2

iOS手机下载:

https://itunes.apple.com/us/app/google-authenticator/id388497605

注意:打开google需要翻墙,或者自己想办法下载Google身份验证器安装。

另外,还需要安装条形码扫描器,用来扫描验证二维码,以获取动态验证码

以Android手机为例:

安装好Google身份验证器,打开如下图所示:

开始设置-扫描条形码,然后扫描第六步中生成的二维码图形

扫描完成后,如下图所示:

八、ssh远程登录服务器

输入账号之后,会提示输入验证码

login as: root

Using keyboard-interactive authentication.

Verification code:

打开手机上的Google身份验证器,输入动态验证码,回车。

注意:动态验证码没有回显,所以在屏幕上看不到输入的内容,但只要保证输入正确即可!

Using keyboard-interactive authentication.

Password:

接着输入密码,即可成功登录系统!

注意:以此步骤必须在30秒内完成。否则动态验证码过期,必须重新操作。

时间: 2024-07-31 06:17:18

CentOS下Google Authenticator配置SSH登录动态验证码配置的相关文章

Centos下禁止root的ssh登录shell脚本

linux禁止root用户通过ssh登录 禁止root用户通过ssh登录,是通过操作sshd_config配置文件来实现. 1.打开ssh配置文件 vim /etc/ssh/sshd_config 2.找到文件中下面一行文字 #PermitRootLogin no也有可能是#PermitRootLogin yes 3.去掉该行前面的#号,使其成为下面这样: PermitRootLogin no 4.重启ssh服务 /etc/init.d/sshd restart 接下来试着用root账号再登陆发

centos下防止暴力破解ssh

centos rhel下如下 #! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt cat /var/log/secure|awk '/Invalid user/{print $NF}'|sort|uniq -c|awk '{print $2"="$1;}' >> /tm

dlink路由器配置上网之动态IP配置上网(光纤入户)

光纤线路连接: 首次安装,建议准备两条网线.从光猫LAN口连接一条网线到路由器INTRNET口,再用另一条网线从路由器LAN口连接到电脑.若只有一条线,也可以先连接电脑和路由器,配置好路由器后再拔掉,重新连接到ADSL Modem和路由器INTERNET口:或用笔记本.手机等设备无线连接到路由器进行配置. 如图连线完成后,查看被连线接口对应的指示灯是否正常亮,如果不亮,请参考:指示灯不亮,如何处理? 登陆路由器管理界面: 打开浏览器在地址栏输入http://192.168.0.1  ,如图:  

用 Google Authenticator 加强 VPS 及 WordPress 甚至桌面电脑的安全性

传统地用 SSH 登录 VPS 的时候,靠的只是一串字符密码,如果密码被泄露.被猜解.被暴力枚举成功,那么 VPS 就完全暴露在坏人手中了.安全意识高一些的用户会使用公私钥代替字符来登录,但是这样的缺点是如果在陌生的电脑上想要临时登录一下,由于没有私钥,也就没办法了.传统地登录 WordPress 的时候,更是只有一个密码,如果被泄露.猜解.暴力枚举,辛辛苦苦经营的博客就完蛋了. 本文介绍如何通过 Google Authenticator 构建"物理屏障",最大限度地阻断来自网络的密码

CentOS下SSH无密码登录的配置文件_Linux

1.确认本机sshd的配置文件(需要root权限) $ gedit /etc/ssh/sshd_config 找到以下内容,并去掉注释符"#" RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys 2.如果修改了配置文件需要重启sshd服务 (需要root权限) $ /sbin/service sshd restart 配置SSH无密码登录需要3步: 1.生成公钥和私

Linux CentOS下shell显示-bash-4.1$不显示用户名路径

  Linux CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法 问题描述: CentOS下新增一个用户,登录进去之后shell脚本的信息如下: 而不是我们经常看到的username@hostname的组合,看起来很不舒服. 解决方案: 问题的原因是因为没有配置.bash_profile的问题,可以通过配置这个文件来解决问题. 1.在Terminal输入: [plain] vi ~/.bash_profile 2.如果没有.bash_profile可以自己添加.然后往文

设置 Linux 上 SSH 登录的 Email 提醒

VPS)上启用 SSH 服务使得该服务器暴露到互联网中,为黑客攻击提供了机会,尤其是当 VPS 还允许root 直接访问时.VPS 应该为每次 SSH 登录成功尝试配置一个自动的 email 警告. VPS 服务器的所有者会得到各种 SSH 服务器访问日志的通知,例如登录者.登录时间以及来源 IP 地址等信息.这是一个对于服务器拥有者来说,保护服务器避免未知登录尝试的重要安全关注点.这是因为如果黑客使用暴力破解方式通过 SSH 来登录到你的 VPS 的话,后果很严重.在本文中,我会解释如何在 C

如何设置 Linux 上 SSH 登录的 Email 提醒

如何设置 Linux 上 SSH 登录的 Email 提醒 虚拟私有服务器 (VPS)上启用 SSH 服务使得该服务器暴露到互联网中,为黑客攻击提供了机会,尤其是当 VPS 还允许root 直接访问时.VPS 应该为每次 SSH 登录成功尝试配置一个自动的 email 警告. VPS 服务器的所有者会得到各种 SSH 服务器访问日志的通知,例如登录者.登录时间以及来源 IP 地址等信息.这是一个对于服务器拥有者来说,保护服务器避免未知登录尝试的重要安全关注点.这是因为如果黑客使用暴力破解方式通过

思科模拟器静态路由和动态路由配置

思科模拟器 静态路由,动态路由 RIP配置 动态路由协议 RIP 命令就两行比较简单 实验拓扑图如下: 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Network/lyjs/ 要想实现pc1 和pc2 通讯就要对R1 ,R2,R3 进行配置 1. 静态配置命令格式如下: 对于R1 Router>en Router#conf Configuring from terminal, memory, or network [terminal]?