Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 4. User, Group and Permission

useradd (Add User)

useradd (Add User)

Adding Users in Linux

The useradd command is used to create a new user. Usually, the useradd command comes with the -m option, which can create the home directory for the new user under the path of /home with the user name. To run this command, you need to run it with superuser privileges.

Create a new user with its home directory

Adding a user is a privilege of the superuser. When you run the command, you need to run it as root or with the sudo command. If you run the command as a normal user without sudo, you'll see a permission error like the one below.

Command Line - INPUT
useradd user_a
Command Line - RESPONSE
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.

To create a new user, switch to the superuser and run the useradd command. Use the -m option to create the home directory for the new user.

Command Line - INPUT
sudo su -
useradd -m user_a

Check the user data

The user data is recorded under the /etc/passwd file. To check the file, run the following command.

Command Line - INPUT
cat /etc/passwd

You can see that a new user is added at the bottom of the file.

Command Line - RESPONSE
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
:
:
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
lxd:x:998:100::/var/snap/lxd/common/lxd:/bin/false
user_a:x:1001:1001::/home/user_a:/bin/sh

In this file, the following information is covered (left to right):

  1. user name
  2. user password (x) (actual encrypted user passwords are saved in a different file named /etc/shadow)
  3. user ID
  4. primary group ID
  5. user comment or full name (it's blank in the example above)
  6. user's home directory
  7. login shell path

You can also use the grep command to see only the necessary part of the user data. For example, what we need to know is the user data which has a string with "user" in it. Run the following command to get the data.

Command Line - INPUT
grep user /etc/passwd

You'll get the result like the one below.

Command Line - RESPONSE
user_a:x:1001:1001::/home/user_a:/bin/sh

Options

There are several options for the useradd command. You can use these options to customize user data.

  • -u: specify user ID
  • -g: specify primary group ID
  • -c: specify user’s comment
  • -s: specify login shell
  • -d: specify home directory path

Practice (1): Create new users

For practice purposes, let's create three users: user_a, user_b, and user_c. First, switch to the superuser.

Command Line - INPUT
sudo su

Run the useradd commands three times.

Command Line - INPUT
useradd -m user_a
useradd -m user_b
useradd -m user_c

Check the user data with the grep command.

Command Line - INPUT
grep user /etc/passwd

You'll see that three user accounts have been created.

Command Line - RESPONSE
user_a:x:1001:1001::/home/user_a:/bin/sh
user_b:x:1002:1002::/home/user_b:/bin/sh
user_c:x:1003:1003::/home/user_c:/bin/sh

Practice (2): Switch to a new user

You can further switch to another user by running the su command. To switch to user_a, run the command below.

Command Line - INPUT
su - user_a

You can see that # changed to $ as you are back to the normal user. Also, you can see that the home directory has changed to the user_a directory by running the pwd command.

Command Line - INPUT
pwd
Command Line - RESPONSE
/home/user_a

To go back to the ubuntu user, use the exit command twice as shown below.

Command Line - INPUT
exit
exit
Command Line - RESPONSE
exit
ubuntu:~$


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Django Templates vs. Django APIs for Web Development

Django Templates vs. Django APIs

Key Steps for Deploying a Django App

Key Steps of Django App Deployment

Managing Static Files in Django with {% static %}

Static Files in Development Environment – {% static %} tag

Understanding the Difference Between Django Projects and Apps

Project vs. App

Django Templates vs. Django APIs for Web Development

Django Templates vs. Django APIs

Key Steps for Deploying a Django App

Key Steps of Django App Deployment

Managing Static Files in Django with {% static %}

Static Files in Development Environment – {% static %} tag

Understanding the Difference Between Django Projects and Apps

Project vs. App

Tags:

Superuser

Normal User

Sudo

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server