Now, you know what our motto is?
We have to go through and try something to really understand it.
So we, emphasis on the term we, are now going to create some users.
We can create those users using some of the defaults.
We can look at where those defaults are stored and modifying defaults,
as well as then overwriting some of those defaults by
creating users using non‑default values as well.
Come on, let's find that command line.
And here we are at the glorious alma8 command line.
So let's go through and elevate our privileges,
just to save us having to run sudo in front of every command.
Now,
we're going to be taking a look at creating our users, and
would you believe, that's the command, useradd?
But let's begin by looking at the option ‑d. With the
option ‑d, we can see those defaults that are implemented
when using the useradd command.
So if we don't specify,
for example, the primary group we want our users to belong to, it's going
to be group id 100. Group id 100 is our users group, but we'll see how
there are other settings that can change that.
If we go ahead now and create a user with a minimum amount of
settings, just the username, obviously we need the username to be
able to be specified, but we've now created our user account based
on these defaults. But as I say,
some of the results might appear a little bit confusing. To begin with, let's
go through and use the id command where we can then go through and see some of
the group memberships that the user belongs to.
So we can see that we have user id 1001 and the user
account u1, but look at the primary group id, the gid,
that's 1001, and also the u1 group.
We don't belong to group id 100 as our primary group or the
users group, if we're to give it its name. That's because
in many Linux distributions,
including AlmaLinux, and other RedHat‑based distributions and
those based around Debian, choose to add the users not to the
users group, but use a private group scheme.
It differs if you're using this on openSUSE where your
users would belong to the group users.
When we look inside of our directory /home,
we can see that we have got the directory u1.
So on this system, the user's home directory was automatically created.
However,
that's an easy default to overwrite. Using the option ‑M, we are
specifying not to create the home directory.
We now look at the id command,
and we can see that's pretty much the same as we saw
before, but with a new private group,
u1a, but when we look inside of the home directory,
there's no home directory. Now,
this might be useful for when we go through and take a look at
creating accounts, maybe in a university where you're adding in a
complete cohort, and you want their home directories to be created
over a period of time, rather than all in one go when we're creating,
say, 1000 accounts in one go.
Now,
we can also go through and use the glorious command, getent, to search
our password databases rather than just a single file.
And if we go back and use the Esc and dot, we can
navigate back to previous arguments.
We can just use Esc and dot multiple times.
So we can see then that we've got a path through to our home
directory, /home/u1a, but it's just not created at present.
We can use modules to create this on user login.
Now let's go and remind ourselves that it was useradd ‑D that
printed the defaults, but these defaults are really coming from
the file, /etc/default/useradd.
So you see, it's exactly the same information that we are looking at.
We could go through and change these defaults either by editing the file,
which is often the easiest way to modify it.
Alternatively,
you could use the useradd ‑D with additional options,
specifying which element you want to change.
But most often, it's easy enough just to change the file.
But it's not just this file that is working on the
defaults. Now that would be too easy, wouldn't it?
Of course, this is Linux and there are lots of files
that are working towards this.
There's also a file called /etc/login.defs, and if we go through
and search that file with our good friend, grep,
I'm going to use the option ‑E to allow me to use
an enhanced regular expression.
I want to use a group. We're going to say lines that begin with, so that's
my caret, the group then is specified via the parentheses, so this is why I
have to use the uppercase E, and I'm going to look then for CREATE_HOME. I'm
going to use a vertical bar so I can specify the or option here, so either
starts with CREATE_HOME or USERGROUPS. With that in place, I can close off
my parentheses, closing off the group, close off my single quotes,
encapsulating the regular expression, and then go through, and
remember the file name I gave you, /etc/login.defs. And we can see
there we've got a couple of settings. So we can see on this system,
home directories are created automatically. USERGROUPS_ENABLED,
that's our private groups.
So we can see then that automatically, we are creating a
group based around the same name as our user,
but these are just defaults. We can, of course, go through and change them.
So if I look at editing the file, I can use / inside of my vi text editor,
to search for a configuration setting, and let's look for, say, _HOME, that
should find it easy enough, and then go through and changes to no. So now
we're not defaulting to creating the home directory. This will be more of a
Debian‑based way of doing things.
Now, as I go through and double‑check,
we can see that we are not automatically creating a home directory.
So if I go through and add in a new user now, we can go through and
maybe let's stay with our theme and have user u1b.
Now, when I go through and look in the home directory,
/home, there won't be a home directory.
We've changed some of the default. But just because I said
I didn't want to create it by default,
there's nothing stopping me going through and maybe running a useradd ‑m.
Lowercase m is specifying that we explicitly want our home directories
created, and often these are good options to put in because then
irrelevant of the distribution, we know we are going to be using the
settings that we specify. The uppercase G is specifying the secondary
group that I want to belong to, the complementary groups. I can set the
comment to be User Two, and then it might make sense calling my user u2.
But now, even though the default says not to create the home directory,
we have created it.
I'm able to go through and check the id to check the group memberships.
We've still got the private group; we didn't overwrite that, but of
course, I've gone through and created the home directory for u2, so we
will have that directory in place. And a simple listing of the directory
/home should be able to confirm that.
Look at that.
We've got a glorious directory there for u2.
So we can see then how we can create our user accounts, either
working with the default, but in some ways, I like the idea of
specifying the settings that we actually want from the command line.
Explicit is always better than implicit, I think.