Kerberos 的基本原理已在上文(Kerberos 入門實戰(1)--Kerberos 基本原理)介紹過了,本文主要介紹 Kerberos 的安裝及使用,文中使用到的軟件版本:CentOS 7.6、Kerberos5 1.15.1。
1、規划
ip | 主機名 | 用途 |
10.49.196.10 | pxc1 | Kerberos Server |
10.49.196.11 | pxc2 | Kerberos Client |
10.49.196.12 | pxc3 | Kerberos Client |
三台機器上修改 /etc/hosts 文件:
10.49.196.10 pxc1 10.49.196.11 pxc2 10.49.196.12 pxc3
Kerberos Client 根據需要進行安裝,安裝后可以使用 kadmin 命令;對應在 Kerberos Server 上使用 kadmin.local 命令。
2、Kerberos Server 安裝
在 10.49.196.10 上執行如下命令:
yum install krb5-server
2.1、配置 krb5.conf
根據需要修改 /etc/krb5.conf:
# Configuration snippets may be placed in this directory as well includedir /etc/krb5.conf.d/ [logging] default = FILE:/var/log/krb5libs.log kdc = FILE:/var/log/krb5kdc.log admin_server = FILE:/var/log/kadmind.log [libdefaults] dns_lookup_realm = false ticket_lifetime = 24h renew_lifetime = 7d forwardable = true rdns = false pkinit_anchors = FILE:/etc/pki/tls/certs/ca-bundle.crt default_realm = ABC.COM default_ccache_name = KEYRING:persistent:%{uid} [realms] ABC.COM = { kdc = 10.49.196.10 admin_server = 10.49.196.10 } [domain_realm] # .example.com = EXAMPLE.COM # example.com = EXAMPLE.COM
相關參數說明:
[logging]:日志的位置
[libdefaults]:每種連接的默認配置
dns_lookup_realm:是否通過 dns 查找需使用的 releam
ticket_lifetime:憑證的有效時限,一般為 24 小時
renew_lifetime:憑證最長可以被延期的時限,一般為一周。當憑證過期之后,對安全認證的服務后續訪問就會失敗
forwardable:ticket 是否可以被轉發(如果用戶已經有了一個TGT,當他登入到另一個遠程系統,KDC會為他重新創建一個TGT,而不需要讓用戶重新進行身份認證)
rdns:如果為 true,則除根據 hostname 正向查找外,同時反向查找對應的 principal。如果 dns_canonicalize_hostname 設置為 false,則此標志不起作用。默認值為 true。
pkinit_anchors:受信任錨(根)證書的位置;如果用戶在命令行上指定X509_anchors,則不使用該配置。
default_realm:默認的 realm,必須跟要配置的 realm 名稱一致
default_ccache_name:指定默認憑據緩存的名稱。默認值為 DEFCCNAME
[realms]:列舉使用的 realm
kdc:kdc 運行的機器
admin_server:kdc 數據庫管理服務運行的機器
[domain_realm]:配置 domain name 或 hostname 對應的 releam
詳細說明可參考官網文檔:http://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/krb5_conf.html。
2.2、配置 kdc.conf
根據需要修改 /var/kerberos/krb5kdc/kdc.conf:
[kdcdefaults] kdc_ports = 88 kdc_tcp_ports = 88 [realms] ABC.COM = { #master_key_type = aes256-cts acl_file = /var/kerberos/krb5kdc/kadm5.acl dict_file = /usr/share/dict/words admin_keytab = /var/kerberos/krb5kdc/kadm5.keytab supported_enctypes = aes256-cts:normal aes128-cts:normal des3-hmac-sha1:normal arcfour-hmac:normal camellia256-cts:normal camellia12 8-cts:normal des-hmac-sha1:normal des-cbc-md5:normal des-cbc-crc:normal }
相關參數說明:
[kdcdefaults]:KDC 默認配置
kdc_ports:UDP 端口號
kdc_tcp_ports:TCP 端口號
[realms]:realm 數據庫配置
master_key_type:主密鑰的密鑰類型;默認值為 aes256-cts-hmac-sha1-96。
acl_file:用於指定哪些用戶可以訪問 kdc 數據庫的控制文件;如果不需要現在用戶訪問,該值可以設為空
dict_file:字典文件位置,該文件中的單詞不能被用於密碼;如果文件為空,或者沒有為用戶分配策略,則不會執行密碼字典檢查。
admin_keytab:KDC 進行校驗的 keytab。
supported_enctypes:支持的加密方式,默認為 aes256-cts-hmac-sha1-96:normal aes128-cts-hmac-sha1-96:normal。
詳細說明可參考官網文檔:https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/kdc_conf.html。
2.3、配置 kdc.conf
根據需要修改 /var/kerberos/krb5kdc/kadm5.acl:
*/admin@ABC.COM *
Kerberos kadmind 使用該文件來管理對 Kerberos 數據庫的訪問權限。對於影響 principa 的操作,ACL 文件還控制哪些 principa 可以對哪些其他 principa 進行操作。文件格式如下:
principal permissions [target_principal [restrictions] ]
ACL 文件中的行順序很重要,會使用第一個匹配的行來設置用戶權限。
相關參數說明:
principal:設置該 principal 的權限;principal 的每個部分都可以使用 *。
permissions: 權限,有如下一些權限:
a | [Dis]allows the addition of principals or policies |
c | [Dis]allows the changing of passwords for principals |
d | [Dis]allows the deletion of principals or policies |
e | [Dis]allows the extraction of principal keys |
i | [Dis]allows inquiries about principals or policies |
l | [Dis]allows the listing of all principals or policies |
m | [Dis]allows the modification of principals or policies |
p | [Dis]allows the propagation of the principal database (used in Incremental database propagation) |
s | [Dis]allows the explicit setting of the key for a principal |
x | Short for admcilsp. All privileges (except e ) |
* | Same as x. |
target_principal:目標 principal,目標 principal 的每個部分都可以使用 *。
restrictions:針對權限的一些補充限制,如:限制創建的 principal 的票據最長時效。
詳細說明可參考官網文檔:https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/kadm5_acl.html
2.4、創建 Kerberos 數據庫
執行如下命令:
kdb5_util create -s -r ABC.COM
-s:表示生成 stash file,並在其中存儲 master server key(krb5kdc)
-r:指定 realm name
Kerberos 數據庫的目錄為:/var/kerberos/krb5kdc,如果需要重建數據庫,可刪除改目錄。
2.5、啟停 Kerberos 服務
啟動:
systemctl start krb5kdc
systemctl start kadmin
停止:
systemctl stop krb5kdc
systemctl stop kadmin
2.6、kadmin.local
Kerberos 服務機器上可以使用 kadmin.local 來執行各種管理的操作。進入 kadmin.local:
[root@pxc1 kerberos]# kadmin.local Authenticating as principal root/admin@ABC.COM with password. kadmin.local:
常用操作:
操作 | 描述 | 例子 |
add_principal, addprinc, ank | 增加 principal | add_principal -rnadkey test@ABC.COM |
delete_principal, delprinc | 刪除 principal | delete_principal test@ABC.COM |
modify_principal, modprinc | 修改 principal | modify_principal test@ABC.COM |
rename_principal, renprinc | 重命名 principal | rename_principal test@ABC.COM test2@ABC.COM |
get_principal, getprinc | 獲取 principal | get_principal test@ABC.COM |
list_principals, listprincs, get_principals, getprincs | 顯示所有 principal | listprincs |
ktadd, xst | 導出條目到 keytab | xst -k /root/test.keytab test@ABC.COM |
這里先創建一個 principal,方便 Kerberos Client 登錄 kadmin:
kadmin.local: add_principal root/admin@ABC.COM WARNING: no policy specified for root/admin@ABC.COM; defaulting to no policy Enter password for principal "root/admin@ABC.COM": Re-enter password for principal "root/admin@ABC.COM": Principal "root/admin@ABC.COM" created. kadmin.local:
輸入兩次密碼,創建成功。
3、Kerberos Client 安裝
在 10.49.196.11、10.49.196.12 上執行如下命令:
yum install krb5-workstation
3.1、配置 krb5.conf
從 10.49.196.10 上拷貝 /etc/krb5.conf 並覆蓋本地的 /etc/krb5.conf。
3.2、kadmin
Kerberos 客戶端機器上可以使用 kadmin 來執行各種管理的操作。需先在 Kerbers Server 上創建登錄的 principal,默認為 {當前用戶}/admin@realm;
[root@pxc2 krb5]# kadmin Authenticating as principal root/admin@ABC.COM with password. Password for root/admin@ABC.COM: kadmin:
輸入密碼,登錄成功。kadmin 的操作和 kadmin.local 類似。
3.3、kinit(在客戶端認證用戶)
[root@pxc2 krb5]# kinit root/admin@ABC.COM Password for root/admin@ABC.COM:
輸入密碼認證成功。
3.4、klist(查看當前的認證用戶)
[root@pxc2 krb5]# klist Ticket cache: KEYRING:persistent:0:0 Default principal: root/admin@ABC.COM Valid starting Expires Service principal 2021-12-03T17:23:07 2021-12-04T17:23:07 krbtgt/ABC.COM@ABC.COM
3.5、kdestroy(刪除當前的認證緩存)
[root@pxc2 krb5]# kdestroy