Linux Administrator

Linux Command Example – useradd, userdel, usermod, groupadd, last and lastb

linux-command-example
mm
Written by Santosh Prasad

In this tutorial I am going to explain some basic Linux command ” useradd, userdel, usermod, groupadd, last and lastb” that help to Linux beginner users as well as advanced users.

1. useradd

useradd command used to create a new user or updates default new user information. Below are the some example:

Add new user called sagar with default settings

# useradd sagar

Create new user with home directory in /opt

# useradd sagar -d /opt/sagar

Disable user account, In below example account will be disable from 28/4/2017.

# useradd sagar-e 2017-04-28

Set Warning to change password after after password expired, system will allow the user to login for 7 days with warning.

# useradd sagar -f 7

Set user (sagar) group as admin

# useradd sagar -g admin

Create user with the given encrypted password. Without password account will disabled.

# useradd sagar -p $1$d7

Change user shell C as the default login shell for the sagar.

# useradd sagar -s /bin/csh

2. userdel

userdel command to delete a user account and user’s related files.

Delete the user account

# userdel sagar

Delete the user account together with their home directory and all files inside it.

# userdel -r sagar

Delete the user with home directory as well file, forcefully even user is still login,

# userdel -f sagar

3. usermod

usermod command is used to modify the user account.

Create new home directory for sagar in /var/sagar and move old home directory contents to this directory.

# usermod -d /var/sagar sagar

Disable user account, In below example account will be disable from 20/8/2018.

# usermod -e 2018-08-28

Set warning to change password after after password expired, system will allow the user to login for 7 days with warning.

# usermod -f 7 sagar

Set sagar’s initial group as infra

# usermod -g infra sagar

Set the new password for the sagar

# usermod -p $3$32d

Set BASH as default login shell for the sagar.

# usermod -s /bin/bash sagar

Lock the user’s password

# usermod -L sagar

Unlock the user’s password

# usermod -U sagar

4. groupadd

groupadd command is used to create new group.

Create a new group on the system.

# groupadd sales

5. last

last command is used to show list of last logged in users.

List all entries

# last

Show who were all logged through tty2

# last tty2

Show the all entries of Jim user

# last Jim

Display only last 7 entries

# last -7

Hide the Host Name field from output.

# last -R

Show hostname field in last column.

# last -a

Display the IP Address of Remote Host.

# last -i

List all entries up-to Aug 18 12:00:00 2016

# last -t 20160818120000

6. lastb

lastb command is used to display a list of recent bad login attempts ” from /var/log/btmp log file “.

The syntax for lastb is same as useradd but the login given is for an already existing user. Below are the some commonly used options.

  • -d : Change the user’s home directory.
  • -c : Update the comment for the user.
  • -g : Change the user primary group.
  • -a -G : Add the user to one or more supplemental group.
  • -G : Change the supplemental groups to only the list of groups and remove any other supplemental groups.
  • -s : Change the default shell for the user.
  • -u : Update the user’s UID number.
  • -L : Lock the account by disabling the password.
  • -U : Unlock the account and re-enabling its previous password.

I hope this article will help to learn some basic Linux command. If you have any queries and problem please comment in comment section.

Thanks:)

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

2 Comments

Leave a Comment