In this lecture, I'll be showing you how to create new user accounts. In Linux you need root privileges 1
to create, change or delete an account. To create a new account 2
you run the useradd command. Run man useradd and you’ll see lots of options. 3
To create a new user without specifying any options,just run useradd and the new username: 4
sudo useradd u1 5
Let's see the last five user accounts: tail -n 5 and the password file /etc/passwd 6
And the last user is u1, the user I've just created. The user was created using the default options 7
from /etc/default/useradd 8
By default, for the new user a group with the same name as the user name was also created. 9
Let's see the groups to which u1 belongs. 10
So groups u1 11
And it belongs to only one group called u1; this is its primary group. 12
Note that the default behavior of user add can differ from distribution to distribution; useradd also reads the 13
contents of the file /etc/login.def 14
this file. 15
This file contains options for the shadow password, such as password expiration policy, ranges of 16
user IDs used when creating system and regular users and more. To be able to login is the newly created 17
user 18
you need to set the user's password; to do that passwd 19
command followed by the username. 20
So sudo passwd 21
and the username u1 22
Now the user has a password and can login. 23
Now let's create the user by giving it the most common options manually. 24
If a user is created without any option, its home directory is not created as well. 25
There is no home directory for u1. 26
If you want to create the user's home directory, use the -m option. 27
So, sudo useradd -m; the directory name will be the same as the username and will be 28
created in /home. If you want to give it another name or create it in another location add the -d 29
option. 30
Like -d /home/james If you want to add a command to the user entry in /etc/ password 31
file, add the -c option and a command between quotes. 32
This command is typically a short description, like the user's full name or the contact information. 33
So -c in between quotes let's say C++ developer. 34
To create the user with a specific login shell at the -s option and the absolute path to that shell. 35
That shell will be automatically launched when the user logs in or opens a terminal. 36
I suppose you want the user to have the bash shell as the default. 37
So -s /bin/bash By default, the primary group of the user will be a group with the same name 38
as the user. 39
If you want to add this user to another primary group, use the -g option 40
and the name of the group. 41
However, this is not common. 42
What is common is to add the user to some secondary groups as well. To do that, use the capital G option 43
and the secondary groups with a comma between them let's say sudo,adm,mail 44
Note that the groups should already exist or you'll get an error. 45
And finally, the username James. 46
The user was created with those specified options. Don't forget to set a password for the user if you 47
want it to be able to login. 48
If you want to create a temporary user, then you should specify an expiration date, the date on which 49
the user account will be disabled. 50
Use the -e option and the date in the format year, hyphen month, hyphen day like this: 51
sudo usseradd 52
-c and the date: he year, hyphen, the month and the day, and finally the username u2. 53
If you want to change or list the user's password expire policy of an existing user run the 54
chage command and the username. 55
So chage command and the username. 56
To show the aging information of an account add the -l option like this: -l james 57
Of course, I have to run it as root. 58
And the passport expired policy of u2 59
And the account will expire at the end of the year. 60
If you take a look in /etc/passwd you’ll see that some accounts have as shell something like /usr /bin/ 61
nologin 62
or /bin/false 63
I'm sure it looks pretty strange, so I want to explain to you what it means. 64
Sometimes you need a system account to be used only to run a service such as a Web server. In Linux 65
every daemon or process should be run as a specific user 66
and it’s recommended not to be root. 67
This is an important security consideration. 68
To have such a limited user account, that never logs in, you set its shell as being false or nologin. 69
If no login is set as the user's shell 70
when the user tries to login, it will get a message saying this account is currently not available. 71
And false, is just a binary that immediately exists, returning false when called. That's why when someone 72
that has false as shell logs in is immediately logged out.There isn't actually such a big difference 73
between no login and false. 74
On some distributions, especially those based on Ubuntu, you'll see another command called adduser. 75
So what’s the difference between useradd and adduser? 76
useradd is a native binary compiled with any Linux system. But, adduser is just a perl script 77
which uses useradd binary as a back-end. 78
adduser is more user friendly and interactive than its back-end useradd. 79
There is no actual difference in features provided. 80
You’ve learned how to use the useradd command instead of adduser because it is recommended to know a general 81
way of creating users that work on any Linux distribution.