Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding users to linux box from LDAP

Tags:

bash

scripting

I received a request to add around 100 users to a linux box the users are already in ldap so I can't just use newusers and point it at a text file. Another admin is taking care of the ldap piece so all I have to do is create all the home directories and chown them to the correct user once he adds the users to the box. creating the directories isn't a problem, but I'd like a more elegant script for chowning them to the correct user. what I have currently basically looks like

chown -R testuser1 testgroup1 /home/tetsuser1; chown -R testuser2 testgroup2 /home/testgroup2; chown -R testsuser3 testgroup1 /home/testuser3

bascially I took the request that the user name and group name popped it into excel added a column of "chown -R" to the front, then added a column of "/", copied and pasted the username column after it and then added a column of ";" and dragged it down to the second to last row. Popped it into notepad ran some quick find and replaces and in less than a minute I have a completed request and a sad empty feeling. I know this was a really ghetto method and I'm trying to get away from using excel to avoid learning new scripting techniques so here's my real question.

tl;dr I made 100 home directories and chowned them to the correct users, but it was ugly. Actual question below.

You have a file named idlist that looks like this (only with say 1000 users and real usernames and groups)

testuser1 testgroup1
testuser2 testgroup2
testuser3 testgroup1

write a script that creates home directories for all the users and chowns the created directories to the correct user and group. To make the directories I used the following(feel free to flame/correct me on this as well. )

var= 'cut -f1 -d" " idlist' (I used backticks not apostrophes around the cut command)
mkdir $var

like image 501
Chris Hupman Avatar asked Mar 09 '26 10:03

Chris Hupman


1 Answers

#!/bin/bash
# should work in any POSIX shell
while read user group
do
    mkdir "/home/$user"
    chown "$user:$group" "/home/$user"
done < idlist
like image 132
Dennis Williamson Avatar answered Mar 11 '26 00:03

Dennis Williamson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!