Adding Linux Users and Groups

adding-users-in-linux_featured-image

What we’ll cover in this article is adding and deleting users, as well as modifying exiting users. We’ll then focus on groups and how to add/delete them. I will also point out key files that are associated with this process for those of you that are new to Linux or are looking to pass some type of certification.

What is passwd/groups?

I know you didn’t ask, but before we get into the main course of this article I want to introduce two files that we will be using as examples. In the /etcdirectory, the passwd & the group files hold all of the users and group information. These files are essential when logging on to the system. Anytime you add a user, that user is added to the passwd file. Let’s take a look at/etc/passwd first.

When you add a user to the system, that user is placed into the passwd file.

Issue command: less /etc/passwd

Use the arrows keys to go up and down and “q” to exit.

Adding Users in Linux 1

Adding Users in Linux 2

Adding Users in Linux 3

You can edit the file directly or use the commands we will go over shortly. I recommend using the commands especially if you are a beginner. You do not want to corrupt the passwd file.

Let’s take a look at the group file:

Adding Users in Linux 4

Adding Users in Linux 5

The /etc/group file holds all of the group information as well as the users belonging to each group. The structure is very similar to that of/etc/password.

Adding Users in Linux 6

Again, these files are vital to the system and you will need to know them if you are taking any Linux exams.

Adding/Deleting Users

Adding a user is easy. The command used is: useradd “name of the user”

Note – You must be logged-in as root to add, delete, and modify users. It is not recommended to stay logged-in as root other than when necessary and only certain members should have root access.

Example:
useradd roman

Adding Users in Linux 7

You can then use “su” plus the name of the user you just added to logon. “exit” will take you out.

Adding Users in Linux 8

The command for deleting a user is “userdel”.
userdel roman

Adding Users in Linux 9

These commands are very basic, but there are other options we have as well. Options:

  • -d sets home directory for the user (if other than the default which is: /home/”user’s name”)
  • -m creates the home directory

Adding Users in Linux 10

Using the –d option on its own will only set the home directory for the user, but does not create it.

You can see I confirm this by “echo $HOME” which tells me my home directory and I use “ls” to confirm.

Adding Users in Linux 11

Adding the –m option will create the directory.

Adding Users in Linux 12

If you just add the user, default directory is /home/”users name” and you can just use the –m to create.

Lastly, using the “-r” option along with userdel will delete the user as well as the home directory.

Adding Users in Linux 13

Changing Passwords

If you are logged in as root, the command is “username” password.

Example: passwd roman

Adding Users in Linux 14

If you are logged on as the user, the command is “passwd”.

Adding Users in Linux 15

Adding Users to Groups

Let’s say we want to add roman to the group accounting. “-g” is used to change the user’s primary group.

Command is: useradd –gaccounting roman

I then ran the grep command to confirm.

Adding Users in Linux 16

However, say I want to add roman to the group accounting and make his primary group sales. We can add the “-G” option (other groups).

“-G” basically says add this user to a new group, but keep them in the old one (append).

Adding Users in Linux 17

Then issue command “id roman” – to confirm.

We can use “-G” on its own to add a user to another group.

Adding Users in Linux 18

Note: The groups must exit before we can add users to them.

Modifying Users

If a user is created and you just want to add that user to a group, or change the home directory, etc:

Example: usermod -Gmanagement roman

Adding Users in Linux 19

Or you can change the home directory for the user:

Example: usermod –d/home/newfolder roman

Adding Users in Linux 20

Creating Groups

The command for adding groups is “groupadd” or “groupdel”.

Adding Users in Linux 21

You can confirm by checking the /group/etc file.

Example: grep software /etc/group or cat /etc/group

Adding Users in Linux 22

The “groupdel” command will remove the group entirely.

There are a number of options you have when creating users and groups. Again, you could just go into /etc/passwd directly and add a user there, but unless you are familiar with file editors and putting a lock on, you should work with the commands. We will go over alternate methods in the Vi section.

Summary

  • Commands: useradd, userdel, usermod, groupadd, groupdel
  • Options
    -d change user’s home directory
    -m create directory
    -s used to change the default shell
    -r remove home directory when deleting user
  • “Passwd” will change the user’s password

How to Add a User and Grant Root Privileges on Ubuntu 14.04

Pre-Flight Check
  • These instructions are intended specifically for adding a user on Ubuntu 14.04 LTS.
  • I’ll be working from a Liquid Web Core Managed Ubuntu 14.04 LTS server, and I’ll be logged in as root.
Step 1: Add the User

It’s just one simple command to add a user. In this case, we’re adding a user called mynewuser :

adduser mynewuser

First you will be prompted to enter the user’s password (twice); do this step. Next you’ll be prompted to enter in the user’s information. This step is not required, and pressing enter fills the field with the default information:

Adding user `mynewuser’ …
Adding new group `mynewuser’ (1001) …
Adding new user `mynewuser’ (1001) with group `mynewuser’ …
Creating home directory `/home/mynewuser’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for mynewuser
Enter the new value, or press ENTER for the default
Full Name []: User
Room Number []:
Work Phone []:
Home Phone []:
Other []:

When prompted with the following question, enter Y then hit enter to continue.

Is the information correct? [Y/n] Y

Step 2: Grant Root Privileges to the User

visudo

Find the following code:

# User privilege specification
root ALL=(ALL:ALL) ALL

In this case, we’re granting root privileges to the user mynewuser . Add the following below that code:

mynewuser ALL=(ALL:ALL) ALL

Then exit and save the file with the key commands Ctrl-x, Y, enter.

If you’ve followed the instruction above correctly, then you should now have a user setup by the name ofmynewuser which can use sudo to run commands as root!

what is difference between /etc/shadow and /etc/passwd

passwd is the file where the user information (like username, user ID, group ID, location of home directory, login shell, …) is stored when a new user is created.

shadow is the file where important information (like an encrypted form of the password of a user, the day the password expires, whether or not the passwd has to be changed, the minimum and maximum time between password changes, …) is stored when a new user is created.

Advertisements

Posted on June 15, 2015, in Linux (Ubuntu/CentOS). Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: