LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-01-2020, 10:57 PM   #1
RedMan1001001
LQ Newbie
 
Registered: Oct 2020
Posts: 7

Rep: Reputation: Disabled
Assigning UID's from specific range and in sequence RHEL 7 - PLEASE HELP!!!


Hi All,

Sorry if this is the wrong section - I am a Linux Newbie and need some help

I am writing a bash script to allow users to create new accounts using the 'useradd' command. The problem i have is I need the UID assigned from a specific range and in sequence.

For example, user runs the script and creates an new account with UID of 5000. Then user runs the script again however this time the UID assigned should be 5001. If 5001 is not available then it should select 5002 automatically.

Does anyone know how i can achieve this?

This is what i have so far:

Code:
#!/bin/bash

uarray=($(seq 5000 5049))

n=0

read -p "username : " i

   if ! grep $i /etc/passwd >/dev/null; then
             useradd -u ${uarray[$n]} -g system_user -c "Regular User" $i
                echo $?
                if [[ $? -eq 4 ]]; then
                        while [[ $? -eq 4 ]]
                        do
                        UID=`expr "$n" + 1`
                        useradd -u ${uarray[$UID]} -g system_user -c "Regular User" $i
                        echo $?
                        done
               fi
   fi
The above code creates a user with UID of 5000 but then when i re-run the script it doesn't increment to the next UID available.

i am writing this is BASH and am running RHEL 7.9

I'm no scripting expert by any means and i've almost pulled out all my hair trying to make this work. Any help will be appreciated.
 
Old 10-02-2020, 06:29 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
The problem is probably that $? always contains the result of the most recent command. Therefore, in the if condition, it contains the result of the echo command, which is 0. Your program never enters the while loop.

Fix this by saving $? to a variable. That's all you need, I think.

A few suggestions for a prettier and easier to read script:

Rather than redirecting grep's output, use grep -q.
You don't need the if [[ $? -eq 4 ]]. Remove it entirely.
Arithmetic looks prettier like this: while (($?==4))
No need to use expr, rather: UID=$((n+1)) (look, ma, no $ sign!)
 
Old 10-02-2020, 06:54 AM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
A. That’s how useradd should work out of the box. You shouldn’t need to anything special in your script. Is it not working that way for you?
B. Have you looked at RedHat’s support documentation and/or contacted their support? You’re paying for it, yes?
 
1 members found this post helpful.
Old 10-02-2020, 08:57 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
To expand on berndbausch suggestions you can simplify your loops and get rid of the array.

Untested code...
Code:
for ((n=5000;n<5050;n++)); do
    useradd -u $n ...
    if [[ $? -eq 0 ]]; then
       echo "$i added successfully"
       exit
    fi
done
With most distributions now UIDs for regular users start at 1000, if the OP wants something different you have specify it as an option.

Last edited by michaelk; 10-02-2020 at 09:06 AM.
 
1 members found this post helpful.
Old 10-02-2020, 09:10 AM   #5
RedMan1001001
LQ Newbie
 
Registered: Oct 2020
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
The problem is probably that $? always contains the result of the most recent command. Therefore, in the if condition, it contains the result of the echo command, which is 0. Your program never enters the while loop.

Fix this by saving $? to a variable. That's all you need, I think.

A few suggestions for a prettier and easier to read script:

Rather than redirecting grep's output, use grep -q.
You don't need the if [[ $? -eq 4 ]]. Remove it entirely.
Arithmetic looks prettier like this: while (($?==4))
No need to use expr, rather: UID=$((n+1)) (look, ma, no $ sign!)

Thanks for the response!... I have updated the script with the suggestions you made - it looks like:

Code:
#!/bin/bash

uarray=($(seq 5000 5049))

n=0

read -p "type : " i

        if ! grep $i /etc/passwd >/dev/null; then

             useradd -u ${uarray[$n]} -g system_user -c "Regular User" $i

                        while (($?==4))

                        do

                        newuid=$((n+1))

                        useradd -u ${uarray[$newuid]} -g system_user -c "Regular User" $i

                        done
        fi
When i ran the above, it did select the next UID, so instead of 5000, it picked 5001 but then didnt move on to 5002 as 5001 was not available, it just kept looping between 5000 & 5001 and got stuck...

Code:
type : test4
useradd: UID 5000 is not unique
useradd: UID 5001 is not unique
useradd: UID 5001 is not unique
useradd: UID 5001 is not unique
useradd: UID 5001 is not unique
useradd: UID 5001 is not unique
useradd: UID 5001 is not unique
useradd: UID 5001 is not unique
....
What am i doing worng?
 
Old 10-02-2020, 09:17 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
See my post (#4) on simplifying your script a bit.

Why are you changing variable names?

Code:
n=$((n+1))
useradd -u ${uarray[$n]}...
 
1 members found this post helpful.
Old 10-02-2020, 09:18 AM   #7
RedMan1001001
LQ Newbie
 
Registered: Oct 2020
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
To expand on berndbausch suggestions you can simplify your loops and get rid of the array.

Untested code...
Code:
for ((n=5000;n<5050;n++)); do
    useradd -u $n ...
    if [[ $? -eq 0 ]]; then
       echo "$i added successfully"
       exit
    fi
done
With most distributions now UIDs for regular users start at 1000, if the OP wants something different you have specify it as an option.
Thank you for this.

It worked perfectly.

For anyone else searching like i was, here's what i used:

Code:
read -p "username : " i

for ((n=5000;n<5050;n++)); do
    useradd -u $n -g system_user -c "Regular User" $i
    if [[ $? -eq 0 ]]; then
       echo "$i added successfully"
       exit
    fi
done
The result was:

Code:
username : test6
useradd: UID 5000 is not unique
useradd: UID 5001 is not unique
useradd: UID 5002 is not unique
useradd: UID 5003 is not unique
test6 added successfully
A quick cat on the passwd file confirmed it all worked ok:

Code:
test6:x:5004:1002:Regular User:/home/test6:/bin/bash
Thank you all for the help and advice. It is much appreciated.
 
Old 10-02-2020, 09:25 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I left out your check to see if your username is unique and you might want to add something to indicate if all the UIDs in your sequence have been used.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assign UID from a specific range via bash script - RHEL7 r34per Linux - Newbie 1 09-03-2020 09:55 PM
assigning value with read command vs assigning value with = sign bubai70 Linux - Server 1 12-11-2017 10:47 PM
assigning NIC IP range with an exception of a specific ip address ashlyjay Linux - Newbie 2 09-03-2014 04:08 PM
Prevent programs from creating users within a specific UID range martindl Linux - Security 2 07-22-2011 09:07 PM
Changing the UID of a Physical Volume to a specific UID jambraun Linux - Newbie 2 02-09-2006 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration