Linux Users and Groups Commands

Simple Example

useradd user007 -m -s /bin/bash
passwd user007
adduser user007 www-data

Adding Group

Adding group someusername with a specific gid of 5001

groupadd -g 5001 someusername

Adding User

Addding user someusername with a specific id, assign to gid 5001, etc

useradd -d /home/someusername -g 5001 -m -s /bin/bash -u 5001 -p somepassword someusername

Appending a group to an existing user as secondary group

Appending a group to an existing user as secondary group

usermod -a -G <group_name> <user_name>

Or

adduser <user_name> <group_name>

View a user id

id <user_name>

Changing password

This was done as root

echo "newpassword" | passwd --stdin <userid>

Sample

  • Create group and user vmail and add user to group
  • Create /home/vmail directory and make that the default home directory for user
groupadd -g 5000 vmail
useradd -d /home/vmail -g 5000 -m -s /bin/bash -u 5000 -p somepassword vmail
usermod -a -G vmail vmail
id vmail
  • Add user vmail to additional groups adm and sudo
adduser vmail adm
adduser vmail sudo

Adding User - Alternative

Adding group and user without specifying id. Also sets password.

UN=vipanicker
UN1="Vinod Panicker"
UN2="password"
groupadd $UN
useradd -d /home/$UN -g $UN -m -s /bin/bash -p somepassword $UN --comment "$UN1"
#useradd -d /home/$UN -g $UN -G wheel -m -s /bin/bash -p somepassword $UN --comment "$UN1"
echo "$UN2" | passwd --stdin $UN

Adding User - Ubuntu

Adding user johndoe - Run as root. This also gives johndoe sudo priviledges!

NEWUSER=vidman
groupadd ${NEWUSER}
useradd -d /home/${NEWUSER} -g ${NEWUSER} -G adm,cdrom,sudo,dip,plugdev,sambashare,lpadmin -m -s /bin/bash -p somepassword ${NEWUSER}

Setting password

passwd ${NEWUSER}

Locking User - Ubuntu

To disable or lock the password of an user account. This action prepends a ! to the password hash and the password will no longer match anymore.

# Disable user01's password
passwd -l user01 

To unlock user01

# Enable password
passwd -u user01

Passwords

Tested only on redhat

# List password policy
chage -l snathan
# Change to no expiry
chage -m 0 -M 99999 snathan
# Change password
passwd snathan

QR Code
QR Code tech:linux:commands_to_add_users_and_groups (generated for current page)