Jump to: navigation, search

Linux Manage User and Group

From w3cyberlearnings

Contents

adduser vs. useradd

  • useradd command creates user without password and home directory.
  • adduser command creates user and will prompt you to provide each information about

the user such as home directory, password, user name, and even the telephone number.

create user: useradd <username>

root@ubuntu:~# useradd user200

set user password: passwd <username>

  • This command will ask you to enter a password.
root@ubuntu:~# passwd user200
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

create user and home directory: useradd -d <homedirectorypath> -m <username>

  • -d option to set home directory
  • -m option force useradd command to create home directory
root@ubuntu:~# useradd -d /home/user300 -m user300

change or set user password: passwd <username>

root@ubuntu:~# passwd user300

find out when the last password change:useradd -S <username>

root@ubuntu:~# passwd -S user200
user200 P 04/13/2012 0 99999 7 -1

create user and set user expired date:useradd -e <date> <username>

root@ubuntu:~# useradd -e 2012-12-12 user12

create user and set password expired:useradd -e <date> -f <days> <username>

root@ubuntu:~# useradd -e 2013-12-13 -f 18 user13

List Users

list all users and user home directory

root@ubuntu:~# cat /etc/passwd
kernoops:x:108:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:109:116:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:110:119:RealtimeKit,,,:/proc:/bin/false
hplip:x:111:7:HPLIP system user,,,:/var/run/hplip:/bin/false
saned:x:112:121::/home/saned:/bin/false
sophal:x:1000:1000:joyotama,,,:/home/sophal:/bin/bash
ftp:x:113:123:ftp daemon,,,:/srv/ftp:/bin/false
apache2:x:1001:1001:king,queen,318-548-8020,:/home/apache2:/bin/bash
mysql:x:114:124:MySQL Server,,,:/nonexistent:/bin/false
postgres:x:115:125:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
ntp:x:116:126::/home/ntp:/bin/false
chrome:x:1002:1002::/home/chrome:/bin/bash
kick2:x:1003:1003::/home/kick2:/bin/sh
user200:x:1004:1004::/home/user200:/bin/sh
user300:x:1005:1005::/home/user400:/bin/sh
...

List only user login name

root@ubuntu:~# awk -F':' '{ print $1}' /etc/passwd

Change or modify user information:chfn <username>

root@ubuntu:~# chfn user300
Changing the user information for user300
Enter the new value, or press ENTER for the default
	Full Name []: 

Group

create group:groupadd <groupname>

root@ubuntu:~# groupadd g1taxas

List all group

root@ubuntu:~# cat /etc/group

Search group or user in the group

root@ubuntu:~# grep sophal /etc/group
root@ubuntu:~# grep g1taxas /etc/group

Display sample result

adm:x:4:sophal
dialout:x:20:sophal
cdrom:x:24:sophal
plugdev:x:46:sophal
lpadmin:x:112:sophal
admin:x:120:sophal
sophal:x:1000:
sambashare:x:122:sophal

Add User to Group

Add or Change User to the primary group: usermod -g <groupname> <username>

root@ubuntu:~# usermod -g g1taxas user300

Add user to a supplementary/secondary groups

root@ubuntu:~# usermod -G g2taxas user300

Add User to multiple supplementary/secondary groups: usermod -G <suppgrop1>,<suppgrop2>,.. <username>

  • Group separated by a comma
root@ubuntu:~# usermod -G admin,ftp,www user300

Add User to a group: gpasswd -a <username> <groupname>

root@ubuntu:~# gpasswd -a sophal g2taxas

List User in the Group

List User information and included group:id <username>

root@ubuntu:~# id user300

Display result

uid=1005(user300) gid=1008(g1taxas) groups=1008(g1taxas),120(admin),123(ftp)

List User group name:groups <username>

root@ubuntu:~# groups user300

Display Result

user300 : g1taxas admin ftp g2taxas

Remove User from group

Remove User from the secondary group:gpasswd -d <username> <groupname>

  • -d: remove user
root@ubuntu:~# gpasswd -d user300 g2taxas
Removing user user300 from group g2taxas

Delete User

Delete User: userdel <username>

root@ubuntu:~# userdel user200

Delete user and home directory: userdel -r <username>

root@ubuntu:~# userdel -r user300

Create User and Assign Primary and Secondary Group

Create New User and Add to the primary and secondary group:useradd

  • -m: create home directory
  • -g: primary group
  • -G: secondary group
root@ubuntu:~# useradd -m -g g2taxas -G g1taxas,ftp,admin king200

List user group

root@ubuntu:~# groups king200
king200 : g2taxas admin ftp g1taxas
Navigation
Web
SQL
MISC
References