通过Google身份验证器加强Linux帐户安全

简介: 下载Google的身份验证模块:   # wget https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.

下载Google的身份验证模块:

 

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

 

解压缩并编译安装:

# tar xvf libpam-google-authenticator-1.0-source.tar.bz2 
# cd libpam-google-authenticator-1.0
# make
# make install

而后,google的验证模块就会被复制到/lib64/security目录下,而用来生成密钥的可执行程序:google-authenticator,则复制到/usr/local/bin目录下,方便调用。

Tips:编译安装google-authenticator需要pam-devel依赖包,如果没有的话,请首先安装该依赖包。

比如,我们想为账户:liuke 增加一层额外的验证机制,则先通过google-authenticator生成密钥:

[liuke@localhost ~]$ google-authenticator 
 
Do you want authentication tokens to be time-based (y/n) y

首先会提示你,是否要基于时间生成令牌,选择Y,然后它会生成密钥,以及紧急状态使用的验证码(有5个,谨当无法获取验证码时使用,注意这些紧急验证码用一次就少一个的哟,所以这几个紧急验证码一定要保存好,关键时刻要派上大用场的),详细信息如下:

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/jss@localhost.localdomain%3Fsecret%3D3V7K2ONO55DE56SD
Your new secret key is: 3V7K2ONO55DE56SD
Your verification code is 424380
Your emergency scratch codes are:
  96307775
  87311306
  56915688
  84694809
  27875014

然后会提示你是否要更新验证文件,肯定更新啊:

Do you want me to update your "/home/jss/.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

默认情况下,令牌只在30秒内有效,由于客户端和服务器时间不完全一致的因素,可以将时间窗口加大到最长4分钟,是否要这么做:

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

是否限制尝试次数,每30秒只能尝试最多3次:

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

密钥生成好之后,下面修改PAM中关于ssh的配置,编辑/etc/pam.d/sshd文件:

# vim /etc/pam.d/sshd 

增加一行:

auth    required        pam_google_authenticator.so

接下来再修改ssh的配置文件,编辑/etc/ssh/sshd_config文件:

# vim /etc/ssh/sshd_config 
将ChallengeResponseAuthentication no修改为ChallengeResponseAuthentication yes
然后重新启动ssh服务:

当再次使用 liuke 用户以SSH方式登录时,就会提示输出验证码:

[root@localhost ~]# ssh liuke@192.168.33.126
Verification code: 

那么,验证码从哪里来呢?在手机上安装一款名叫:Google身份验证器的应用。在打开的应用界面中新增帐户,然后会出现两个选择:扫描条形码(二维码),或者选择输出提供的密钥,任选其一即可。

这两项信息从哪里来呢?前面执行google-authenticator命令后首先输出的信息,那个HTTP的网址,打开你就会发现,原来它是个图片二维码,扫描即可。若网址打不开也没关系,选择密钥就OK了。

帐户添加完成后,你应该就能在手机上看到它生成的验证码了,先输入验证码,然后再输入密码,只有验证码和密码都输入正确,才能正常登录。

Password: 
Last login: Tue Jan 14 10:14:33 2015 from 192.168.33.126

注意!!!输入验证码的时间最长只有30秒钟,超时之后该验证码就失效,需要到手机端获取新的验证码才行~

 


img_42a4adae4716d0e15c3eeaabfd040044.png

注:转载需注明出处及作者。

流柯      

目录
相关文章
|
6月前
|
运维 安全 Linux
实现远程访问Linux堡垒机:通过JumpServer系统进行安全的服务器管理
实现远程访问Linux堡垒机:通过JumpServer系统进行安全的服务器管理
|
4月前
|
安全 关系型数据库 MySQL
Linux 实用小脚本系列(2)----mysql安全初始化脚本的免交互执行--mysql_secure_installation
Linux 实用小脚本系列(2)----mysql安全初始化脚本的免交互执行--mysql_secure_installation
49 0
|
2月前
|
安全 网络协议 Linux
Linux系统管理、服务器设置、安全、云数据中心
Linux系统管理、服务器设置、安全、云数据中心
|
6月前
|
安全 Linux 网络安全
百度搜索:蓝易云 ,Linux Debian11服务器安装SSH,创建新用户并允许SSH远程登录,及SSH安全登录配置!
这些步骤提供了在Debian 11服务器上安装SSH,创建新用户并允许SSH远程登录以及进行SSH安全登录配置的指南。请确保按照步骤操作,并根据您的需求进行必要的修改。
100 0
|
3月前
|
Ubuntu 安全 Linux
Linux/Ubuntu 的日常升级和安全更新,如何操作?
Linux/Ubuntu 的日常升级和安全更新,如何操作?
69 0
Linux/Ubuntu 的日常升级和安全更新,如何操作?
|
3月前
|
安全 Linux C语言
【Linux权限:系统中的数字锁与安全之门】(下)
【Linux权限:系统中的数字锁与安全之门】
|
3月前
|
安全 Linux 数据安全/隐私保护
【Linux权限:系统中的数字锁与安全之门】(上)
【Linux权限:系统中的数字锁与安全之门】
|
8月前
|
安全 网络协议 Linux
Linux SSH:安全远程访问与管理
SSH(Secure Shell)是一种网络协议,广泛应用于Linux系统中,用于在不安全的网络中进行安全的远程访问和管理。SSH协议通过加密和身份认证机制,确保数据传输的安全性和用户身份的可靠性。本文将深入探讨Linux下的SSH协议,包括工作原理、配置、密钥认证、安全性措施以及实际应用等,帮助读者充分了解SSH的重要性,并学会在Linux系统中正确使用SSH实现安全远程访问和管理。
277 0
|
4月前
|
安全 Linux 网络安全
Linux【安全 01】云服务器主机安全加固(修改SSHD端口、禁用登陆失败的IP地址、使用密钥登录)
Linux【安全 01】云服务器主机安全加固(修改SSHD端口、禁用登陆失败的IP地址、使用密钥登录)
180 1
|
5月前
|
Web App开发 存储 Linux
Linux C/C++开发(后端/音视频/游戏/嵌入式/高性能网络/存储/基础架构/安全)(下)
Linux C/C++开发(后端/音视频/游戏/嵌入式/高性能网络/存储/基础架构/安全)