簡介
在運維管理中,服務器的密碼管理十分重要。服務器數量少的時候還好說,可以定時來改密碼。一旦數量多了,再來改密碼就不現實了。
前提
我們假定運維訪問服務器是這樣的:
- 創建一個普通用戶用於登錄服務器,給出基礎權限,能進行簡單的日常操作。但不能修改系統層面的東西。這個權限不能做太多的事。
- 切換到root用戶時,不能使用root密碼來進行認證切換。不然密碼泄露,服務器最高權限就算是沒了。這里可以使用動態口令的方式來進行認證切換。
原理
原理圖如下:
這里通過FreeRadius+GoogleAuthenticator實現linux動態口令認證,其原理是:
- 通過開源的GoogleAuthenticator來實現動態口令
- 服務器通過FreeRadius進行認證
- FreeRadius使用PAM+GoogleAuthenticator來進行認證
這樣說可能有點不清楚,我這里再整理下整個流程。流程是這樣的:
- 用戶使用普通用戶登錄業務服務器A 進行操作
- 用戶需要提權,切換到root,需要輸入動態口令來進行切換
- 輸入動態口令
- 業務服務器A 就向動態口令認證服務器B 的FreeRadius進行認證
- 認證服務器B的FreeRadius收到認證請求后,開始認證
- FreeRadius會向GoogleAuthenticator來進行動態口令的校驗,然后將結果返回給業務服務器。
- 業務服務器成功切換到root用戶
安裝部署
服務端
系統:CentOS 7.2
NTP安裝
由於Google Authenticator依賴於時間,所以你的服務器時間必須總是正確的。這里通過ntp服務自動同步網絡時間。
yum install ntp -y
systemctl enable ntpd
systemctl start ntpd
Google Authenticator Pam安裝配置
安裝
git clone https://github.com/google/google-authenticator-libpam.git
cd google-authenticator-libpam/
./bootstrap.sh
./configure
make
make install
ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so
如果出現:
configure: error: Unable to find the PAM library or the PAM header files
請安裝此套件後,重新執行./configure:
# yum install pam-devel
配置pam
/etc/pam.d/sshd
#添加下面一行
auth required pam_google_authenticator.so
#注釋下面一行
#auth substack password-auth
配置ssh
/etc/ssh/sshd_config
# Change to no to disable s/key passwords
ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication no
重啟sshd
systemctl restart sshd.service
為用戶啟用google-authenticator
輸入命令:google-authenticator
1)屏幕提示Do you want authentication tokens to be time-based (y/n) ,回答y選用基於時間的token
2)屏幕提示二維碼,拿出手機打開google authenticator軟件(沒有請自行下載),點擊+后選擇“條形碼掃描"添加認證條目。
---
注意:將屏幕顯示的secret key, verification code 和 recovery codes 保存在安全的地方,供密碼恢復使用。
---
3)Do you want me to update your "/home/sammy/.google_authenticator" file (y/n) y
4)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
4)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
5)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連接(不要關閉當前的防止無法訪問)測試配置是否成功。
FreeRadius 安裝配置
安裝
yum install freeradius freeradius-utils -y
配置
/etc/raddb/radusd.conf
user = radiusd
group = radiusd
#改成
user = root
group = root
/etc/raddb/users
#取消下面2行注釋
DEFAULT Group == "disabled", Auth-Type := Reject
Reply-Message = "Your account has been disabled."
#添加一行
DEFAULT Auth-Type := PAM
/etc/raddb/sites-enabled/default
authenticate {
...
# Pluggable Authentication Modules
# pam
...
}
#取消pam行前面的注釋
freeradius啟用pam模塊
ln -s /etc/raddb/mods-available/pam /etc/raddb/mods-enabled/pam
/etc/raddb/clients.conf
#在最后添加一段認證類型
client macolee_sudo {
ipaddr = 0.0.0.0
netmask = 0
secret = macolee_sudo
require_message_authenticator = no
}
/etc/pam.d/radiusd
#%PAM-1.0
#注釋掉下面一行
#auth include password-auth
#添加下面一行。secret是google Authenticator認證的配置文件,user是切換為root的時候需要認證
auth required pam_google_authenticator.so secret=/etc/raddb/gauth user=root
account required pam_nologin.so
account include password-auth
password include password-auth
session include password-auth
啟動freeradius
systemctl enable radiusd
systemctl start radiusd
打開防火牆限制,開通FreeRadius的1812端口
/etc/sysconfig/iptables
-A INPUT -s 10.1.2.97 -p udp -m udp --dport 1812 -j ACCEPT
客戶端
pam_radius安裝配置
安裝
#添加epel yum源
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
#安裝
yum install pam_radius -y
配置
...
# server[:port] shared_secret timeout (s)
# 127.0.0.1 secret 1
# other-server other-secret 3
#添加認證服務器地址 認證類型
10.214.60.37:1812 macolee_sudo 10
修改sudo的pam配置
#%PAM-1.0
#添加下面一行
auth required pam_radius_auth.so
#注釋下面一行
#auth include system-auth
account include system-auth
password include system-auth
session optional pam_keyinit.so revoke
session required pam_limits.so
驗證
前提
客戶端服務器上的用戶名,在radius服務端也必須存在。比如客戶端要切換為root的普通用戶是test1,那么radius服務端也必須有test1這個普通用戶
客戶端和服務端添加用戶
#添加用戶
useradd test1
#給用戶sudo權限
命令:visudo
#添加
macolee ALL=(ALL) /bin/su - root
測試登錄
> 1. 以test1用戶登錄客戶端服務器
> 2. 輸入名sudo su - root
> 3. 輸入手機app上google驗證器上的驗證碼
> 4. 能成功切換,表示配置成功