X
Tech

How to create users and groups in Linux from the command line

Jack Wallen walks you through creating new users and groups on Linux and adding those new users to the new groups.
Written by Jack Wallen, Contributing Writer

Linux is a multi-user environment, which means more than one user can use the system at one time. Granted, that mostly takes the form of console access (via SSH), because you can't easily have two people simultaneously logging into the desktop GUI (although multiple users can forward X11 GUI applications over SSH with ease). The point is, that a Linux system can include hundreds of users (if necessary). Although you wouldn't want this, a Linux server can handle 4,294,967,294 concurrent and distinct users.

Whether you're a serious admin or a casual user, chances are pretty good you will eventually need to add a new user and/or a group to a Linux system. 

Let's find out how.

Requirements

The process of creating users and groups from the command is the same, regardless of which Linux distribution you use, so it doesn't matter which distro you use. You will, however, need a user with sudo privileges because this is an administrative task.

With that said, let's get to the creation of users and groups.

Creating a new user

The first thing we're going to do is add a new user. I'll demonstrate by adding the user olivia but you can substitute whatever username you need for the user.

1. Log in to your desktop or server

To create this new user, log in to your desktop or server. If either has a GUI, you'll then need to open a terminal window from which you'll run the necessary command.

2. Create the new user

From the terminal window, create the user olivia with the command:

sudo adduser olivia

The above command will ask you a few questions:

New password (you'll type and verify a new password for the user)

  • Full Name
  • Room Number
  • Work Phone
  • Home Phone
  • Other

After answering the questions, you'll be asked to okay the information (type Y or N).

Of the above questions, the only one that is required is the password. The adduser command also automatically creates a home directory for the new user (unlike the useradd command which requires the -m option to create the home directory). 

Creating a new group

Next, we'll create a new group. Groups are a very convenient way to hand over permissions to a directory or file to several users at once (instead of having to do so one user at a time). For that, you create the group, add users, and then change the file or directory ownership to the new group. 

Creating a new group is even easier than creating a user. Let's create a group named "editorial" with the command:

sudo groupadd editorial

Boom!, group is ready for users.

Adding users to a group

We now have the new user olivia and the new group editorial. Let's add olivia to editorial, so she can enjoy all the benefits that come with membership. To add olivia to editorial we make use of the usermod command with the -a (append) and -G (groups) options like so:

sudo usermod -aG editorial olivia

User olivia can issue the groups command to see that she's been added to the editorial group.

Giving a folder group ownership

Let's say you've created the folder /DOCS and you want to change the ownership to editorial, so anyone in that group can work with the contents within. To change the ownership to the editorial group, the command would be:

sudo chown -R :editorial /DOCS

The -R option stands for recursive and means it will apply the ownership not just to the parent folder but to all child folders and files. The :editorial option means we're only changing group ownership and not the actual owner.

At this point, olivia would be able to view the contents of the folder, but cannot write to that folder. To give the editorial group write permissions, we use the chmod command like so:

sudo chmod -R g+w /DOCS

Now, any member of the editorial group will have write access to the /DOCS folder (and anything it contains).

And that, my dear Linux user friends, is how we add users and groups (and add users to groups) on the Linux operating system.

Editorial standards