Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Managing Users and Groups

Save for later
  • 7 min read
  • 10 Nov 2016

article-image

In this article, we will cover the following recipes:

  • Creating user account
  • Creating user accounts in batch mode
  • Creating a group

Introduction

In this article by Uday Sawant, the author of the book Ubuntu Server Cookbook, you will see how to add new users to the Ubuntu server, update existing users. You will get to know the default setting for new users and how to change them.

(For more resources related to this topic, see here.)

Creating user account

While installing Ubuntu, we add a primary user account on the server; if you are using the cloud image, it comes preinstalled with the default user. This single user is enough to get all tasks done in Ubuntu. There are times when you need to create more restrictive user accounts. This recipe shows how to add a new user to the Ubuntu server.

Getting ready

You will need super user or root privileges to add a new user to the Ubuntu server.

How to do it…

Follow these steps to create the new user account:

  1. To add a new user in Ubuntu, enter following command in your shell:
    $ sudo adduser bob
  2. Enter your password to complete the command with sudo privileges:

    managing-users-and-groups-img-0

  3. Now enter a password for the new user:

    managing-users-and-groups-img-1

  4. Confirm the password for the new user:

    managing-users-and-groups-img-2

  5. Enter the full name and other information about new user; you can skip this part by pressing the Enter key.
  6. Enter Y to confirm that information is correct:

    managing-users-and-groups-img-3

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

    managing-users-and-groups-img-4

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 that if you skip the username part from the preceding command you will end up changing the password of root account.

There's more…

With adduser, you can do five different tasks:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at R$50/month. Cancel anytime
  • Add a normal user
  • Add a system user with system option
  • Add user group with the--group option and without the--system option
  • Add a system group when called with the --system option
  • 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 adduser, addgroup, 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

See also

Creating user accounts in batch mode

In this recipe, we 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:

  1. Create a new text file users.txt with the following command:
    $ touch users.txt
  2. Change file permissions with the following command:
    $ chmod 600 users.txt
  3. Open users.txt with GNU nano and add user accounts details:
    $ nano users.txt

    managing-users-and-groups-img-5

  4. Press Ctrl + O to save the changes.
  5. Press Ctrl + X to exit GNU nano.
  6. Enter $ sudo newusers users.txt to import all users listed in users.txt file.
  7. Check /etc/passwd to confirm that users are created:

    managing-users-and-groups-img-6

How it works…

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

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 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.

Creating a group

Group is a way to organize and administer user accounts in Linux. Groups are used to collectively assign rights and permissions to multiple user accounts.

Getting ready

You will need super user or root privileges to add a group to the Ubuntu server.

How to do it...

  1. Enter the following command to add a new group:
    $ sudo addgroup guest
  2. Enter your password to complete addgroup with root privileges.

How it works…

Here, we are simply adding a new group guest to the server. As addgroup needs root privileges, we need to use sudo along with the command. After creating a new group, addgroup displays the GID of the new group.

There's more…

Similar to adduser, you can use addgroup in different modes:

  • Add a normal group when used without any options
  • Add a system group with the--system option
  • Add an existing user to existing group when called with two non-option arguments
  • Check out groupadd, a low level utility to add new group to the server

See also

  • Check out groupadd, a low level utility to add new group to the server

Summary

In this article, we have discussed how to create user account, how to create a group and also about how to create user accounts in batch mode.

Resources for Article:


Further resources on this subject: