CentOS下Google Authenticator配置SSH登入動態驗證碼配置

來源:互聯網
上載者:User

說明:

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 #安裝
 三、安裝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身分識別驗證器,開啟如下圖所示:

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

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

八、ssh遠程登入伺服器

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

login as: root
Using keyboard-interactive authentication.
Verification code:
開啟手機上的Google身分識別驗證器,輸入動態驗證碼,斷行符號。
注意:動態驗證碼沒有回顯,所以在螢幕上看不到輸入的內容,但只要保證輸入正確即可!
Using keyboard-interactive authentication.
Password:
接著輸入密碼,即可成功登入系統!
注意:以此步驟必須在30秒內完成。否則動態驗證碼到期,必須重新操作。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.