Showing posts with label LDAP Configuration. Show all posts
Showing posts with label LDAP Configuration. Show all posts

How to Add LDAP Users and Groups in OpenLDAP on Linux

To add something to the LDAP directory, you need to first create a LDIF file.

The ldif file should contain definitions for all attributes that are required for the entries that you want to create.
With this ldif file, you can use ldapadd command to import the entries into the directory as explained in this tutorial.

Create a LDIF file for New User

The following is a sample LDIF file that will be used to create a new user.
# cat adam.ldif
dn: uid=adam,ou=users,dc=tgs,dc=com
objectClass: top
objectClass: account
objectClass: posixAccount
objectClass: shadowAccount
cn: adam
uid: adam
uidNumber: 16859
gidNumber: 100
homeDirectory: /home/adam
loginShell: /bin/bash
gecos: adam
userPassword: {crypt}x
shadowLastChange: 0
shadowMax: 0
shadowWarning: 0

Add a LDAP User using ldapadd

Now, use ldapadd command and the above ldif file to create a new user called adam in our OpenLDAP directory as shown below:
# ldapadd -x -W -D "cn=ramesh,dc=tgs,dc=com" -f adam.ldif
Enter LDAP Password:
adding new entry "uid=adam,ou=users,dc=tgs,dc=com"

Assign Password to LDAP User

To set the password for the LDAP user we just created above, use ldappasswd command as shown in the below example:
# ldappasswd -s welcome123 -W -D "cn=ramesh,dc=tgs,dc=com" -x "uid=adam,ou=users,dc=tgs,dc=com"
Enter LDAP Password:
In the above command:

  • -s specify the password for the username entry
  • -x The username entry for which the password is changed
  • -D specify your DN here. i.e Distinguished name to authenticate in the server

Create LDIF file for New Group

Similar to adding user, you’ll also need a ldif file to add a group.
To add a new group to the LDAP groups OU, you need to create a LDIF with the group information as shown in the example ldif file below.
# cat group1.ldif
dn: cn=dbagrp,ou=groups,dc=tgs,dc=com
objectClass: top
objectClass: posixGroup
gidNumber: 678

Add a LDAP Group using ldapadd

Just like adding user, use ldapadd command to add the group from the group1.ldif file that we created above.
# ldapadd -x -W -D "cn=ramesh,dc=tgs,dc=com" -f group1.ldif
Enter LDAP Password:
adding new entry "cn=dbagrp,ou=groups,dc=tgs,dc=com"

Create LDIF file for an existing Group

To add an existing user to a group, we should still create an ldif file.
First, create an ldif file. In this example, I am adding the user adam to the dbagrp (group id: 678)
# cat file1.ldif
dn: cn=dbagrp,ou=groups,dc=tgs,dc=com
changetype: modify
add: memberuid
memberuid: adam

Add an User to an existing Group using ldapmodify

To add an user to an existing group, we’ll be using ldapmodify. This example will use the above LDIF file to add user adam to dbagrp.
# ldapmodify -x -W -D "cn=ramesh,dc=tgs,dc=com" -f file1.ldif
Enter LDAP Password:
modifying entry "cn=dbagrp,ou=groups,dc=tgs,dc=com"

Verify LDAP Entries

Once you’ve added an user or group, you can use ldapsearch to verify it.
Here is a simple example to verify if the users exists in the LDAP database:
# ldapsearch -x -W -D "cn=ramesh,dc=tgs,dc=com" -b "uid=adam,ou=users,dc=tgs,dc=com" "(objectclass=*)"
Enter LDAP Password:
# extended LDIF
#
# LDAPv3
# base <uid=adam,ou=users,dc=tgs,dc=com> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#

# adam, users, tgs.com
dn: uid=adam,ou=users,dc=tgs,dc=com
objectClass: top
objectClass: account
objectClass: posixAccount
objectClass: shadowAccount
cn: adam
uid: adam
uidNumber: 16859
gidNumber: 100
homeDirectory: /home/adam
loginShell: /bin/bash
gecos: adam
shadowLastChange: 0
shadowMax: 0
shadowWarning: 0
userPassword:: e1NTSEF9b0lPd3AzYTBmT2xQcHBPNDcrK0VHRndEUjdMV2hSZ2U=

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Delete an Entry from LDAP using ldapdelete

If you’ve made a mistake while adding an user or group, you can remove the entry using ldapdelete.
To delete an entry, you don’t need to create an ldif file. The following will delete user “adam” that we created earlier.
# ldapdelete -W -D "cn=ramesh,dc=tgs,dc=com" "uid=adam,ou=users,dc=tgs,dc=com"
Enter LDAP Password:

Set up OpenLDAP server on CentOS 6.5

Set up OpenLDAP on CentOS 6.5

Hi readers, hope you are learning Linux and gaining more knowledge every day. In this tutorial I am going to show you how to set up an OpenLDAP server on CentOS 6.5 server. Follow along to get a better understanding.

This is going to be a two part tutorial/article as shown below
  • Set up OpenLDAP server on CentOS 6.5
  • Enable LDAP authentication on client machines
    • LDAP Authentication on Ubuntu 14.04
    • LDAP Authentication on CentOS 6.5

What my set up will look like ?

I have an Ubuntu machine which will be my host and has KVM set up in it. All demonstrations will be done in the KVM virtual machines. I have three virtual machines.
  • Cent1 – CentOS 6.5 minimal server (This will be the ldap server)
  • Cent2 – CentOS 6.5 minimal server (users will be authenticated using ldap server cent1)
  • Ubunt1 – Ubuntu 14.04 minimal server (Users will be authenticated using ldap server cent1)

Tasks we will be doing

  • Install software packages for OpenLDAP
  • Configure LDAP & logging
  • Set up firewall rules
  • Do a first import of an ldif file
  • Verify our first import
  • Create ldap users
  • Query ldap for users we added in the previous step

Install OpenLDAP

The basic installation requires the following three packages
  1. openldap – installed by default on CentOS 6.5
  2. openldap-servers – provides slapd ldap server
  3. openldap-clients – provides client utilities like ldapadd, ldapsearch, ldapmodify
Lets install all the required packages using the below command.
# yum install openldap openldap-servers openldap-clients
Verify the installation using the below command
# rpm -qa | grep openldap
openldap-servers-2.4.23-34.el6_5.1.x86_64
openldap-clients-2.4.23-34.el6_5.1.x86_64
openldap-2.4.23-34.el6_5.1.x86_64
Ldap service is called slapd in CentOS/RHEL. By default, the service will be stopped and disabled. Once we complete all our configurations we shall enable and start it.

Configure LDAP

Note that you won’t have any configuration named slapd.conf under /etc/openldap. All the configuration files are located under /etc/openldap/slapd.d

Change the default domain:

The first thing you need to do is change the default domain in few config files. The default domain will be my-domain. Change this to your domain. In my case I will be using junglegeek.com as my domain throughout this article.
# cd /etc/openldap/slapd.d/cn\=config
Find out which files you need to update using the below grep command.
# grep my-domain *
Usually it will be the following two files
  • olcDatabase={1}monitor.ldif
  • olcDatabase={2}bdb.ldif
Open these two files in vi editor and change all occurrences of my-domain to junglegeek. I opened them and changed it with the following vi command
:%s/my-domain/junglegeek/

Set up admin password:

Now lets generate the admin password hash using slappasswd utility provided by the openldap-servers package.
# slappasswd
New password: 
Re-enter new password:
{SSHA}PlOJU60HjF+WTt9/8L10fjPyTugQ79V
Copy the above password hash. We need to add it to couple of config files. Open up the following two files
  • olcDatabase={2}bdb.ldif
  • olcDatabase={0}config.ldif
Look for line that starts with olcRootDN. Add the following line next to this line in the above two files
olcRootPW: {SSHA}PlOJU60HjF+WTt9/8L10fjPyTugQ79V

Copy database config file

Copy DB_CONFIG.example from /usr/share/openldap-servers to /var/lib/ldap and set correct permissions as shown below
# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
# chown -R ldap.ldap /var/lib/ldap

Configure LDAP Logging

Now lets set up logging. Open /etc/rsyslog.conf
# vi /etc/rsyslog.conf
Add the below line
local4.*                         /var/log/slapd/slapd.log
Set up permissions appropriately
# mkdir /var/log/slapd
# chmod 755 /var/log/slapd
# chown ldap.ldap /var/log/slapd

Set up iptables

Ldap server uses the following ports. We need to add iptables rules to allow connections on port 389.
Port Description
389 LDAP
636 LDAPS
Add the below IPTable rule
# iptables -I INPUT -m tcp -p tcp --dport 389 -j ACCEPT

Restart services

All set and now start/restart all the services as below
# chkconfig slapd on
# service slapd start
# service rsyslog restart
# service iptables save
Verify slapd service using below commands
# netstat -nltp | grep slapd
# tail -f /var/log/slapd/slapd.log

Set up first import using ldapadd

Next thing we need to do is create a root entry for ldap. Create a file named firstimport.ldif
# vi firstimport.ldif
And enter the following content
dn: dc=junglegeek,dc=com
dc: junglegeek
o: junglegeek
objectclass: dcObject
objectclass: organization
Now add this entry to ldap using the below command
# ldapadd -x -D "cn=Manager,dc=junglegeek,dc=com" -W -f firstimport.ldif
Enter the admin password that you created earlier when asked. This command will add the root entry to the ldap server.
  • -x     : use simple authentication instead of SASL
  • -D    : binddn, the distinguished name to bind to the ldap directory
  • -W   : prompt for authentication instead of entering password in the command line
  • -f      : read information from the given file instead of the standard input

Verify import using ldapsearch

Now that we have added a single entry, we will verify it by querying the ldap server as below
[root@cent1 ~]# ldapsearch -x -b dc=junglegeek,dc=com
# extended LDIF
#
# LDAPv3
# base <dc=junglegeek,dc=com> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#

# junglegeek.com
dn: dc=junglegeek,dc=com
dc: junglegeek
o: junglegeek
objectClass: dcObject
objectClass: organization

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1
If you want to see the actual query output without comments and ldap version information use -LLL option as shown below.
[root@cent1 ~]# ldapsearch -x -LLL -b dc=junglegeek,dc=com 
dn: dc=junglegeek,dc=com 
dc: junglegeek 
o: junglegeek 
objectClass: dcObject 
objectClass: organization

Create LDAP Users

We have got a root entry dc=junglegeek,dc=com. Now lets add some OUs organizational units and some users.

Add organizational units OU:

Create a file named usersou.ldif and enter the below contents
dn: ou=users,dc=junglegeek,dc=com
ObjectClass: organizationalUnit
ou: users

dn: ou=groups,dc=junglegeek,dc=com
ObjectClass: organizationalUnit
ou: groups
Now add it to LDAP using the below command
[root@cent1 ~]# ldapadd -x -D "cn=Manager,dc=junglegeek,dc=com" -W -f usersou.ldif 
Enter LDAP Password: 
adding new entry "ou=users,dc=junglegeek,dc=com"
adding new entry "ou=groups,dc=junglegeek,dc=com"

Add a user to OU users:

We shall add a user to the newly created users organizational unit. Create a file named users.ldif and enter the below content.
# Section for User's primary group
dn: cn=venkatn,ou=groups,dc=junglegeek,dc=com
cn: venkatn
objectClass: top
objectClass: posixGroup
gidNumber: 5000

# Section for User
dn: uid=venkatn,ou=users,dc=junglegeek,dc=com
cn: Venkat Nagappan
givenName: Venkat
sn: Nagappan
uid: venkatn
uidNumber: 5000
gidNumber: 5000
homeDirectory: /home/venkatn
objectClass: top
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
userPassword: {CRYPT}*
Add it to LDAP using the below command
# ldapadd -x -D "cn=Manager,dc=junglegeek,dc=com" -W -f users.ldif
Set up password for the newly created user using the below command
# ldappasswd -x -D "cn=Manager,dc=junglegeek,dc=com" -W -S "uid=venkatn,ou=users,dc=junglegeek,dc=com"
We are done adding an user and his primary group.

Query LDAP for users

Lets query the user we created in the previous step. You can use any of the user fields to query ldap. Try the following search commands.
# ldapsearch -x -LLL "uid=venkatn" -b "ou=users,dc=junglegeek,dc=com"
# ldapsearch -x -LLL "cn=Venkat Nagappan" -b "ou=users,dc=junglegeek,dc=com"
That’s it for this tutorial. In my next tutorial I will show you how to set up/enable LDAP authentication on CentOS 6.5 & Ubuntu client machines so that we can use this ldap user venkatn for login.
Hope you enjoyed this article. Thanks for reading.

Enable LDAP Authentication in Ubuntu 14.04

Enable LDAP Authentication in Ubuntu 14.04

What I will be doing here ?

If you read my previous article, you will see that I had set up a CentOS 6.5 machine as an LDAP server and created a user named madesh whose primary group is venkatn with uid=5000 and gid=5000. The IP address of the CentOS LDAP server is 192.168.122.249. The CentOS machine is a KVM virtual machine hosted on an Ubuntu machine.

I have also got an Ubuntu server 14.04 virtual machine running under the same KVM host. It is a fresh install of Ubuntu server 14.04 and I will enable LDAP authentication. I will also use the LDAP user madesh for login.

Initial check before enabling LDAP authentication:

Just to make sure we are using only LDAP user and not the local user, I will check for local user venkatn using following commands which all will return nothing.
# id madesh
id: madesh: no such user
# groups madesh
groups: madesh: no such user
# getent passwd madesh
# getent group madesh
# grep madesh /etc/passwd /etc/group
# ls /home/madesh
ls: cannot access /home/madesh: No such file or directory
So there is no user/group named venkatn in the local system. Lets check once we enable ldap authentication.

Install packages

All we need is the following two packages.
  • libpam-ldap – Pluggable Authentication Module for LDAP. This package provides an interface between LDAP server and the PAM user authentication system
  • nscd - Name Service Cache daemon which handles passwd, group and host lookups and caches the result for next query
Install them using the below command
# apt-get install libpam-ldap nscd
When you install libpam-ldap it will ask you a series of questions. Each of them is show below for your reference. Make a note of the LDAP server IP address, base dn and ldap admin. In my case the details are as below.
LDAP Server IP 192.168.122.249
Base DN dc=junglegeek,dc=com
LDAP admin cn=Manager,dc=junglegeek,dc=com

Screen #1: Enter the LDAP server address

LDAP Authentication 1Enter you ldap server address in the below format
LDAP Authentication 2

Screen #2: Set the Base DN

LDAP Authentication 3

Screen #3: Select LDAP verion 3

LDAP Authentication 4

Screen #4: Select “Yes” for “Make local root database admin”

LDAP Authentication 5

Screen #5: Select “No” for “Does the LDAP database require login?”

LDAP Authentication 6

Screen #6: Enter LDAP admin id and password

LDAP Authentication 7
LDAP Authentication 8

Configure ldap authentication

We just need to make two more configuration changes before we can use LDAP users.

Step #1: Update /etc/nsswitch.conf

Edit /etc/nsswitch.conf file and make sure you add ldap as below
passwd:         ldap compat
group:          ldap compat
shadow:         ldap compat

Step #2: Update /etc/pam.d/common-session

Edit /etc/pam.d/common-session and add the following line to the end of the file.
session required         pam_mkhomedir.so  skel=/etc/skel  umask=0022

Verifying the LDAP user

Restart the name service cache daemon nscd before trying anything.
# service nscd restart
Now lets verify the LDAP user from the local system.
# id madesh
uid=5000(madesh) gid=5000(madesh) groups=5000(madesh)
# groups madesh
madesh: madesh
# getent passwd madesh
madesh:x:5000:5000:Madesh Soniya:/home/madesh:/bin/bash
# getent group madesh
madesh:*:5000:

Loggin in using LDAP user

It’s all working now. Lets try logging in using the ldap user
[root@ubunt1: ~] # ssh madesh@localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is b7:b0:d9:76:d2:11:24:da:02:50:40:92:31:c0:61:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
madesh@localhost's password: 

[madesh@ubunt1: ~]$ pwd
/home/madesh
We have successfully logged in to the local system using an LDAP user authentication.

Changing LDAP user password

Now lets try to change the LDAP user password when logged in as madesh user.
[madesh@ubunt1: ~] $ passwd
Enter login(LDAP) password: 
passwd: Authentication information cannot be recovered
passwd: password unchanged
[madesh@ubunt1: ~] $
As you see there is some problem updating the user password. Lets fix it.
Open the file /etc/pam.d/common-password
# vi /etc/pam.d/common-password
Look for the below line
password        [success=1 user_unknown=ignore default=die]     pam_ldap.so use_authtok try_first_pass
Remove the use_authtok parameter so that the line now looks like the below one.
password        [success=1 user_unknown=ignore default=die]     pam_ldap.so try_first_pass
Save it and try updating the password now.
[madesh@ubunt1: ~] $ passwd
Enter login(LDAP) password: 
New password: 
Re-enter new password: 
LDAP password information changed for madesh
passwd: password updated successfully
[madesh@ubunt1: ~] $
Success this time.
That’s it for this tutorial. Hope you enjoyed it and thanks for reading.