Linux useradd command

linux-useradd-linux-add-user-to-group-ipcisco

How to Add a User in Linux?

In Linux, different users have their own accounts. So, for a user, we need to create his/her account to give him/her access to Linux system. If you are a Linux administrator, you will do linux user add job too much. So, how can we ass a user in Linux? To add a new user in Linux, we will use linux useradd command with user’s name. Let ‘s add user arwen and learn how to add a user in Linux.

 

root@kali:/home/kali# useradd Arwen

 

After adding user Arwen, let’s check it in all user list. Here, we will use tail command with -5 option to show last five users.

 

root@kali:/home/kali# tail -5 /etc/passwd

king-phisher:x:133:142::/var/lib/king-phisher:/usr/sbin/nologin

kali:x:1000:1000:kali,,,:/home/kali:/bin/bash

systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin

gokhan:x:1001:1002::/home/gokhan:/bin/sh

arwen:x:1002:1009::/home/arwen:/bin/sh

 

As you can see above, arwen is added to the users.

 


You can check also Linux Cheat Sheet for more Linux commands.


 

Linux Delete User

To delete a user, we will use linux userdel command with the name of the user. Let’s delete user arwen with userdel command.

 

root@kali:/home/kali# userdel arwen

 

After this command, there is no user arwen on the system. We can check /etc/passwd file which stores all the users on the system.

 

root@kali:/home/kali# tail -5 /etc/passwd

lightdm:x:132:141:Light Display Manager:/var/lib/lightdm:/bin/false

king-phisher:x:133:142::/var/lib/king-phisher:/usr/sbin/nologin

kali:x:1000:1000:kali,,,:/home/kali:/bin/bash

systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin

gokhan:x:1001:1002::/home/gokhan:/bin/sh

 

You can also watch the video of this lesson below!


 

Adding New User With Group

We can also add a user with a group in Linux. By default, user is created with a group with the same name. To do this, we will use linux useradd command again but this time, we will use -G option with group name and username.

 

root@kali:/home/kali# useradd -G ipciscoLinux, ipciscoNetwork arwen

 

Again, to check our new user on the /etc/passwd file, we will use the tail command.

 

root@kali:/home/kali# tail -5 /etc/passwd

king-phisher:x:133:142::/var/lib/king-phisher:/usr/sbin/nologin

kali:x:1000:1000:kali,,,:/home/kali:/bin/bash

systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin

gokhan:x:1001:1002::/home/gokhan:/bin/sh

arwen:x:1002:1009::/home/arwen:/bin/sh

 

root@kali:/home/kali# groups arwen

arwen : arwen ipciscoLinux ipciscoNetwork

 

As you can see, our user has two groups. Primary one is the default group arwen and the secondary one is group ipciscoLinux.

linux-useradd-linux-add-user-to-group-ipcisco


 

Adding Password to Linux User

We can also add a password to Linux user. To do this we will use linux passwd command. After entering this command, you will type user’s password twice to set her password.

 

root@kali:/home/kali# passwd arwen

New password:

Retype new password:

passwd: password updated successfully

 

To use this password and enter with user arwen, we will use su command with username Arwen. Then we will enter the password that we have set.

 

$ su arwen

Password:

$

 

When we checked with whoami, as you can see I am logged in as arwen.

$ whoami

arwen

 


Listing Linux Users

There are various ways to list Linux users. One of them is using cat, less or tail command with /etc/passwd file. Because all the users stored in this file.

 

root@kali:/home/kali# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

bin:x:2:2:bin:/bin:/usr/sbin/nologin

sys:x:3:3:sys:/dev:/usr/sbin/nologin

sync:x:4:65534:sync:/bin:/bin/sync

…

 

Or we can list only last 5 users with tail -5/etc/passwd command.

 

root@kali:/home/kali# tail -5 /etc/passwd

king-phisher:x:133:142::/var/lib/king-phisher:/usr/sbin/nologin

kali:x:1000:1000:kali,,,:/home/kali:/bin/bash

systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin

gokhan:x:1001:1002::/home/gokhan:/bin/sh

arwen:x:1002:1009::/home/arwen:/bin/sh

 

Or, if you want to list only user names, you can use compgen -u command.

 

root@kali:/home/kali# compgen -u

root

daemon

bin

sys

sync

games

…

 

Again, we can use tail with compgen command to shorten out list. Belwo, we will list latest 5 users.

 

root@kali:/home/kali# compgen -u | tail -5

 


 

Summary

In this lesson, we have learned how to add a user in Linux basically with linux useradd command. We have also learned how to remove a user, how to add a user with a secondary group with one command. We saw setting user password in Linux and how to list users.

 

Linux user commands are very important and linux administrators use these commands too much on the job. So, to be more familiar for linux user adding, user deleting, you can practice by yourself.

 


Would You Like To Test Yourself With Linux Questions?


 

Lesson tags: useradd, linux user
Back to: Kali Linux Course

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Blog Posts