Now when we were creating our user accounts,
it was a useradd command, add for adding.
If we want to modify a user account, what do you think it might be?
You've probably guessed it, usermod. Here,
we can go through and set the full name for our user and
appending to the secondary group wheel for user one.
When we're deleting user accounts, then it's going to be userdel.
But don't just take it as that, userdel will delete the
user, but won't delete the home directory, or associated
mail file, or cron tab file.
We can verify that by going through and searching where
those files would be, so below home and below var, where we
have nouser owner for our file.
So very useful tools that we've got with find. If we want to delete
those, and it's your choice, sometimes you might want to go through the
home directory or through the mail file to see if there's anything that
needs to be passed onto another user.
But if we don't need to do that,
we can use a userdel ‑r that will delete then the user account,
their associated home directory,
their associated mail spool file, and any cron tab file they might have.
We can verify that that is all being done by again going through looking
for where there is no user and we're going to be able to find then that
we don't have any leftovers from user2.
But as always,
the very best way to learn is to dive out through to our command line,
where we can take a look at modifying and deleting our users, so the
usermod command and the userdel command.
Let's find ourselves a command line.
So diving out and finding our glorious command line,
we're going to be able to pitter‑patter around these little
pixels and decide to start using our usermod command, so
usermod to modify an existing account.
We can set our full name or our comment, so using ‑c we can set the name
maybe to User One. We can go through and maybe if we want to go through
and set our group memberships, we can do so.
But our group memberships, we're really talking about our complimentary groups.
We might already belong to several groups, so we just
want to add one more group membership.
We can use then ‑a to append, and we use G to specify our secondary groups.
We can say then that we want to belong to the wheel group,
the administrative group that we have on Red Hat‑based systems. We
can use, then, ‑g to specify the primary group.
Remember, the primary group is specified against the user in the etc/passwd
file, and we can set it to that central shared group, users. Once we're
ready with everything that we want to modify, we can specify the user
account, and we're doing this then for the user, u1. We of course can go
through and verify that information,
especially the group information, by looking at then id
and u1. We can see the primary group,
the gid, is now group 100, the users group, but we additionally belong to those
secondary or complimentary groups, which includes, then, our primary group, but
in addition, the wheel group. But don't forget to use ‑a for append when we're
adding groups to a user's existing group memberships. To look inside of our
passwd file, we can use the getent command. Remember, this is going to search
all of our identity management systems, and we can see then that we've got more
information where we can see the full name being configured as User One.
So we've done a lot of work, but remember, if usermod was
modifying, userdel is deleting the account.
But if I just use userdel without specifying an option, just the argument, we
are deleting the user account, but not the associated files.
Also, because my primary group was modified, we're not
deleting the personal group, u1.
And of course, we're not going to go through and delete that central shared
group either, u1. We can prove that we haven't deleted the home directory
very easily, and that's what we want to show here when we list out with a
long listing of the home directory.
We also see the ownership, and we can see the ownership now is shown
as 1001 because there's no associated user account.
But to show what else has been left behind, the system files that we're
talking about are going to exist underneath /home and /var, and if we use
the option with find ‑nouser, it's looking where there is no user account
associated with the user ID, so all of the files within the home directory,
as well as the mail spool file.
But look how easy it is just adding in the extra option, ‑delete, that
would go and delete all of those files if that's what we want to do.
Clearing my screen and just going back up, if I do a userdel and then maybe
my user, u2, but add in the option, ‑r, ‑r is going to delete those
system‑related files, the home directory,
the mail spool file, and any cron tab the user might have.
It won't look at any central shared directory, looking at any files
they might own, that's not part of the normal system.
If I go back now having deleted a user, we can see that all of that
superfluous data has been removed along with the user account.
Now there is no right way or wrong way of doing it,
it's very much your business process.
If we delete the user's home directory, well there might
be files that need to be processed by somebody else, the
same way with the mail spool.
So maybe we do need to spend a little bit of time going through checking that,
but that's down to your business process and what you think the account might have associated with them.