Google Authenticator + SSH public key 实现两步认证

OpenSSH 版本6.2以上才支持AuthenticationMethods

安装依赖:

Red Hat, CentOS, Fedora 系统需要安装 pam-devel 包:

yum install git pam-devel make gcc-c++ wget -y

Ubuntu,Linux Mint,Debian 系统需要安装‘libpam0g-dev包:

apt-get install git libpam0g-dev make gcc-c++ wget -y

谷歌身份验证器模块:

git clone https://github.com/google/google-authenticator.git
cd google-authenticator/libpam
./bootstrap.sh
make && make install

运行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/[email protected]%3Fsecret%XXXXXXXXXXXXXX
Your new secret key is: XXXXXXXXXXXX
Your verification code is 000000
紧急码,只能使用一次
Your emergency scratch codes are:
  80801116
  43011118
  34981118
  88321117
  93711113

Do you want me to update your "/root/.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秒生成一个token
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

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

配置SSH使用谷歌身份验证器模块

编辑/etc/pam.d/sshd 添加如下:

auth sufficient pam_google_authenticator.so
注释:
auth       include      password-auth

编辑/etc/ssh/sshd_config,修改

PubkeyAuthentication yes
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
PasswordAuthentication no
UsePAM yes

重启SSH服务

service sshd restart

手机端安装Google 身份验证器

Andorid版
https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2
iOS版
https://itunes.apple.com/cn/app/google-authenticator/id388497605
手机端打开google 身份验证器,扫二维码或者输入secret key

[[email protected] ~]# ssh 127.0.0.1
Authenticated with partial success.
Verification code: 6位动态码
Last login: Thu Aug 14 15:20:16 2015 from 127.0.0.1
[[email protected] ~]#