LDAP [Phần 1] - Hướng Dẫn Cài đặt LDAP Trên CentOS 7

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=cloud365,dc=local" read by * none dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcSuffix olcSuffix: dc=cloud365,dc=local dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcRootDN olcRootDN: cn=Manager,dc=cloud365,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=cloud365,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=cloud365,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=cloud365,dc=local objectClass: top objectClass: dcObject objectclass: organization o: Cloud365 Local dc: Cloud365 dn: cn=Manager,dc=cloud365,dc=local objectClass: organizationalRole cn: Manager description: Directory Manager dn: ou=People,dc=cloud365,dc=local objectClass: organizationalUnit ou: People dn: ou=Group,dc=cloud365,dc=local objectClass: organizationalUnit ou: Group EOF

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

ldapadd -x -D cn=Manager,dc=cloud365,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=cloud365,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=cloud365,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 !!!

Từ khóa » Cài đặt Ldap Trên Linux