how to add a user linux

How to Add a User on Linux

By Olivia Allen
AD

Are you looking to add a new user to your Linux system? Creating a new user account in Linux is a straightforward process that can help enhance system security and simplify user management. Let's dive into the detailed step-by-step guide on how to add a user to Linux.

Before we get started, it's essential to understand that Linux is case-sensitive, so ensure you type your commands accurately. Also, you need to have sudo privileges or be logged in as the root user to add a new user. If you're not already logged in as the root user, you can switch to it using the command 'su'.

Adding a User through the Useradd Command

The useradd command is the traditional way of adding users in Linux. It's efficient and reliable, and it's available on many Linux distributions.

AD
How to Add a User in Linux
How to Add a User in Linux
Adding a User to a Group on Linux
Adding a User to a Group on Linux
How To Install Linux
How To Install Linux

Here's a basic syntax of the useradd command:

sudo useradd -m -s /bin/bash username

Linux Commands Cheat Sheet: Essential Commands Every Beginner Should Know
Linux Commands Cheat Sheet: Essential Commands Every Beginner Should Know
Linux Help Commands Guide (man, info, --help Explained) | Day 7
Linux Help Commands Guide (man, info, --help Explained) | Day 7
Basic Linux Commands Cheat Sheet for Beginners 🐧 | Linux Commands
Basic Linux Commands Cheat Sheet for Beginners 🐧 | Linux Commands

Let's break down this command:

  • -m : creates the home directory for the new user if it doesn't exist.
  • -s : specifies the user's default shell (/bin/bash in this case).
  • username : the new user's name.

Setting the User's Password

After adding the user, you need to set their password. You can do this using the passwd command:

AD

sudo passwd username

30 LINUX COMMANDS EVERY DEVELOPER SHOULD KNOW
30 LINUX COMMANDS EVERY DEVELOPER SHOULD KNOW
StudyWithSTEM is Visuals That Make Ideas Click.
StudyWithSTEM is Visuals That Make Ideas Click.
the linux file system has many different types of information on it, including text and images
the linux file system has many different types of information on it, including text and images
LINUX by categories
LINUX by categories
How to allow an existing Linux user to log in to Webmin (illustrated)
How to allow an existing Linux user to log in to Webmin (illustrated)
a black and yellow flyer with linux commands
a black and yellow flyer with linux commands

Then enter and confirm the new password when prompted.

Adding the User to the sudo Group

If you want this user to have sudo privileges (i.e., the ability to run commands with the permissions of another user, by default the superuser), you'll need to add them to the sudo group.

AD

You can do this using the usermod command:

How Linux Permissions Actually Work (rwx Explained in Minutes) 🔐🐧
How Linux Permissions Actually Work (rwx Explained in Minutes) 🔐🐧
Master Linux Special Permissions: SUID, SGID, and Sticky Bit
Master Linux Special Permissions: SUID, SGID, and Sticky Bit
Learn Linux from Beginner to Advanced (Part-1) | Essential Terminal Commands Cheat Sheet
Learn Linux from Beginner to Advanced (Part-1) | Essential Terminal Commands Cheat Sheet

sudo usermod -aG sudo username

The -a or --append option adds the user to the group without removing them from any other groups they may belong to.

AD

Adding a User through the Useradd Command with Additional Options

You can add more users with similar settings using the -D (useradd defaults determining) option. Here's an example:

sudo useradd -D -m -s /bin/bash

Enter the appropriate settings for the new users one at a time.

Setting the User's Gecos (User Information)

The gecos field is used to store user information, such as the user's full name. You can set this using the chfn command:

sudo chfn -f "User's Full Name" username

Adding a User using the Adduser Command

Many modern Linux distributions use the adduser command, which is a wrapper around useradd that automates some processes. Here's how to use it:

sudo adduser

Follow the prompts to create the user, set their password, and give them sudo privileges.

Remember to log out and back in for changes to take effect. The newer user can then start contributing to your Linux system!

AD