Linux下部署SSH登录时的二次身份验证环境记录(利用Google Authenticator)

简介:

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

一、关闭SELINUX

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

二、安装编辑工具包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@ test  ~] # yum install wget gcc make
[root@ test  ~] # yum install pam-devel libpng-devel
 
下载repoforge第三方yum源及libpam-google-authenticator-1.0- source . tar .bz2、qrencode-3.4.4. tar .gz(后两个软件需要在FQ条件下才能下载。这里我提前下载了)
下载地址:https: //pan .baidu.com /s/1i4WDbyX
提取密码:anxd
 
下载到 /data/software 目录下
[root@ test  ~] # cd /data/software/
[root@ test  software] # ls
libpam-google-authenticator-1.0- source . tar .bz2  qrencode-3.4.4. tar .gz  rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
 
[root@ test  software] # rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
 
[root@ test  software] # yum install mercurial
 
安装google authenticator PAM插件
[root@ test  ~] # cd /data/software/
[root@ test  software] # tar -jxvf libpam-google-authenticator-1.0-source.tar.bz2
[root@ test  software] # cd libpam-google-authenticator-1.0
[root@ test  libpam-google-authenticator-1.0] # make && make install
 
安装QrenCode,此工具可以在Linux命令行下生成二维码
[root@ test  ~] # cd /data/software/
[root@ test  software] # tar -zvxf qrencode-3.4.4.tar.gz
[root@ test  software] # cd qrencode-3.4.4
[root@ test  qrencode-3.4.4] # ./configure --prefix=/usr
[root@ test  qrencode-3.4.4] # make && make install

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

1
2
3
4
5
6
7
8
[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

 

四、使用google authenticator PAM插件为ssh登录账号生成动态验证码
注意:哪个账号需要动态验证码,请切换到该账号下操作。(可以在不同用户下执行这个命令以生成各自的二次验证码)

[root@test ~]# google-authenticator

Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@BJLX_NET_TEST-01%3Fsecret%3DCGB5NWP6SABN3TM7          #这个链接只能在FQ条件下才能打开

Your new secret key is: CGB5NWP6SABN3TM7    #如果在手机的谷歌身份验证器上不想通过"扫描条形码"的方式添加,就输入这个key,通过"手动输入验证码的方式"。账号就是服务器主机名。
Your verification code is 730249
Your emergency scratch codes are:      #下面会生成5个紧急验证码(当无法获取动态验证码或验证码不能使用使用可以使用这5个)
66151894                                              #需要注意的是:这5个验证码用一个就会少一个!请保存好!
91475582
37383236
70667696
70522112

Do you want me to update your "/root/.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身份验证器,通过此工具扫描上一步生成的二维码图形,获取动态验证码。
在App Store里直接可以下载Authenticator

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

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

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

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

1
2
3
4
5
6
7
8
[wangshibo@BJLX_NET_TEST-01 ~]$  ssh  root@172.29.32.251
The authenticity of host  '[172.29.32.251]:22 ([172.29.32.251]:22)'  can't be established.
RSA key fingerprint is 5c:e7:1a:05:8b:2e:66:99:20:90:1f:47:56:bf:b9:41.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '[172.29.32.251]:22'  (RSA) to the list of known hosts.
Verification:
Password:
[root@ test  ~] #
分类:  SSH

本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/7065255.html,如需转载请自行联系原作者
相关文章
|
2月前
|
监控 Ubuntu 安全
debian或Ubuntu中开启ssh允许root远程ssh登录的方法
在Debian或Ubuntu系统中启用root用户的SSH远程登录需要编辑SSH配置文件、设置root密码并重启SSH服务。虽然这可以在某些情况下提供便利,但必须注意安全性,通过使用强密码、限制IP访问、使用SSH密钥认证等方法来保护服务器的安全。
823 5
|
6月前
|
JavaScript 应用服务中间件 Linux
【应用服务 App Service】解决无法从Azure门户SSH登录问题
【应用服务 App Service】解决无法从Azure门户SSH登录问题
|
5月前
|
安全 Linux 网络安全
Linux端的ssh如何升级?
Linux端的ssh如何升级?
530 59
|
3月前
|
监控 Ubuntu Linux
使用VSCode通过SSH远程登录阿里云Linux服务器异常崩溃
通过 VSCode 的 Remote - SSH 插件远程连接阿里云 Ubuntu 22 服务器时,会因高 CPU 使用率导致连接断开。经排查发现,VSCode 连接根目录 ".." 时会频繁调用"rg"(ripgrep)进行文件搜索,导致 CPU 负载过高。解决方法是将连接目录改为"root"(或其他具体的路径),避免不必要的文件检索,从而恢复正常连接。
|
4月前
|
网络安全 虚拟化 Docker
SSH后判断当前服务器是云主机、物理机、虚拟机、docker环境
结合上述方法,您可以对当前环境进行较为准确的判断。重要的是理解每种环境的特征,并通过系统的响应进行综合分析。如果在Docker容器内,通常会有明显的环境标志和受限的资源视图;而在云主机或虚拟机上,虽然它们也可能是虚拟化的,但通常提供更接近物理机的体验,且可通过硬件标识来识别虚拟化平台。物理机则直接反映硬件真实信息,较少有虚拟化痕迹。通过这些线索,您应该能够定位到您所处的环境类型。
130 2
|
6月前
|
监控 安全 Ubuntu
在Linux中,如何进行SSH服务配置?
在Linux中,如何进行SSH服务配置?
|
6月前
|
安全 Shell Linux
如何禁止某个用户使用ssh登录
本文介绍了五种禁止用户通过SSH登录的方法:1) 修改`/etc/ssh/sshd_config`文件中的`DenyUsers`和`DenyGroups`来阻止特定用户或用户组登录;2) 将用户的默认shell设置为`/usr/sbin/nologin`或`/bin/false`以禁用其SSH访问;3) 利用PAM(可插入认证模块)通过编辑`/etc/security/sshd.conf`来限制登录权限;4) 通过编辑`/etc/hosts.deny`文件拒绝特定用户的SSH访问;5) 锁定或禁用用户账号以阻止所有类型的登录。每种方法都提供了详细的步骤指导。
903 1
|
6月前
|
Linux 网络安全
Linux开启ssh
Linux开启ssh
63 0
|
6月前
|
安全 Linux 网络安全
在Linux中,使用rsync同步数据时,假如采用的是ssh方式,并且目标机器的sshd端端并不是默认的22端口,该如何做?
在Linux中,使用rsync同步数据时,假如采用的是ssh方式,并且目标机器的sshd端端并不是默认的22端口,该如何做?
|
8月前
|
安全 Linux Shell
Linux中SSH命令介绍
Linux中SSH命令介绍
227 2