User account management this is the lesson we are going to Khowar how we could automate these user account
creation.
Now why do we need to create a user account and why do we have to have automation around it when we
all have to do is to run a user or add command right.
Well that's a very good question.
But what happens is sometimes you are working in environment where your system is also being accessed
by other users.
Let's say keyway or development.
And they also need to create user account for their own group but they don't have root privileges or
that's one reason that you need to automate the user account creation that you don't want them to run
use or at command.
And you know make a mistake or mess up your password file or even delete users accidentally so you don't
want to give them root access.
So what do you do is you created an automation tool which will allow them to only enter the users username
and some information and it will create that account for you.
You could actually create some new rights for them and that's how they could do it.
That's one of the reason that most people do and that's one of the reason I created that script in the
past as well where I was working environment there was was huge but they were all interlinked.
Where there was a cure department development department and a database group networking group all needed
access to the user accounts in Linux.
So we didn't want to give them full access.
We created this automation too.
Well another way is that even if you do not have that type of environment you're the only one you want
to create users.
You could create them.
But then again you could reduce use or add command you would have to remember again what was the option
chosen whether that user ID exists or not.
But anyway this is what this whole lesson is about.
What are we going to cover in this lesson is that.
First thing is we will create a script that will simply create a user account by just asking what is
the username and the user account is created and done.
Second script that I will create is it will error out if the X user already exists.
Let's say if you are creating an account let's say Jerry.
And if a Gery account already exists it will error out and it will tell you hate this account already
exist.
Second thing that I want to add as a third script is add a user description.
Now you could use user add minus see option to provide that description but most of the time people
forget to put that description or use them.
My A C option so we could add that to the script as well.
The Fortschritt then we will write we'll combine everything but also add the user id option meaning
if you want to create a user over some specific user id it will ask you to put that user id in and we'll
create it.
So what happens is the next system and you do a user add command followed by the user.
Auto generate a user ID based on their password and it will add 1 to the last created user ID and it
will give the user id to the account.
But sometimes there are applications that require a specific user account.
So for that purpose you have to specify user ID and then the last group that I will create with script
with you is adding everything that we discuss.
All four scripts but that people also error out if that user id exist it will do the check.
So this is a whole combined script.
Its a little longer script a little code that one script at a time.
So again I have my machine right here my Linux machine and it to save time save my time save your time
while your time is more svelte more valuable.
So I have created through five scripts already and if you notice I have five scripts create user 1 create
use a two three four and five I will go where each script.
One at a time.
But you have to write those scripts again two scripts are in the text document attached to this lesson.
You could download that notepad and you could view that text file and then simply copy paste or type
it.
I would prefer that you type it.
Most of the time and you copy paste something goes wrong with a script.
It doesnt work.
So type it any way.
So lets look at our first VI.
Create user 1
variable on the day description.
Descript will create a user account modified.
I already have that.
Again no waste of time.
Next thing is Echo.
Please provide a username.
This is simply asking your user to provide a user name and it's going to read the input and Echo just
line and then simply is going to run the command user or add the variable and variable v Have you and
the variable is the one that's provided by the user and then once that account is created is going to
say Echo user account has been created.
Simple right.
Straightforward Yes.
Let's run it.
Create user 1.
Please provide a user name.
Jerry after Jerry account has been created perfect.
My script ran awesome varified grew up Jerry from password file and you will see it created the Jerry
account with the user ID group id home directory and my shell.
That's the first script.
Second is I wanted to create a script that will ask the same thing but it will error out the user that
hey this user already exist so I can't create a user ID for that so far for that script.
I have created a second script called Let's clear it
the create user to enter.
Same thing again with the modified description.
Please provide user ID or user name read use or read you echo right here.
Grep command that's where it's adding that check it will check if that user exists or not how he's going
to check.
Right here first is going to run the command grep that minus queue option queue is a silent mode.
Don't show anything on the screen and grep the user which is that container which just use a specified
from ETSI password.
OK good.
Now I'm saying if the error code is equal to.
Sorry the exit status is equal to zero meaning if it does find it right.
So if my command is successful it will give me 0 right and the only way it could be successful if it
actually returns something.
So if it is 0 then echo error.
User this variable already exists.
Please choose another user name.
Echo exit 0.
Just exit out don't do anything.
See if that is not equal to zero.
If it's something else then it will add the user that will go through it.
If this goes through if this checks is work perfect then error out.
Otherwise you run this command use or add Aqua's user account has been created.
Let's run it.
Create user to please provide user name.
We created a user name earlier Jerry Wright created again and it says error user.
Jerry already exist.
Please choose another user name.
That's what we wanted to do.
If I run the same command again and this time I pick another user let's say Kramer has says Cremer account
has been created.
You could check that
and you'll see right here.
Cramer is user ID and from 1003 to the 1000 for it added one.
Number two.
OK so this is the second script we want to do is add use a description right here.
The third one use a description.
So the way you can add user descriptions by running the command user add and the user name whatever
and minus C is the option.
And then you put in the code
system admin guy or whatever and then you run it.
But most of the time people don't use that but we want to force the user to add their user description.
So I created a script the-I create use a three
handed How did a description same thing please provide user name.
It will do the check.
Same thing if the user exists.
It will come down right here.
Next one echo.
Please provide use a description read D as in variable and then write.
Here is user add the variable Q minus see option for description and the dida description.
The reason I put a coat around the variable is because you could actually add use a description with
spaces.
So if you have spaces then you have to have a code around
all right time to run it create.
Use a three.
Please provide user name this time let's pick Kramer.
I'm not going to worry because we're all really exist.
Run it anyone.
Please provide username.
So this time will pick George.
Please provide use description
George
a character and your George account has been created perfect this is what we wanted.
Now let's confirm
OKC is has a user name the updated user ID and the description right here user description is George
a character goes to home directory and abash.
This is our third script which is to add his description for script is to add a user ID.
Now what if you don't want the Linux to pick a user ID for you want to specify that user.
Perfect.
Clear the screen for that.
I have already created an script which is create user for Enter updated the description.
Now here same thing echoed.
Please provide user name.
Everything is the same to check the user account exist right here.
Please provide user description and now from here at this point on this is the new stuff that we have
added.
This is going to ask is use echo.
Do you want to specify a user ID because we also want to give the use of the option to say yes or no.
Or we could just simply force the user to say hey please and to user ID.
But what if the user doesn't want to add it.
They want Linux to pick itself.
So that's why we have given the user board option to specify yes or no.
So we're going to say Echo do you want to specify user ID.
Why slash and read.
And this is the variable i just pick why and you echo.
Now here are you saying if the variable equal Y Y pick equal equals sign because this is what works
what letters.
If I had digit or integer there I would pick dash E Q But since it's a letter I'm picking equal equal
Y.
So what I'm saying is if the user enters Why then akele please and to user ID right.
Perfect.
And then once the user ID is entered.
Put that in a variable named variable.
You Y.D. OK and then what to do and then simply use ID and has been specified then you run the command
user or add Redmain as you might see with the description and minus you is mine as you is the option
to add user ID and user ID is the one that's been specified by the user and this question perfect.
Echo.
Echo user account has been created perfect.
Now what if the user says no.
If you use a pick no I don't want it to pick a user ID.
Then what do you do then you come down here.
Then you say l f if the statement that you have to use as l.
If memory had if then else what else does is else.
If you say yes then do then do this else do whatever that you specify.
But now since we have to specifically say and as the letter and so that we have to pick l f l f u input
is right here no.
Which is and then what you do is echo.
No worries we will assign a user ID is just a friendly message I.
Then what do you do as user admin minus you minus C for the description and the description that's been
specified.
Right here in this variable and an echo echo user account has been created.
Fee done.
And this fee is coming from the US.
If you always have to and if the that's pretty much it guys for the script save it.
And now let's run it.
Create user account for.
Please provide use in name Kramer.
Kramer already exist.
Ok no problem.
Pick another one.
Please provide user name George.
Oh I created George already sorry.
OK but it's a good check right.
Please provide user name Elaine hit enter perfect please provide use a description
Elaine
Jerry friend perfect.
And now it's going to get into user ID.
Do you want to specify a user id this time I will say yes then is going to say now since you hit y now
please.
And to the user id say one thousand seven.
I hope it's not pect.
Alina account has been created perfect.
Yea that's confirm at sea.
Elaine
Yes but the description with a user ID.
Perfect.
Awesome.
OK.
Now everything is working as expected.
Last thing we want to do is what if I had picked a user ID that's been assigned to let's say
Jerry which is 1003 it would have been a problem right.
Yes.
But before we put that forth script let's do let's run that script again and this time will pick No.
To make sure no works as well.
Right.
You want to test both options create user.
Please provide user name.
So I picked already all four characters walking onto the next one.
How the heck what's the character of Seinfeld show.
This is really for me.
I'm picking those characters.
How bad.
David you guys know David Peddy probably if you watched Seinfeld.
That's a David puttee Elaine's boyfriend.
So you picked David enter.
Please provide a description.
They put Elayne boyfriend do you want to specify a user ID.
I would say no this time says no worries we will assign a user ID.
This is what we wanted to do right.
David the camera has been created.
OK lets see what idea has been assigned.
Is assigned a ok.
Hey why did it assign
anything after Jerry.
Or you know what let's do cat hash.
And you notice it has three 1003 1004 by six is not taken seven eight.
Why didn't assign six.
Well this is by default but Linux always picks the ID from the last ID's if his last ID.
Let's see if I change it to 10.
It will pick the next one is 11.
That's a counting mechanism.
It has.
So that's how it picked.
Anyway let's go back to our script let's talk about how it creates user accounts.
Now the last one and the last one is the longest one is that we wanted to do everything we just did.
It's clear the squeak but this time we want to do is last one error out.
User ID exist.
OK so we do everything but this time if the user ID is pect that is already in that c file I want it
to error out no problem.
Let's see how we could add that in there.
So I have created a script for it.
Create user 5 everything the same description is a little modified same thing.
Please provide use user name checking they use a name exist.
Next one is.
Please provide user description then echo.
Do you want to specify user ID.
This is what we did in the last clip or effect.
Everything is perfect now right here.
If you notice this is the part that I added.
So if you are creating this script I would recommend you to copy your CCRI create user for script and
paste it as a newer one and name it create file and then you can modify by coming directly into this
line where it says read user ID.
Then you add these lines here to what this line is good.
Again it's going to do the same thing grep que user ID from password member the same way we did.
If we go back up to check the user id right here it did the same thing right at grepped from the password.
So the same mechanism or same syntax of the scripts you will implement right here.
So Will do grep.
Q Which is silent.
User ID which is specified by the user right here from the password.
And if the status is the exit status is 0 then meaning if it finds it then echo error user id already
exist.
Echo empty line exit exit 0 meaning completely exit the entire script.
OK.
If it does not exist.
If it does not exist then run user add minus you option time not mine has the option of it minus you
variable minus C as a description.
DE is the description variable by NSU and is used right.
Okay.
And then Ecko user account has already been created.
This is the check right here.
If that check passed it will create that.
Now this fun.
Next coming down here is the same thing that we have if the user says no.
Then you simply come to this one and run it OK.
So let's run the script real quick.
And 5 and this time I'm going to pick
Stella
people I use his description
still
is mother and mother of George.
Anyway do you want to specify a user ID.
Yes.
And.
And to use radio 00 6 I know uses this.
So I use a 7 I know use Zuzu is 7 idea has been taken so I'm going to put it in just a test and it says
areas right here are taken right.
So I know the user id 6 is not take some were put that in this time.
Stella.
Stella is mother yes.
1006 and cell count has been created perfect group for Stella that's the password and you will see it
has been assigned user id 6.
And when you do cat at c password and you will see is at the bottom.
Next time you create an account it will automatically checks for ID and a the new one.
But if a new one is already taken it will give you.
It automatically goes to the next one anyway.
This is about this user account management.
I hope you learn a lot from this lesson.
It's a very useful script.
Try it out with different options and a different way that you want to add is user.