Creating a user account on Ubuntu desktop or server

Suppose you have already installed the Ubuntu desktop or server. In that case, it means you have already created a default user on your system, even on the cloud also Ubuntu is installed with preconfigured default user account.

You can do all administrative tasks using this user, but sometimes you need to create another user for your team member or something else.

Here, In this ttorial, I’ll show you how to create a new user on Ubuntu Desktop or Server system.

Getting ready

You should have root or superuser privileges to create a new user on Ubuntu Server or Desktop.

How to do it…

Follow these steps to create the new user account:

To add a new user in Ubuntu, enter following command in your shell:

$ sudo adduser bob

Enter your password to complete the command with sudo privileges:

Sudo Password Linux Concept

Now enter a password for the new user:

adduser ubuntu Linux Concept

Confirm the password for the new user:

useradd confirm password Linux Concept

Enter the full name and other information about the new user; you can skip this part by using the Enter key.

Enter Y to confirm that information is correct:

ubuntu useradd confirmation LinuxConcept

This should have added new user to the system. You can confirm this by viewing the file /etc/passwd:

ubuntu etc password file LinuxConcept

How it works…

In Linux systems, the adduser command is higher level command to quickly add a new user to the system. Since adduser requires root privileges, we need to use sudo along with the command, adduser completes following operations:

Adds a new user.
Adds a new default group with the same name as the user.
Chooses UID (user ID) and GID (group ID) conforming to the Debian policy.
Creates a home directory with skeletal configuration (template) from /etc/skel.
Creates a password for the new user.
Runs the user script, if any.

If you want to skip the password prompt and finger information while adding the new user, use the following command:

$ sudo adduser --disabled-password --gecos "" username

Alternatively, you can use the useradd command as follows:

$ sudo useradd -s <SHELL> -m -d <HomeDir> -g <Group> UserName

Where:

-s specifies default login shell for the user
-d sets the home directory for the user
-m creates a home directory if one does not already exist
-g specifies the default group name for the user

Creating a user with the command useradd does not set password for the user account. You can set or change the user password with the following command:

$ sudo passwd bob

This will change the password for the user account bob.

Note:

Note that if you skip the username part from the above command you will end up changing the password of the root account.

With adduser, you can do five different tasks:

  1. Add a normal user
  2. Add a system user with system option
  3. Add user group with the --group option and without the --system option
  4. Add a system group when called with the --system option
  5. Add an existing user to existing group when called with two non-option arguments

Check out the manual page man adduser to get more details. You can also configure various default settings for the adduser command. A configuration file /etc/adduser.conf can be used to set the default values to be used by the adduseraddgroup, and deluser commands. A key value pair of configuration can set various default values, including the home directory location, directory structure skel to be used, default groups for new users, and so on. Check the manual page for more details on adduser.conf with following command:

$ man adduser.conf

Creating user accounts in batch mode

In this section, you will see how to create multiple user accounts in batch mode without using any external tool.

Getting ready

You will need a user account with root or root privileges.

How to do it…

Follow these steps to create a user account in batch mode:

Create a new text file users.txt with the following command:

$ touch users.txt

Change file permissions with the following command:

$ chmod 600 users.txt

Open users.txt with GNU nano and add user account details:

$ nano users.txt

Press Ctrl + O to save the changes.

Press Ctrl + X to exit GNU nano.

Enter $ sudo newusers users.txt to import all users listed in users.txt file.

Check /etc/passwd to confirm that users are created:

How it works…

We created a database of user details listed in same format as the passwd file. The default format for each row is as follows:

/etc/passwd

username:passwd:uid:gid:full name:home_dir:shell

Where:

username: This is the login name of the user. If a user exists, information for user will be changed; otherwise, a new user will be created.
password: This is the password of the user.
uid: This is the uid of the user. If empty, a new uid will be assigned to this user.
gid: This is the gid for the default group of user. If empty, a new group will be created with the same name as the username.
full name: This information will be copied to the gecos field.
home_dir: This defines the home directory of the user. If empty, a new home directory will be created with ownership set to new or existing user.
shell: This is the default login shell for the user.

The new user command reads each row and updates the user information if the user already exists, or it creates a new user. We made the users.txt file accessible to owner only. This is to protect this file, as it contains the user’s login name and password in unencrypted format.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

10 − seven =

Related Articles