1、下载Google Authenticator源码
wget https://github.com/google/google-authenticator/tree/master/libpam
打开https://github.com/google/google-authenticator/blob/master/libpam/pam_google_authenticator.c,查找到pam_get_item(pamh, PAM_AUTHTOK, &password),修改PAM_AUTHTOK为PAM_AUTHTOK_TYPE
Build & install
./bootstrap.sh ./configure make sudo make install
拷贝pam_google_authenticator.so到/lib64/security下
Running
执行./google-authenticator
Your new secret key is: A3FY5V6TCAEUN4FX
Your verification code is 680297
Your emergency scratch codes are:
75304045
77554864
18069864
99313356
32694105
2、下载Google身份验证器
通过手机,ipad等下载,打开身份验证器,在google身份验证器中输入用户名(一般使用邮件),秘钥则输入./google-authenticator结果中的secret key,选择基于时间候保存,就可以看到每30s生成一个6位数的随机数。
3、下载JPam
官网:http://jpam.sourceforge.net/
wget http://sourceforge.net/projects/jpam/files/jpam/jpam-1.1/JPam-Linux_amd64-1.1.tgz/download
按照JPam安装说明,
a> 首先在/etc/pam.d下,创建一个google-auth文件,在文件中添加如下内容:
auth required /lib64/security/pam_google_authenticator.so use_first_pass
account required /lib64/security/pam_unix_acct.so
b> 写一个JPamTest.java程序
String user1Name = "root"; String user1Credentials = "password"; Pam pam = new Pam("google-auth"); //如果不输入名称,则默认使用net-sf-jpam boolean authenticated = pam.authenticateSuccessful(user1Name, user1Credentials)); JPamTest.java依赖JPam1.1.jar,libjpam.so(此动态库有问题,需要按照下面的步骤重新编译)
下载源码,并重新编译libpam.so
wget https://github.com/gregrluck/jpam
打开jpam/src/main/c/Pam.c,查找到pam_set_item(pamh, PAM_AUTHTOK, password),修改PAM_AUTHTOK为PAM_AUTHTOK_TYPE
修改后,执行make,重新编译libpam.so
在linux下编译并执行JPamTest.java,其中username为linux root用户名,密码为google身份验证器中的动态密码,如果验证成功,则authenticated的状态值为True.