LDAP [Phần 1] – Hướng dẫn cài đặt LDAP trên CentOS 7

30/12/2020

LDAP viết tắt của Lightweight Directory Access Protocol là một giao thức dạng client-server được phát triển trên chuẩn X500 có chức năng phục vụ cho việc truy cập dịch vụ thư mục. LDAP có thể được sử dụng để lưu trữ bất kỳ loại thông tin nào và thường được sử dụng như một thành phần của hệ thống xác thực tập chung.

Mục lục

  1. Cấu hình đề nghị
  2. Cài đặt LDAP trên CentOS 7
  3. Cài đặt PHP Ldap Admin để quản lý tài khoản LDAP trên giao diện

1. Cấu hình đề nghị

Để cài đặt thành công LDAP, cấu hình đề nghị tối thiểu như sau:

  • Hệ điều hành: CentOS 7
  • RAM: 1GB
  • CPU: 1 Core
  • DISK: 15GB

2. Cài đặt LDAP trên CentOS 7

Cấu hình hostname

hostnamectl set-hostname "LDAP" exec bash

Tắt firewalld

systemctl stop firewalld systemctl disable firewalld

Tắt Selinux

sudo setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/sysconfig/selinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config

Cài đặt epel-release và cập nhật các gói phần mềm

yum install epel-release -y yum update -y

Khởi động lại máy để lấy lại cấu hình mới nhất :

init 6

Cài đặt OpenLDAP

yum -y install openldap-servers openldap-clients

Sao chép file cấu hình và phân quyền

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG chown ldap. /var/lib/ldap/DB_CONFIG 

Khởi động slapd

systemctl start slapd systemctl enable slapd

Thiết lập LDAP admin password, tạo mật khẩu

[root@ldap ~]# slappasswd New password: Re-enter new password: {SSHA}xxxxxxxxxxxxxxxxxxxxxLDAPPASS1

Thêm mới file chroot.ldif

cat > chrootpw.ldif << EOF # Chỉ định mật khẩu được tạo ở trên cho phần "olcRootPW" dn: olcDatabase={0}config,cn=config changetype: modify add: olcRootPW olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxLDAPPASS1 EOF

Chạy lệnh sau để update thông tin từ file chroot.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif

Kết quả

Import các schemas:

ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

Kết quả

Thiết lập Manager Password, tạo mật khẩu :

# Tạo mật khẩu cho người quản lý [root@ldap ~]# slappasswd New password: Re-enter new password: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxxLDAPPASS2

Thêm mới file chdomain.ldif:

cat > chdomain.ldif << EOF # Thay thế domain của bạn và các section dc=**,dc=** # Chỉ định mật khẩu vừa được tạo ở trên vào phần "olcRootPW" dn: olcDatabase={1}monitor,cn=config changetype: modify replace: olcAccess olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth"   read by dn.base="cn=Manager,dc=Onet,dc=local" read by * none  dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcSuffix olcSuffix: dc=Onet,dc=local  dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcRootDN olcRootDN: cn=Manager,dc=Onet,dc=local  dn: olcDatabase={2}hdb,cn=config changetype: modify add: olcRootPW olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxxLDAPPASS2  dn: olcDatabase={2}hdb,cn=config changetype: modify add: olcAccess olcAccess: {0}to attrs=userPassword,shadowLastChange by   dn="cn=Manager,dc=Onet,dc=local" write by anonymous auth by self write by * none olcAccess: {1}to dn.base="" by * read olcAccess: {2}to * by dn="cn=Manager,dc=Onet,dc=local" write by * read EOF

Chạy lệnh sau để update thông tin:

ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif 

Kết quả

Thêm file basedomain.ldif:

cat > basedomain.ldif << EOF # Thay thế domain của bạn và các section dc=**,dc=** dn: dc=Onet,dc=local objectClass: top objectClass: dcObject objectclass: organization o: Onet Local dc: Onet  dn: cn=Manager,dc=Onet,dc=local objectClass: organizationalRole cn: Manager description: Directory Manager  dn: ou=People,dc=Onet,dc=local objectClass: organizationalUnit ou: People  dn: ou=Group,dc=Onet,dc=local objectClass: organizationalUnit ou: Group EOF

Cập nhật thông tin của basedomain

ldapadd -x -D cn=Manager,dc=Onet,dc=local -W -f basedomain.ldif  Enter LDAP Password: # password của manager

Kết quả

Sau khi thực hiện xong các bước chúng ta sử dụng lệnh sau để kiểm tra các entry:

slapcat

Kết quả

Để thêm mới một entry chúng ta cần tạo ra file ldif và update thông tin các file ldif đó và dùng slapcat để kiểm tra.

Ví dụ về thêm một entry user :

cat > adduser_1.ldif << EOF dn: cn=adduser_1,ou=People,dc=Onet,dc=local objectClass: person objectClass: inetOrgPerson userPassword:: V2VsY29tZTEyMw== sn: user cn: adduser_1 EOF

Update file adduser_1.ldif để thông tin user được thêm vào cây LDAP :

ldapadd -x -D cn=Manager,dc=Onet,dc=local -W -f adduser_1.ldif

3. Cài đặt PHP LDAP Admin để quản trị LDAP trên giao diện

Cài đặt httpd

yum -y install httpd

Mở file /etc/httpd/conf/httpd.conf và sửa đổi các thông tin sau :

# Tại dòng 151 sửa như sau : AllowOverride All  # Tại dòng 164, sửa thông tin như sau: DirectoryIndex index.html index.cgi index.php  # Thêm vào cuối file những cấu hình sau: ServerTokens Prod KeepAlive On 

Khởi động lại httpd

systemctl restart httpd systemctl enable httpd

Cài đặt PHP

yum -y install php php-mbstring php-pear

Mở file /etc/php.ini:

# Tại dòng 878 sửa lại timezone: date.timezone = "Asia/Ho_Chi_Minh"

Cài đặt phpLdapadmin

yum --enablerepo=epel -y install phpldapadmin

Mở file /etc/phpldapadmin/config.php và sửa các thông tin như sau :

# Tại dòng 398: $servers->setValue('login','attr','dn'); // $servers->setValue('login','attr','uid');

Mở file /etc/httpd/conf.d/phpldapadmin.conf và thêm thông tin sau

# Tại dòng 11 sửa lại như  Require all granted

Khởi động lại httpd

systemctl restart httpd

Truy cập vào trang quản trị theo đường dẫn “http://IP_Server/phpldapadmin/”

Kết quả

Tiến hành đăng nhập như sau

Nếu đúng thông tin đăng nhập sẽ chuyển đến màn quản trị LDAP

Cảm ơn bạn đã theo dõi bài viết. Ở những bài viết sau tôi sẽ chia sẻ cách tích hợp LDAP làm hệ thống xác thực tập chung cho một số hệ thống khác.

Chúc các bạn thành công !!!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

[CI/CD] Phần 2: Hướng dẫn cài đặt Jenkins trên CentOS 7

Jenkins là phần mềm tự động hóa, mã nguồn mở và viết bằng Java. Với Jenkins, các nhà phát triển...
30/12/2020

LDAP [Phần 2] – Triển khai dịch vụ LDAP trên Onet Onet

Trong phần trước, Onet đã hướng dẫn các bạn triển khai LDAP trên CentOS 7. Trong bài này, mình sẽ...
30/12/2020

[10 phút ] [Ansible] [Cơ bản] [Phần 5] Viết Playbook cài đặt WordPress trên CentOS 7

Ở bài viết trước, chúng ta đã cùng nhau tìm hiểu về các thành phần, quy tắc cũng như cách viết...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Huớng dẫn dùng proxy cho ios, iphone 2023
23/09/2023

Cách gắn set proxy cho điện thoại android, oppo, giả lập android, Ldplayer Bằng Proxydroid
20/09/2023

Mua Proxy Socks5 VN Chơi Game Gia Lập Tăng Cường Trải Nghiệm Chơi Game
22/06/2023

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại Onet.com.vn
22/06/2023

Mua Proxy Game – Giải pháp tuyệt vời cho việc chơi game trên mạng mà không bị giới hạn về vị trí địa lý
03/06/2023