Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tech:linux:commands_to_add_users_and_groups [2019/12/07 06:27] (current)
Line 1: Line 1:
 +====== Linux Users and Groups Commands ======
 +===== Simple Example =====
 +<​code>​
 +useradd user007 -m -s /bin/bash
 +passwd user007
 +adduser user007 www-data
 +</​code>​
 +
 +===== Adding Group =====
 +Adding group someusername with a specific gid of 5001
 +<code bash>
 +groupadd -g 5001 someusername
 +</​code>​
 +
 +===== Adding User =====
 +Addding user someusername with a specific id, assign to gid 5001, etc
 +
 +<code bash>
 +useradd -d /​home/​someusername -g 5001 -m -s /bin/bash -u 5001 -p somepassword someusername
 +</​code>​
 +
 +===== Appending a group to an existing user as secondary group =====
 +Appending a group to an existing user as secondary group
 +
 +<​code>​
 +usermod -a -G <​group_name>​ <​user_name>​
 +</​code>​
 +
 +Or
 +
 +<​code>​
 +adduser <​user_name>​ <​group_name>​
 +</​code>​
 +
 +===== View a user id =====
 +<​code>​
 +id <​user_name>​
 +</​code>​
 +
 +===== Changing password =====
 +This was done as root
 +<​code>​
 +echo "​newpassword"​ | passwd --stdin <​userid>​
 +</​code>​
 +
 +
 +====== Sample ======
 +  * Create group and user vmail and add user to group
 +  * Create /home/vmail directory and make that the default home directory for user
 +
 +<code bash>
 +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
 +</​code>​
 +
 +  * Add user vmail to additional groups adm and sudo
 +
 +<​code>​
 +adduser vmail adm
 +adduser vmail sudo
 +</​code>​
 +
 +===== Adding User - Alternative =====
 +Adding group and user without specifying id.  Also sets password.
 +<code bash>
 +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
 +</​code>​
 +
 +===== Adding User - Ubuntu =====
 +Adding user johndoe - Run as root.  This also gives johndoe sudo priviledges!
 +<code bash>
 +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}
 +</​code>​
 +
 +Setting password
 +<code bash>
 +passwd ${NEWUSER}
 +</​code>​
 +
 +===== Locing 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.
 +
 +<​code>​
 +# Disable user01'​s password
 +passwd -l user01 ​
 +</​code>​
 +To unlock user01
 +<​code>​
 +# Enable password
 +passwd -u user01
 +</​code>​
 +
 +===== Passwords =====
 +Tested only on redhat
 +
 +<code bash>
 +# List password policy
 +chage -l snathan
 +# Change to no expiry
 +chage -m 0 -M 99999 snathan
 +# Change password
 +passwd snathan
 +</​code>​
  

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