In this lesson you'll learn how to disable delete and archive users on a Linux system.
Now I've already booted up our local user's virtual machine here and I've logged into it.
I'm still in vagrants home directory because I haven't changed into the shared directory of forward
slash vagrant just yet and will do that later in the lesson.
I just want to work in my home directory here for now.
So far we've been using the example of creating users as an excuse to learn all kinds of shell scripting
concepts and techniques.
The last few lessons I've actually been preparing to write a script around deleting users.
The main point isn't to have scripts that can create daily users.
It's to learn techniques that you can use in any shellscript.
Having said that you are going to write a script in the next assignment that will have you deleting
users so you need to know how to delete users.
So let's go ahead and cover that.
Now the command to delete a user from the system is user Del.
Now if you use our normal type command as a regular user you're probably going to get an error.
So if I do this type a user DL says user.
DLL not found.
And let's see what happens with the which command which user DL.
And it says there is no user DL and then it gives our path there which tells us that there's no user
DL and user local bad news or bad news or local Hispanic cetera.
It is our path and it's displaying there so just as the which command tells us the user DEL command
is not in our path.
We talked about paths earlier in the course.
At this point there really is two options.
One the user DEL command really doesn't exist it does not exist.
Or two it in fact does exist but it just exists outside of our current path.
So I quickly want to cover how to find a command that is not in your path because you may run into this
from time to time.
Now most Linux systems have locate installed and configured the locate command searches an index that
is created by the update divi command the update command is typically scheduled to run once a day.
This means that locate doesn't have up to the minute information but on the other hand it's a very fast
it finds things in that pre-created or pre-populated database or index very fast.
So if we do this when we type in the locate user Dell will get some results here and if we look at the
top of our command we can actually see that the command resides in Fort Sless user for Espen forward
slash user Dalle that's actually where the command lives.
Now just to prove that locate isn't using real time data.
Let's create a file called user Del and then try to find it with locate.
So I'm just going to touch user Dell right here in my home directory.
Again the touch command creates a file if it doesn't exists or if the file exists it touches it and
updates its time stamp.
So sure enough user DL is on our system and if we type locate user DL We don't see anything about vagrants
home directory.
So if we force an index update we should be able to find that user Idel file that we placed into vagrants
home directory by using the touch command.
And by the way to run update D-B you need root privileges because what it does is search is the entire
file system and you don't have permissions to every single file as a regular user such that vagrant
is there will do this sudo update DBI and let that run for a second here and once that is done now we
can do locate a user Dell and now at the very top of the output you see Ford slash home vagrant user
Dell that file we just created.
Let me take a step back so I would first use the locate command and actually since I'm looking for a
binary or an executable file.
I would limit my search to only include things in the bin directory.
Let's now take the standard output of the locate command and send it as standard input to the grip command
the grip command displays matches to a pattern and discard everything else.
So if we do locate a user Del pipe that into grap in our search pattern is simply going to be the characters
be in and hit enter.
So this really really narrows it down for us.
We see two possible options and obviously the one at the bottom there user ESPNews or Del is what we're
looking for.
Sometimes the file you're looking for is not in a place where you have read permissions.
The locate command honors those permissions.
So if we do something like this locate dot bash RC and hit enter.
We only see SC scaled up bash RC and then the bash RC in our home directory.
Now let's run that same command with root privileges.
Now we see all the dot bash RC files on the system as the vagrant user we don't have permission to look
inside root's home directory for example.
So if we do this LS Tesch l root bash R C we get an error.
Permission denied.
We can't look at anything in root's home directory by default.
So if we use root permissions we can now see the file.
By the way the quick little thing I did there the double exclamation marks or Bang Bang actually represents
the most recently executed command.
So if you want to execute the most recent command with root privileges.
Use the shortcut Sea-Doo space.
Bang bang.
The command that will be executed actually gets displayed to the screen first and then any output generated
by that command is displayed.
So here we can see that sudo bing bang expands to Sea-Doo space LS shell or slash root for Slash dash
bash RC and then the output of that command follows.
So that's just another quick shell tip for you right there.
Thone in the middle of this lesson.
Anyway my point here is that sometimes you need to use root privileges to find a file because you do
not have permissions to view that file as your own normal regular user.
Let's say locate isn't installed are configured so what would you try next.
Well I would use my knowledge of the Linux file system hierarchy and then start looking in places where
the file might live.
For example if I'm looking for a configuration file I would start looking and forward slash ETSI or
forward slash ATC.
But since we're looking for an executable also called a binary I would look in bin directory.
So let's do this here it looks like Ford slash bin is a symlink to user bin and S-band is actually a
symlink to user Esben and most modern Linux systems this will be the case but on some older Linux distros
or on some Unix systems you might find that they are actually different directories with different contents.
So let's go ahead and look in user bin and see if user data is there.
No no such file or directory.
And by the way when you see no such file a directory please believe it.
It's telling you that the file doesn't exist or the file that you specified does not exist.
Anyway let's try it.
End user Espen user.
Here we don't get an error.
We get some valid LS output that shows us that the file does exist.
Actually I probably could have shortened up my search just a little bit here because user Dell is a
system administration type of command Now system and then commands are usually found in s bin directories.
Normal commands that all users can run are found in bin directories for example LS is a command that
everyone needs.
Not just a system administrator.
So it's found in user bin.
There is another way to find files and that is where the find to command unlike locate find looks at
files in real time which makes it slower than locate but yet up to date there are a lot of options to
the find command and I'm not going to go through each and every one of them.
But here's just a very quick crash course and how to use the find command.
Now the format of the line command is find followed by the path to search in followed by any options
expressions or search patterns.
If you don't get find a path it starts searching in your current directory.
So let's look in the user bin directory Sophies find.
Follow that with a path and then hit enter.
Here it just lists all the files in that directory because we haven't narrowed down our search.
And by the way it not only list the files in that directory it would list any files of any subdirectories
and any files within those subdirectories and so on.
To say it another way find searches recursively.
Now let's tell it the name of the file We're looking for so we can do this find user Espin and there
is an option called Dasch name and then we supply that with the name of the file We're looking for.
We're looking for user Del OK admittedly that was too easy because we already knew where to find the
user command and I was teaching a little bit there.
But if you have no idea where the file exists you can search the entire file system starting at the
root.
I don't necessarily always recommend this approach especially if you're working on a system that has
a lot of files on it but it will work.
So let's do find slacks with a dash name of user Del here.
We're seeing a lot of permission denied errors.
There are two ways around this one is just to send all those error messages to dev null.
So we don't have to look at them and let's try that here now.
So we know that error messages are displayed on standard error and standard error is represented by
a file descriptor too.
So we'll use file descriptor to and redirect that to the bitbucket also called forward slash def afford
slash null and hit enter.
Now we don't get any of those air messages to our screen because they were redirected into dev know
which does nothing with them it just Those them away and we're left with all the matches to the user
Dell name without any of the errors.
So in this particular case we find the file that we're looking for.
But what if the file we were looking for is really somewhere where we need root permissions or a place
where our particular user does not have permissions to view in that particular case you want to run
the command as a route.
So we can do this.
So you do find slash and the name of use of Dell.
There is so much more to the find command and there's so much more you can do.
But this little crash course will really get you started and pointed in the right direction.
So at this point we've proven that user Dell exists even if the type in which commands do not provide
any information on those commands due to the settings of our path.
However user Dell will be and root's path.
So I'm going to switch to the root user.
You're on the vaguer system by the way the root password is also vagrant V.A g r E.A. and hit enter
now the root user and if I type type cache user or Del I'm going to see where the user del command resides.
All right I'm going to get out of the root account here and back to vagrant.