1825721 Members
3134 Online
109687 Solutions
New Discussion

useradd command

 
SOLVED
Go to solution
matthew mills
Frequent Advisor

useradd command

I am trying to build a scrict that adds ftpusers. I am using the useradd command but I want to set the UID to use the next available over 1000. can this be done?
2 REPLIES 2
James Specht
Trusted Contributor
Solution

Re: useradd command

do the following in your script.

useradd -u $(expr $(cat /etc/passwd|cut -d ':' -f3|sort -n|tail -1) + 1) ....

This will find the largest number in your /etc/passwd file and add one to it, then pass that value on to useradd. I do the same thing here. Of course this only does a one up. It will not fill in any empty spaces that are less then the max uid.

--Jim

"Everyone can be taught to sculpt: Michelangelo would have had to be taught how not to. So it is with the great programmers."
Kent Ostby
Honored Contributor

Re: useradd command

Add this line to your script:
cut -d: -f3 passwd | sort -n | awk -f nextuid.awk


nextuid.awk should contain:
-------------------------------------
BEGIN {previd=1000;}
$1>1000 {
if ($1-previd > 1)
{print previd+1; exit;}
previd=$1;
}
----------------------------------------

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"