The current operating systems stand out for being multi-user. That is, they can create many users with different usage profiles. This allows the same system to be used by different people. But also, it is possible to create specific system users for certain network services or applications. In any case, thanks to this post you will learn how to add users to Ubuntu.
In this tutorial, we will use the terminal, so you can use it if you have a server or use the system with a graphical interface.
The Linux useradd command
In Ubuntu, the command to create a new user is useradd
. This command is quite easy to use but has some options that allow you to customize the creation.
Open a terminal and run this command to get information about useradd:
useradd --help
And you will get a screen output like the one below:
Usage: useradd [options] LOGIN useradd -D useradd -D [options] Options: -b, --base-dir BASE_DIR base directory for the home directory of the new account -c, --comment COMMENT GECOS field of the new account -d, --home-dir HOME_DIR home directory of the new account -D, --defaults print or change default useradd configuration -e, --expiredate EXPIRE_DATE expiration date of the new account -f, --inactive INACTIVE password inactivity period of the new account -g, --gid GROUP name or ID of the primary group of the new account -G, --groups GROUPS list of supplementary groups of the new account -h, --help display this help message and exit -k, --skel SKEL_DIR use this alternative skeleton directory -K, --key KEY=VALUE override /etc/login.defs defaults -l, --no-log-init do not add the user to the lastlog and faillog databases -m, --create-home create the user's home directory -M, --no-create-home do not create the user's home directory -N, --no-user-group do not create a group with the same name as the user -o, --non-unique allow to create users with duplicate (non-unique) UID -p, --password PASSWORD encrypted password of the new account -r, --system create a system account -R, --root CHROOT_DIR directory to chroot into -s, --shell SHELL login shell of the new account -u, --uid UID user ID of the new account -U, --user-group create a group with the same name as the user -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping --extrausers Use the extra users database
From it, we can get the basic syntax of the command:
useradd [options] [username]
This command needs to be executed as root user. Or at least in addition to the sudo command.
We will now examine the most common options for this command.
Adding Users in Ubuntu with useradd
To add a user in Ubuntu, just execute the following command:
sudo useradd [username]
If you want to create a new user called imaginelinux
, it would be as follows:
sudo useradd imaginelinux
However, we will be able to make use of options to improve the creation.
As you know the user that the system creates by default, has assigned a home folder, to assign one, you must add the option -m
sudo useradd -m imaginelinux
This will create the new user and assign the /home/imaginelinux
directory as the home
Also, you can directly set what you want the Home address to be. In this case, the option to use is the -d
sudo useradd -d [directory] imaginelinux
For example:
sudo useradd -d /opt/imaginelinux imaginelinux
Sometimes it is convenient to create a user and not his personal folder. This is useful if we create a user dedicated to some service of the system. For this, the -M
option is the right one.
sudo useradd -M imaginelinux
Another very useful thing is to create the new user and once assigned to the main group of users and once added to secondary groups as sudo.
In this case, to assign it to a primary group we use the option -g
and for secondary groups -G
For example:
useradd -g users -G sudo,adm imaginelinux
What if it’s time to create an account that we know is temporary? Well, the useradd command has the -e option that defines a date on which the account will expire.
For example:
sudo useradd -e 2020-12-30 imaginelinux
This indicates that the account will expire on December 30, 2020. Fabulous, isn’t it?
If you have several shells in the system like zsh you can specify which one the new user will use. This is what the -s
option is for.
sudo useradd -s /usr/bin/zsh imaginelinux
Remember that all the options I have taught you can be combined into one command.
Now that the user is created, you have to assign him a password. You can do this with the passwd
command.
sudo passwd [username]
In our case:
sudo passwd imaginelinux
There you will have to define a password and confirm it. Now you know how to add and create users in Ubuntu.
Conclusion
Adding users to a system like Ubuntu can be a basic task but is always useful on purchased computers. However, this not only applies when Ubuntu is used on a desktop system but also at the server level.
For more information about the useradd command, you can check this link.
Also, we invite you to read our post about the rsync command.