1830168 Members
4888 Online
109999 Solutions
New Discussion

useradd script

 
SOLVED
Go to solution
Angela Swyers_1
Frequent Advisor

useradd script

Does anyone have an example of a useradd script that creates the new user/group with the next and same id#?
8 REPLIES 8
Sanjay_6
Honored Contributor

Re: useradd script

Hi,

If you don't specify the -u UID option in the useradd command, it will automatically assign the next UID after the currently assignet highest UID.

Hope this helps.

Regds
Sanjay_6
Honored Contributor

Re: useradd script

Same goes for the groupadd command.

Hope this helps.

Regds
Angela Swyers_1
Frequent Advisor

Re: useradd script

I want to keep my uids and gids the same. How would I do this?
Sundar_7
Honored Contributor

Re: useradd script

You want to keep your UID and GID the same ?- Why would you do that ?

Are you looking to create a seperate group entry for each user ? - doesnt sound like a good idea.

As noted above by Sanjay, if you dont mention the UID, useradd will automatically take the next available UID.

You can try something like this

useradd .....
grep "^username" /etc/passwd | awk -F: '{print $4}' | read ID
groupadd -g $ID

Learn What to do ,How to do and more importantly When to do ?
Angela Swyers_1
Frequent Advisor

Re: useradd script

Yes, I want a group for each user. We are trying to get LDAP authentication working with our Novell environment and this is how my network administrator is setting up the users in Novell, so I need to do the same in HP-UX.
Sundar_7
Honored Contributor
Solution

Re: useradd script

OK.. You can try something like this

#!/usr/bin/sh

echo "Enter Username: \c"
read USERNAME

useradd -d /home/$USERNAME -m $USERNAME

grep "^${USERNAME}" /etc/passwd | awk -F: '{print $4}' | read ID

echo "Enter Groupname: \c"
read GNAME
groupadd -g $ID $GNAME
Learn What to do ,How to do and more importantly When to do ?
Angela Swyers_1
Frequent Advisor

Re: useradd script

Thank you! That's exactly what I needed. I just needed to add a usermod at the end to change teh uid to match the new group that was created, and I needed to change the $4 to $3 so it created teh group with the same id as the user.
Angela Swyers_1
Frequent Advisor

Re: useradd script

Thanks for your help.