Linux下使用Google Authenticator配置SSH登錄動態驗證碼

說明:

1、一般ssh登錄服務器,只需要輸入賬號和密碼。

2、本教程的目的:在賬號和密碼之間再增加一個驗證碼,只有輸入正確的驗證碼之後,再輸入密碼才能登錄。這樣就增強了ssh登錄的安全性。

3、賬號、驗證碼、密碼三者缺一個都不能登錄,即使賬號和密碼正確,驗證碼錯誤,同樣登錄失敗。

4、驗證碼:是動態驗證碼,並且是通過手機客戶端自動獲取(默認每隔30秒失效一次)。

5、最終目的:遠程ssh登錄一臺服務器,需要正確的賬號、密碼、及一個可以獲取到動態驗證碼的手機(目前支持Android和ios手機系統)。

具體操作:

操作系統:CentOS

一、關閉SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #註釋掉

#SELINUXTYPE=targeted #註釋掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

二、安裝編輯工具包

1、使用CentOS默認yum源安裝

yum install wget gcc make

yum install pam-devel libpng-devel

2、配置repoforge第三方yum源安裝mercurial包

CentOS各個版本,請選擇正確版本

CentOS 5.x

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.i386.rpm

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.x86_64.rpm

CentOS 6.x

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

yum install mercurial #安裝

系統運維  www.osyunwei.com  溫馨提醒:qihang01原創內容版權所有,轉載請註明出處及原文鏈接

三、安裝google authenticator PAM插件

cd /usr/local/src

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

注意:打開google需要×××,可以先想辦法下載好libpam-google-authenticator-1.0-source.tar.bz2上傳到/usr/local/src目錄進行安裝

tar jxvf libpam-google-authenticator-1.0-source.tar.bz2 #解壓

cd libpam-google-authenticator-1.0 #進入目錄

make #編譯

make install #安裝

四、安裝QrenCode,此工具可以在Linux命令行下生成二維碼

cd /usr/local/src

wget http://fukuchi.org/works/qrencode/qrencode-3.4.4.tar.gz #下載

tar zxf qrencode-3.4.4.tar.gz #解壓

cd qrencode-3.4.4 #進入目錄

./configure --prefix=/usr #配置

make #編譯

make install #安裝

五、配置ssh服務調用google authenticator PAM插件

vi /etc/pam.d/sshd #編輯,在第一行增加以下代碼

auth required pam_google_authenticator.so

:wq! #保存退出

vi /etc/ssh/sshd_config #編輯

ChallengeResponseAuthentication yes #修改no爲yes

:wq! #保存退出

service sshd restart #重啓ssh服務,使配置生效

六、使用google authenticator PAM插件爲ssh登錄賬號生成動態驗證碼

注意:哪個賬號需要動態驗證碼,請切換到該賬號下操作

google-authenticator #運行此命令

Do you want authentication tokens to be time-based (y/n) y #提示是否要基於時間生成令牌,選擇y

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/osyunwei@osyunwei%3Fsecret%3DAXNRWARYQPHI5EAJ

Your new secret key is: AXNRWARYQPHI5WYU

Your verification code is 199255

Your emergency scratch codes are:

21767982

60638828

24009000

44681673

28015662

#上面的網址爲生成的二維碼圖形地址(需要×××才能打開),還會生成密鑰,以及5個緊急驗證碼(當無法獲取動態驗證碼時使用,注意:這5個驗證碼用一個就會少一個!請保存好!)

Do you want me to update your "/home/jss/.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身份驗證器,通過此工具掃描上一步生成的二維碼圖形,獲取動態驗證碼

Android手機下載:

https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2

iOS手機下載:

https://itunes.apple.com/us/app/google-authenticator/id388497605

注意:打開google需要×××,或者自己想辦法下載Google身份驗證器安裝。

另外,還需要安裝條形碼掃描器,用來掃描驗證二維碼,以獲取動態驗證碼

以Android手機爲例:

安裝好Google身份驗證器,打開如下圖所示:

系統運維  www.osyunwei.com  溫馨提醒:qihang01原創內容版權所有,轉載請註明出處及原文鏈接

開始設置-掃描條形碼,然後掃描第六步中生成的二維碼圖形

掃描完成後,如下圖所示:

八、ssh遠程登錄服務器

輸入賬號之後,會提示輸入驗證碼

login as: root

Using keyboard-interactive authentication.

Verification code:

打開手機上的Google身份驗證器,輸入動態驗證碼,回車。

注意:動態驗證碼沒有回顯,所以在屏幕上看不到輸入的內容,但只要保證輸入正確即可!

Using keyboard-interactive authentication.

Password:

接着輸入密碼,即可成功登錄系統!

注意:以此步驟必須在30秒內完成。否則動態驗證碼過期,必須重新操作。

至此,Linux下使用Google Authenticator配置SSH登錄動態驗證碼教程完成!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章