如何在CentOS 7中设置LDAP服务器

时间:2020-03-21 11:47:06  来源:igfitidea点击:

今天,我们将使用389目录服务器在CentOS 7中设置LDAP服务器。

让我们开始在CentOS 7中部署389 DS。
本教程在CentOS 7 64位服务器中进行了测试,但这些步骤对于Fedora,Scientific Linux和Rhel等也是一样的。

这是我的测试环境:

  • 操作系统:CentOS 7 64位最小安装
  • IP地址:192.168.1.150/24
  • hostname:server.theitroad.lan.

准备工作

在安装LDAP之前,我们需要执行以下任务。

配置DNS服务器(可选)

确保在DNS服务器中拥有CentOS服务器的IPA地址和FQDN。
请参阅此链接以了解如何在DNS服务器中记录。

但是,这是可选的。
如果我们打算在本地网络中使用LDAP服务器,则可以跳过此操作。

配置/etc/hosts文件中的fqdn

编辑/etc/hosts文件:

vi /etc/hosts

输入服务器的完全限定域名。
这个很重要。

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.150 server.theitroad.lan server

保存并关闭文件。

配置防火墙或者路由器

我们需要允许通过防火墙或者路由器允许LDAP服务器的默认端口,以便从远程系统访问LDAP服务器。

为此,逐个运行以下命令:

firewall-cmd --permanent --add-port=389/tcp
firewall-cmd --permanent --add-port=636/tcp
firewall-cmd --permanent --add-port=9830/tcp

重新启动防火墙服务以保存更改。

firewall-cmd --reload

添加epel存储库

CentOS官方存储库中不提供389个DS。
因此,让我们添加并启用涡换素存储库以安装389个DS。

要安装epel存储库,请运行:

yum install epel-release

调整系统性能和安全调整

我们必须做一些安全调整调整,以增强LDAP服务器的性能。

为此,请编辑/etc/sysctl.conf文件:

vi /etc/sysctl.conf

添加以下行:

net.ipv4.tcp_keepalive_time = 300
net.ipv4.ip_local_port_range = 1024 65000
fs.file-max = 64000

验证设置:

sysctl -p

示例输出:

net.ipv4.tcp_keepalive_time = 300
net.ipv4.ip_local_port_range = 1024 65000
fs.file-max = 64000

编辑/etc/security/limits.conf文件:

vi /etc/security/limits.conf

在底部添加以下行:

*               soft     nofile          8192   
*               hard     nofile          8192

保存并关闭文件。

编辑/etc/profile文件:

vi /etc/profile

添加以下行:

ulimit -n 8192

编辑/etc/pam.d/login文件:

vi /etc/pam.d/login

结束时添加以下行:

session    required     /lib/security/pam_limits.so

保存并关闭文件。

创建LDAP用户帐户

创建一个名为ldapuser的新用户。

useradd ldapuser

设置新用户的密码:

passwd ldapuser

重新启动CentOS服务器以生效所有更改。

安装LDAP服务器

确保已添加Epel存储库并运行以下命令以安装389个DS服务器:

yum install 389-ds-base 389-admin

配置389目录服务器

安装它后,请运行以下命令来配置我们的LDAP服务器。

setup-ds-admin.pl

示例输出:

==============================================================================
This program will set up the 389 Directory and Administration Servers.
It is recommended that you have "root" privilege to set up the software.
Tips for using this program:
 - Press "Enter" to choose the default and go to the next screen
 - Type "Control-B" then "Enter" to go back to the previous screen
 - Type "Control-C" to cancel the setup program
Would you like to continue with set up? [yes]: ## Press Enter
==============================================================================
Your system has been scanned for potential problems, missing patches,
etc. The following output is a report of the items found that need to
be addressed before running this software in a production
environment.
389 Directory Server system tuning analysis version 23-FEBRUARY-2012.
NOTICE : System is x86_64-unknown-linux3.10.0-327.22.2.el7.x86_64 (1 processor).
Would you like to continue? [yes]: ## Press Enter
==============================================================================
Choose a setup type:
 1. Express
 Allows you to quickly set up the servers using the most
 common options and pre-defined defaults. Useful for quick
 evaluation of the products.
 2. Typical
 Allows you to specify common defaults and options.
 3. Custom
 Allows you to specify more advanced options. This is 
 recommended for experienced server administrators only.
To accept the default shown in brackets, press the Enter key.
Choose a setup type [2]: ## Press ENTER
==============================================================================
Enter the fully qualified domain name of the computer
on which you're setting up server software. Using the form
<hostname>.<domainname>
Example: eros.example.com.
To accept the default shown in brackets, press the Enter key.
Warning: This step Jan take a few minutes if your DNS servers
can not be reached or if DNS is not configured correctly. If
you would rather not wait, hit Ctrl-C and run this program again
with the following command line option to specify the hostname:
 General.FullMachineName=your.hostname.domain.name
Computer name [server.theitroad.lan]: ## Press ENTER
==============================================================================
The servers must run as a specific user in a specific group.
It is strongly recommended that this user should have no privileges
on the computer (i.e. a non-root user). The setup procedure
will give this user/group some permissions in specific paths/files
to perform server-specific operations.
If you have not yet created a user and group for the servers,
create this user and group using your native operating
system utilities.
System User [nobody]: ldapuser ## Enter Ldap username which we created earlier
System Group [nobody]: ldapuser ## Enter LDAP group name (It is same as username)
==============================================================================
Server information is stored in the configuration directory server.
This information is used by the console and administration server to
configure and manage your servers. If you have already set up a
configuration directory server, you should register any servers you
set up or create with the configuration server. To do so, the
following information about the configuration server is required: the
fully qualified host name of the form
<hostname>.<domainname>(e.g. hostname.example.com), the port number
(default 389), the suffix, the DN and password of a user having
permission to write the configuration information, usually the
configuration directory administrator, and if you are using security
(TLS/SSL). If you are using TLS/SSL, specify the TLS/SSL (LDAPS) port
number (default 636) instead of the regular LDAP port number, and
provide the CA certificate (in PEM/ASCII format).
If you do not yet have a configuration directory server, enter 'No' to
be prompted to set up one.
Do you want to register this software with an existing
configuration directory server? [no]: ## Press ENTER
==============================================================================
Please enter the administrator ID for the configuration directory
server. This is the ID typically used to log in to the console. You
will also be prompted for the password.
Configuration directory server
administrator ID [admin]: ## Press ENTER
Password: ## Enter LDAP administrative user password
Password (confirm): ## Re-enter password
==============================================================================
The information stored in the configuration directory server can be
separated into different Administration Domains. If you are managing
multiple software releases at the same time, or managing information
about multiple domains, you Jan use the Administration Domain to keep
them separate.
If you are not using administrative domains, press Enter to select the
default. Otherwise, enter some descriptive, unique name for the
administration domain, such as the name of the organization
responsible for managing the domain.
Administration Domain [theitroad.lan]: ## Press ENTER
==============================================================================
The standard directory server network port number is 389. However, if
you are not logged as the superuser, or port 389 is in use, the
default value will be a random unused port number greater than 1024.
If you want to use port 389, make sure that you are logged in as the
superuser, that port 389 is not in use.
Directory server network port [389]: ## Press ENTER
==============================================================================
Each instance of a directory server requires a unique identifier.
This identifier is used to name the various
instance specific files and directories in the file system,
as well as for other uses as a server instance identifier.
Directory server identifier [server]: ## Press ENTER 
==============================================================================
The suffix is the root of your directory tree. The suffix must be a valid DN.
It is recommended that you use the dc=domaincomponent suffix convention.
For example, if your domain is example.com,
you should use dc=example,dc=com for your suffix.
Setup will create this initial suffix for you,
but you Jan have more than one suffix.
Use the directory server utilities to create additional suffixes.
Suffix [dc=theitroad, dc=lan]: ## Press ENTER
==============================================================================
Certain directory server operations require an administrative user.
This user is referred to as the Directory Manager and typically has a
bind Distinguished Name (DN) of cn=Directory Manager.
You will also be prompted for the password for this user. The password must
be at least 8 characters long, and contain no spaces.
Press Control-B or type the word "back", then Enter to back up and start over.
Directory Manager DN [cn=Directory Manager]: ## Press ENTER
==============================================================================
Certain directory server operations require an administrative user.
This user is referred to as the Directory Manager and typically has a
bind Distinguished Name (DN) of cn=Directory Manager.
You will also be prompted for the password for this user. The password must
be at least 8 characters long, and contain no spaces.
Press Control-B or type the word "back", then Enter to back up and start over.
Directory Manager DN [cn=Directory Manager]: ## Press ENTER
Password: ## Enter Password for directory manager
Password (confirm): ## Re-enter password
==============================================================================
The Administration Server is separate from any of your web or application
servers since it listens to a different port and access to it is
restricted.
Pick a port number between 1024 and 65535 to run your Administration
Server on. You should NOT use a port number which you plan to
run a web or application server on, rather, select a number which you
will remember and which will not be used for anything else.
Administration port [9830]: ## Press ENTER
==============================================================================
The interactive phase is complete. The script will now set up your
servers. Enter No or go Back if you want to change something.
Are you ready to set up your servers? [yes]: ## Press ENTER
Creating directory server . . .
Your new DS instance 'server' was successfully created.
Creating the configuration directory server . . .
Beginning Admin Server creation . . .
Creating Admin Server files and directories . . .
Updating adm.conf . . .
Updating admpw . . .
Registering admin server with the configuration directory server . . .
Updating adm.conf with information from configuration directory server . . .
Updating the configuration for the httpd engine . . .
Starting admin server . . .
The admin server was successfully started.
Admin server was successfully created, configured, and started.
Exiting . . .
Log file is '/tmp/setup46mxl0.log'

LDAP服务器已安装和配置。

测试LDAP服务器

配置LDAP服务器后,运行以下命令以验证它是否有效。

为此,运行:

ldapsearch -x -b "dc=theitroad,dc=lan"

示例输出:

# extended LDIF
## LDAPv3
# base <dc=theitroad,dc=lan> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
## theitroad.lan
dn: dc=theitroad,dc=lan
objectClass: top
objectClass: domain
dc: theitroad
# Directory Administrators, theitroad.lan
dn: cn=Directory Administrators,dc=theitroad,dc=lan
objectClass: top
objectClass: groupofuniquenames
cn: Directory Administrators
uniqueMember: cn=Directory Manager
# Groups, theitroad.lan
dn: ou=Groups,dc=theitroad,dc=lan
objectClass: top
objectClass: organizationalunit
ou: Groups
# People, theitroad.lan
dn: ou=People,dc=theitroad,dc=lan
objectClass: top
objectClass: organizationalunit
ou: People
# Special Users, theitroad.lan
dn: ou=Special Users,dc=theitroad,dc=lan
objectClass: top
objectClass: organizationalUnit
ou: Special Users
description: Special Administrative Accounts
# Accounting Managers, Groups, theitroad.lan
dn: cn=Accounting Managers,ou=Groups,dc=theitroad,dc=lan
objectClass: top
objectClass: groupOfUniqueNames
cn: Accounting Managers
ou: groups
description: People who can manage accounting entries
uniqueMember: cn=Directory Manager
# HR Managers, Groups, theitroad.lan
dn: cn=HR Managers,ou=Groups,dc=theitroad,dc=lan
objectClass: top
objectClass: groupOfUniqueNames
cn: HR Managers
ou: groups
description: People who can manage HR entries
uniqueMember: cn=Directory Manager
# QA Managers, Groups, theitroad.lan
dn: cn=QA Managers,ou=Groups,dc=theitroad,dc=lan
objectClass: top
objectClass: groupOfUniqueNames
cn: QA Managers
ou: groups
description: People who can manage QA entries
uniqueMember: cn=Directory Manager
# PD Managers, Groups, theitroad.lan
dn: cn=PD Managers,ou=Groups,dc=theitroad,dc=lan
objectClass: top
objectClass: groupOfUniqueNames
cn: PD Managers
ou: groups
description: People who can manage engineer entries
uniqueMember: cn=Directory Manager
# search result
search: 2
result: 0 Success
# numResponses: 10
# numEntries: 9

如果我们获得搜索结果:2在上面的任何地方,祝贺! 389目录服务器正在工作!!

启动和停止389-DS服务

每次重新启动时都会自动启用Directory Server和Directory admin服务:

systemctl enable dirsrv.target
systemctl enable dirsrv-admin

启动Directory Server的服务:

systemctl start dirsrv.target

或者

start-dirsrv

停止Directory Server的服务:

systemctl stop dirsrv.target

或者

stop-dirsrv

启动目录admin:

systemctl start dirsrv-admin

或者

start-ds-admin

停止目录管理员:

systemctl stop dirsrv-admin

或者

stop-ds-admin

检查服务的状态:

systemctl status dirsrv.target
systemctl status dirsrv-admin

重新启动服务:

systemctl restart dirsrv.target
systemctl restart dirsrv-admin

我们可以在/etc/dirsrv /目录下找到所有配置文件以及/var/log/dirsrv /目录下的所有日志文件。

在此阶段,LDAP服务器已准备好使用。