[SOLVED] Assigning UID's from specific range and in sequence RHEL 7 - PLEASE HELP!!!
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!)
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?
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
....
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.