CentOS 使用 Google Authenticator 登录验证
Google Authentication 项目 包含了多个手机平台的一次性验证码生成器的实现,以及一个可插拔的验证认证模块(PAM)。这些实现支持基于 HMAC 的一次性验证码(HOTP)算法(RFC 4226)和基于时间的一次性验证码(TOTP)算法(RFC 6238)。
下面将在 CentOS 上安装并使用 Google Authenticator 做登录的身份验证,当前系统的版本为
CentOS Linux release 7.2.1511 (Core)
安装 Google Authenticator PAM module
-
确保
ntpd
已安装并正常运行运行yum install -y ntpdate systemctl start ntpd systemctl enable ntpd
ntpdate
是用来自动同步时间的程序,这里启动它并设置它开机自动启动。 -
安装一些接下去会用到的组件
yum install -y git make gcc libtool pam-devel
-
编译安装 Google Authenticator PAM module
git clone https://github.com/google/google-authenticator cd google-authenticator/libpam ./bootstrap.sh ./configure make make install ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/
配置 SSH 服务
打开 /etc/ssh/sshd_config
文件
vim /etc/ssh/sshd_config
修改下面字段的配置
ChallengeResponseAuthentication yes
PasswordAuthentication no
PubkeyAuthentication yes
UsePAM yes
然后重启一下 sshd
服务,使配置生效
systemctl restart sshd
这里将 PubkeyAuthentication
配置成了 yes
表示支持公钥验证登录,即使某个账号启用了 Google Authenticator 验证,只要登录者机器的公钥在这个账号的授权下,就可以不输入密码和 Google Authenticator 的认证码直接登录。
配置 PAM
打开 /etc/pam.d/sshd
文件
vim /etc/pam.d/sshd
这里分四种情况来配置
-
验证密码和认证码,没有启用 Google Authenticator 服务的账号只验证密码(推荐)
auth substack password-auth #... auth required pam_google_authenticator.so nullok
password-auth
与pam_google_authenticator
的先后顺序决定了先输入密码还是先输入认证码。 -
验证密码和认证码,没有启用 Google Authenticator 服务的账号无法使用密码登录
auth substack password-auth #... auth required pam_google_authenticator.so
-
只验证认证码,不验证密码,没有启用 Google Authenticator 服务的账号不用输入密码直接可以成功登录
#auth substack password-auth #... auth required pam_google_authenticator.so nullok
注释掉
auth substack password-auth
配置就不会再验证账号密码了。 -
只验证认证码,不验证密码,没有启用 Google Authenticator 服务的账号无法使用密码登录
#auth substack password-auth #... auth required pam_google_authenticator.so
启用 Google Authenticator
切换至想要使用 Google Authenticator
来做登录验证的账号,执行下面操作
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/shenyu@shenyu.me%3Fsecret%3DKHMH46EWI2RIRZ53KQTNGHXNP4%26issuer%3Dshenyu.me
# 这里是个二维码
Your new secret key is: KHMH46EWI2RIRZ53KQTNGHXNP4
Your verification code is 753579
Your emergency scratch codes are:
99181037
68865807
88385439
59103432
81045035
这里会显示一个二维码,如果你的终端终端不支持显示二维码,可以手动打开这个网页链接(墙)来查看二维码或者手动输入后面的密钥(secret key)来代替扫描二维码,之后的操作会用到这个二维码/密钥(secret key)。这里还有一个认证码(verifiction code),暂时不知道有什么用,以及 5 个紧急救助码(emergency scratch code),紧急救助码就是当你无法获取认证码时(比如手机丢了),可以当做认证码来用,每用一个少一个,但其实可以手动添加的,建议如果 root 账户使用 Google Authenticator 的话一定要把紧急救助码另外保存一份。
Do you want me to update your "/home/test/.google_authenticator" file? (y/n) y
是否更新用户的 Google Authenticator 配置文件,选择 y
才能使上面操作对当前用户生效,其实就是在对应用户的 Home
目录下生成了一个 .google_authenticator
文件,如果你想停用这个用户的 Google Authenticator 验证,只需要删除这个用户 Home
目录下的 .google_authenticator
文件就可以了。
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
每次生成的认证码是否同时只允许一个人使用?这里选择 y
。
By default, tokens are good for 30 seconds. 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 -1min (window size of 3) to about -4min (window size of
17 acceptable tokens).
Do you want to do so? (y/n) n
是否增加时间误差?这里选择 n
。
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
是否启用次数限制?这里选择 y
,默认每 30 秒最多尝试登录 3 次。
上面交互式的设置也可用通过参数一次性设置(推荐)
google-authenticator -t -f -d -l shenyu@shenyu.me -i SHENYU.ME -r 3 -R 30 -W
可以看到,通过参数还可以自定义 发行商
和 标签
,执行 google-authenticator -h
来查看所有的参数设置
google-authenticator []
-h, --help Print this message
-c, --counter-based Set up counter-based (HOTP) verification
-t, --time-based Set up time-based (TOTP) verification
-d, --disallow-reuse Disallow reuse of previously used TOTP tokens
-D, --allow-reuse Allow reuse of previously used TOTP tokens
-f, --force Write file without first confirming with user
-l, --label=
设置 Google Authenticator 手机 App
在手机上下载并安装 Google Authenticator
手机类型 | App 程序名称 |
---|---|
IOS | Google Authenticator |
Android | 谷歌动态口令(请在手机对应的应用商店里搜索下载) |
安装完后,打开 Google Authenticator/谷歌动态口令 App,点击 开始设置
,选择 扫描条形码
扫描上面 google-authenticator
命令生成的二维码,然后手机上就能看到对应的认证码了。
这里的认证码每 30 秒变化一次,认证码上面的 SHENYU.ME
对应的是 google-authenticator
参数 -i
设置的发行商,认证码下面的 shenyu@shenyu.me
对应的是 google-authenticator
参数 -l
设置的标签,如果你没有通过 google-authenticator
的参数设置发行商和标签,默认会使用系统的 hostname
来作为发行商,标签则则使用用户名和 hostname
的组合,格式为 username@hostname
,标签其实是后期可以通过手机App来修改的,而发行商则修改不了。
现在重新使用 SSH 登录服务器,就会要求输入密码和 Verification code
来验证身份。如果登陆时遇到问题,请查看日志文件 /var/log/secure
。
参考资料
GooglePlus登录添加修改操作记录
使用官方定义按钮页面自动刷新登录:
<div id="gConnect">
<button class="g−signin"
data−scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"
data−requestvisibleactions="http://schemas.google.com/AddActivity"
data−clientId="申请ClientID"
data−callback="onSignInCallback"
data−theme="dark"
data−cookiepolicy="single_host_origin">
</button>
</div>
<script type="text/javascript">
var kx_google_host_config = "配置登录成功后跳转域名";
</script>
<script type="text/javascript" src="https://apis.google.com/js/client:platform.js" async defer></script>
<script type="text/javascript" src="http://简单封装/clientgoogle.so.js" ></script>
使用自己定义按钮页面不自动刷新登录:
<div id="gConnect">
<button type="button" onclick="window.render();return false;" class="btnLarge">使用Google帐号登录</button>
</div>
<script type="text/javascript">
var kx_google_host_config = "配置登录成功后跳转域名";
window.___gcfg = {
parsetags: 'explicit'
};
var render = function(){
gapi.signin.render('gConnect', {
'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
'requestvisibleactions': 'http://schemas.google.com/AddActivity',
'clientId': '申请ClientID',
'callback': 'onSignInCallback',
'theme': 'dark',
'cookiepolicy': 'single_host_origin'
});
};
</script>
<script type="text/javascript" src="https://apis.google.com/js/client:platform.js" async defer></script>
<script type="text/javascript" src="http://简单封装/clientgoogle.so.js"></script>
clientgoogle.so.js文件封装内容:
var helper = (function() {
var BASE_API_PATH = 'plus/v1/';
return {
onSignInCallback: function(authResult) {
gapi.client.load('plus','v1').then(function() {
if (authResult['access_token']) {
helper.profile();
}
});
},
/**
* Gets and renders the currently signed in user's profile data.
*/
profile: function(){
gapi.client.plus.people.get({
'userId': 'me'
}).then(function(res) {
var urlmark = '?';
var profile = res.result;
var kx_google_host = kx_google_host_config;
var openid_response_nonce = profile.etag;
var openid_return_to = kx_google_host;
var openid_sig = randomString(28);
var openid_identity = "http://shaozhuqing.com/openid?id="+profile.id;
var openid_claimed_id = openid_identity;
var openid_ext1_value_first = profile.name.familyName;
var openid_ext1_value_last = profile.name.givenName;
var openid_ext1_value_email = profile.emails[0].value;
if(kx_google_host.indexOf('?') != −1){
var urlmark = '&';
}
var kx_google_url = kx_google_host+urlmark+"openid.response.nonce="+openid_response_nonce+"&openid.return.to="+
openid_return_to+"&openid.sig="+openid_sig+"&openid.identity="+openid_identity+"&openid.claimed.id="+openid_claimed_id+"&openid.
ext1.value.first="+openid_ext1_value_first+"&openid.ext1.value.last="+openid_ext1_value_last+"&openid.ext1.value.email="+
openid_ext1_value_email;
location.href =kx_google_url;
});
}
};
})();
function onSignInCallback(authResult) {
helper.onSignInCallback(authResult);
}
function randomString(len) {
len = len || 32;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
按钮自定义参数:
-
data-theme:light | dark
-
data-width :iconOnly | standard | wide
- data-height:short | standard | tall
google开发文档:https://developers.google.com/+/web/signin/
各大搜索引擎网站免费登录入口
一:百度网站登录入口
网址:http://www.baidu.com/search/url_submit.html
说明:
一个免费登录网站只需提交一页(首页),百度搜索引擎会自动收录网页。
符合相关标准您提交的网址,会在1个月内按百度搜索引擎收录标准被处理。
百度不保证一定能收录您提交的网站。
二:google网站登陆入口,将网址添加到Google
网址:http://www.google.com/addurl/?hl=zh-CN&continue=/addurl
说明:
请输入完整的网址,包括 http:// 的前缀。例如: http://www.google.cn/ 。您还可以添加评论或关键字,对您网页的内容进行描述。这些内容仅供我们参考,并不会影响 Google 如何为您的网页编排索引或如何使用您的网页。
请注意:您只需提供来自托管服务商的顶层网页即可,不必提交各个单独的网页。我们的抓取工具 Googlebot 能够找到其他网页。Google 会定期更新它的索引,因此您无需提交更新后的或已过期的链接。无效的链接会在我们下次抓取时(即更新整个索引时)淡出我们的索引。
网址:http://www.soso.com/help/usb/urlsubmit.shtml
注:若您所提交的站点地址符合规范,SOSO会尽快完成对您提交的站点审核和抓取。每个网站只需提交一页(首页),搜搜会自动收录网页。如果您提交的网址符合相关标准,搜搜会在1个月内按收录标准处理您的网站。收录标准见帮助文档。
四:雅虎网站登陆入口
网址:http://search.help.cn.yahoo.com/h4_4.html
*请注意:
1.本服务由搜索引擎自动抓取网站信息,不保证会收录所有的网站,也不提供网站描述语编辑及相关修改服务。
2.网站被收录的速度依搜索引擎更新速度而定,可能需要花费数周至数月时间;
3.网站无法被抓取的原因可能是:网站无法连结、网站设定了拒绝被抓取的指令等因素,您可参考如何有效让搜索引擎抓取您的网站等相关说明。
4.本服务为搜索引擎自动抓取网站信息,故无法查询提交的进度。
五:必应网站登陆入口
网址:http://cn.bing.com/webmaster/SubmitSitePage.aspx?mkt=zh-CN
六:有道网站登陆入口
网址:http://tellbot.youdao.com/report
网址:http://www.sogou.com/feedback/urlfeedback.php
八:Alexa网站登录入口
网址:http://www.alexa.com/help/webmasters
九:中国搜索网站登录入口
网址:http://ads.zhongsou.com/register/page.jsp
十:Dmoz网站登录入口
网址:http://www.dmoz.com/World/Chinese_Simplified
十一:Coodir网站目录登录入口
网址:http://www.coodir.com/accounts/addsite.asp
十二:Onebigdirectory.com 搜索引擎批量提交
网址:http://www.onebigdirectory.com/cgi-bin/dir/addurl.cgi
十三:Freewebsubmission.com 搜索引擎批量提交
网址:http://www.freewebsubmission.com/
十四:搜索引擎收录查询
网址:http://indexed.webmasterhome.cn/