配置Linux使用LDAP用戶認證

来源:https://www.cnblogs.com/somata/archive/2019/09/16/LinuxLDAPUserAuthentication.html
-Advertisement-
Play Games

配置Linux使用LDAP用戶認證 本文首發:https://www.cnblogs.com/somata/p/LinuxLDAPUserAuthentication.html 我這裡使用的是CentOS完成的LDAP用戶管理,可能與網上的大部分教程不同,不過寫出來了,那麼是肯定能用的了,不過會有部 ...


配置Linux使用LDAP用戶認證

本文首發:https://www.cnblogs.com/somata/p/LinuxLDAPUserAuthentication.html

我這裡使用的是CentOS完成的LDAP用戶管理,可能與網上的大部分教程不同,不過寫出來了,那麼是肯定能用的了,不過會有部分文件,忘指教。
這裡使用的 OPENLdap 配合 CentOS7 完成的用戶管理,需要配置 nssswitch 、pam 和 sssd 3個服務,需要先有一定的瞭解才能完成本文的配置。

基礎配置

  1. 完成yum源的配置
mkdir /root/back
tar -Jcvf /root/back/yum.repos.d-`date  '+(%Y.%m.%d_%H:%M:%S)'`.tar.xz /etc/yum.repos.d/
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum,repos.d/CentOS-epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
  1. 安裝必要軟體
yum -y install vim bash-completion openldap-servers openldap-clients nss-pam-ldapd sssd

OPENLdap服務初始化

初始化過程就不再過多贅述,詳細查詢《OPENLDAP 服務搭建和後期管理》。

  1. 首先停止資料庫服務:
systemctl stop slapd
  1. 然後編輯文件:
# 首先備份文件,以免無法複原
mkdir /root/back
tar -Jcvf /root/back/slapd.config-`date  '+(%Y.%m.%d_%H:%M:%S)'`.tar.xz /etc/openldap/slapd.d/
tar -Jcvf /root/back/slapd.data-`date  '+(%Y.%m.%d_%H:%M:%S)'`.tar.xz /var/lib/ldap/
# 然後再刪除配置文件
rm -rf /etc/openldap/slapd.d/*
rm -rf /var/lib/ldap/*
# 複製配置文件到臨時目錄
mkdir /root/ldap
cd /root/ldap
  1. 編寫slapd的配置文件。這裡的配置文件是從 /usr/share/openldap-servers/slapd.ldif 中演變而來的,主要修改了baseDN(suffix), OPENLDAPTLS,olcRootPW(密碼由 slappasswd 生成,本文中的密碼為: 123456) 和 include。
# file: /root/ldap/slapd.ldif

dn: cn=config
objectClass: olcGlobal
cn: config
olcArgsFile: /var/run/openldap/slapd.args
olcPidFile: /var/run/openldap/slapd.pid
olcTLSCertificateFile: /etc/openldap/certs/server.crt
olcTLSCertificateKeyFile: /etc/openldap/certs/server.key
olcTLSCACertificateFile: /etc/openldap/cacerts/cacert.pem

dn: cn=schema,cn=config
objectClass: olcSchemaConfig
cn: schema

include: file:///etc/openldap/schema/core.ldif
include: file:///etc/openldap/schema/cosine.ldif
include: file:///etc/openldap/schema/nis.ldif
include: file:///etc/openldap/schema/inetorgperson.ldif

dn: olcDatabase=frontend,cn=config
objectClass: olcDatabaseConfig
objectClass: olcFrontendConfig
olcDatabase: frontend

dn: olcDatabase=config,cn=config
objectClass: olcDatabaseConfig
olcDatabase: config
olcAccess: to * 
  by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage 
  by * none

dn: olcDatabase=monitor,cn=config
objectClass: olcDatabaseConfig
olcDatabase: monitor
olcAccess: to * 
  by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read 
  by dn.base="cn=Manager,dc=black,dc=com" read 
  by * none

dn: olcDatabase=hdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: hdb
olcSuffix: dc=black,dc=com
olcRootDN: cn=Manager,dc=black,dc=com
olcRootPW: {SSHA}l1vBI/HOMKLEiQZgcm3Co+hFQI68rH1Q
olcDbDirectory: /var/lib/ldap
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
  1. 配置OPENSSL的證書。為openldap伺服器的加密隧道使用。
# 首先是創建CA服務的證書文件
cd /etc/pki/CA
openssl genrsa -out private/cakey.pem 2048
openssl req -new -x509 -key private/cakey.pem -out cacert.pem   # 這裡的證書創建過程就省略不寫了。
# 不過需要註意的是,必須要保證後面伺服器證書申請的開頭部分必須與證書開頭相同,否則無法通過CA簽發證書。
touch index.txt
echo "01" > serial
# 然後申請簽發伺服器證書
cd /etc/openldap/certs/
openssl genrsa -out server.key 2048
openssl ca -in server.csr -out server.crt -days 365
# 複製CA證書到指定位置
mkdir /etc/openldap/cacerts
cp /etc/pki/CA/cacert.pem /etc/openldap/cacerts/
  1. 根據配置生成伺服器的配置文件
slapadd -F "/etc/openldap/slapd.d/" -b "cn=config" -l slapd.ldif
# 這裡還需要註意文件屬主還是root的,需要改回為openldap
chown -R ldap:ldap /etc/openldap/slapd.d/*
# 然後再開啟服務即可
systemctl start slapd

_#################### 100.00% eta   none elapsed            none fast!          
Closing DB...

  1. 初始化資料庫
dn: dc=black,dc=com
dc: black
objectClass: top
objectClass: domain

dn: cn=Manager,dc=black,dc=com
objectClass: organizationalRole
cn: Manager
description: LDAP Manager

dn: ou=People,dc=black,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
objectClass: domainRelatedObject
associatedDomain: black.com

dn: ou=Group,dc=black,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
objectClass: domainRelatedObject
associatedDomain: black.com
  1. 導入至資料庫
ldapadd -x -D "cn=Manager,dc=black,dc=com" -w 123456 -f  base.ldif  # 將基礎信息導入資料庫。

adding new entry "dc=black,dc=com"
adding new entry "cn=root,dc=black,dc=com"
adding new entry "ou=People,dc=black,dc=com"
adding new entry "ou=Group,dc=black,dc=com"

  1. 那麼再使用apache directory studio來查看 伺服器是否配置成功。

首先先將防火牆開啟

firewall-cmd --add-service=ldap

image.png

  1. 我們這裡先導入一個用戶用於後面的測試使用

image.png
image.png

NSS 、PAM 和 SSSD 配置

NSS服務配置

如果想使 nss 可以查詢ldap,那麼首先就需要啟用一個叫 nslcd 的服務, 以下是該服務的配置文件。

# file: /etc/nslcd.conf

uid nslcd
gid ldap
uri ldap://127.0.0.1/
base dc=black,dc=com
binddn cn=Manager,dc=black,dc=com
bindpw 123456
ssl no
tls_cacertdir /etc/openldap/cacerts

啟動服務

chmod 600 /etc/nslcd.conf
systemctl start nslcd
systemctl enable nslcd

配置nss

# file: /etc/nsswitch.conf

passwd:     files ldap          # 主要就是 passwd、shadow 和 group 這3行需要後面添加ldap
shadow:     files ldap
group:      files ldap
hosts:      files dns myhostname
bootparams: nisplus [NOTFOUND=return] files
ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
services:   files sss
netgroup:   nisplus sss
publickey:  nisplus
automount:  files nisplus sss
aliases:    files nisplus

測試是否可用:

getent passwd | grep black

black❌1001:1001:black:/home/black:/bin/bash

PAM服務配置

這裡PAM的配置文件我是從另一臺使用authconfig-tui 配置內容拷過來的。

# file: /etc/pam.d/system-auth

auth        required      pam_env.so
auth        required      pam_faildelay.so delay=2000000
auth        [default=1 ignore=ignore success=ok] pam_succeed_if.so uid >= 1000 quiet
auth        [default=1 ignore=ignore success=ok] pam_localuser.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 1000 quiet_success
auth        sufficient    pam_sss.so forward_pass
auth        required      pam_deny.so

account     required      pam_unix.so broken_shadow
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 1000 quiet
account     [default=bad success=ok user_unknown=ignore] pam_sss.so
account     required      pam_permit.so

password    requisite     pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password    sufficient    pam_sss.so use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
-session     optional      pam_systemd.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
session     optional      pam_sss.so

PAM不用進行服務重啟,直接可以使用

sssd服務配置

針對LDAP用戶登入,PAM配置會將其轉發給SSSD,由SSSD來認證用戶。

# file: /etc/sssd/sssd.conf

[domain/black.com]
autofs_provider = ldap
cache_credentials = True
ldap_search_base = dc=black,dc=com
id_provider = ldap
auth_provider = ldap
chpass_provider = ldap
ldap_uri = ldap://127.0.0.1/
ldap_id_use_start_tls = True
ldap_tls_reqcert = never
ldap_tls_cacertdir = /etc/openldap/cacerts

[sssd]
services = nss, pam, autofs
domains = black.com

[nss]
homedir_substring = /home

[pam]

[sudo]

[autofs]

[ssh]

[pac]

[ifp]

[secrets]

[session_recording]

配置啟動服務,並且設置開機自啟動。

chmod 600 /etc/sssd/sssd.conf   # 註意許可權的配置,否則無法啟動。
systemctl start sssd
systmctl enable sssd

測試

那麼這樣用戶認證的部分就製作好了,那麼現在來測試一下:
因為配置的 system-auth 文件,並沒有配置 ssh 文件,所以ssh是連接不進去的。因此這裡使用login來測試可行性。
image.png

腳本

我為這個LDAP用戶認證寫了一個腳本,方便添加用戶。我這裡還是要強調一下,CentOS雖然有提供 migrationtools 工具用於將用戶存放至LDAP資料庫,但是如果你把本地用戶全都提到 LDAP 資料庫,不保留本地用戶,那麼你就會發現,電腦就會無法重啟了,所以推薦不要把UID小於1000的用戶存放到 LDAP 伺服器。
腳本放在gitee上了。https://gitee.com/somata/script/blob/master/ldap_adduser.sh
使用方法很簡單
image.png
然後再測試一下這個添加的用戶是否可以登入:
image.png
本文經「原本」原創認證,作者乾坤盤,訪問yuanben.io查詢【5AT479HZ】獲取授權信息。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 此處項目路徑是:C:\GetPathInfo\ ...
  • asp.net core 使用 signalR(一) Intro SignalR 是什麼? ASP.NET Core SignalR 是一個開源代碼庫,它簡化了嚮應用添加實時 Web 功能的過程。 實時 Web 功能使伺服器端代碼能夠即時將內容推送到客戶端。 SignalR 的適用對象: 需要來自服 ...
  • 如果已經看過本章節:目錄傳送門:這是目錄鴨~ 1.場景搭建: 首先我們去AssetStore逛淘寶~~~ 我淘到的是這個資源,其他好看的場景(消耗不高的都行)。 然後我們導入了這個資源後,把資源根文件夾的名字改為Select,把Demo場景文件的名字改為Selection,我這樣修改的emmm... ...
  • static void Main(string[] args) { int i = 0; Parallel.For(0, 100, (x) => { Console.WriteLine(i); i++; }); Console.WriteLine($"i is {i}"); Console.Read ...
  • 本教程僅用作個人學習,請勿用於商業獲利,造成後果自負!!! Pycharm安裝 在這插一個小話題哈,Pycharm只是一個編譯器,並不能代替Python,如果要使用Python,還是需要安裝Python的哈 1、Pycharm下載安裝 Pycharm下載 Pycharm官網:http://www.j ...
  • [TOC] 1. 概述 消息隊列可認為是一個消息鏈表,隊列中的每個消息具有如下屬性: 消息優先順序,由發送者賦予 消息數據長度,可以為0 消息數據(如果消息數據長度大於0) Posix消息隊列主要用於線程間消息的傳遞: A線程向隊列中放置消息,B線程從隊列中取出消息 A線程向隊列寫入消息之前,不需要B ...
  • 我們在虛擬機中安裝好centos後,需要手動配置網卡,得到IP。可以先配置臨時IP,然後在遠程連接軟體中配置永久IP。 ...
  • 大多數人都厭煩使用老舊的系統,無論軟體還是硬體。但有的時候又不得不困守其中,堅持延續著系統的壽命,或者還需要點幾柱香,祈求神佛的護佑。 Linux是一個模塊化極好的操作系統,得益於此,當其中有組件落伍之時,大多數情況下,還能通過下載源碼,手工編譯來升級組件,從而保證系統的可用性。 在這個過程中,cU ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...