Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

Lab5

Managing Users and Groups

T / Ebtisam ALselwi 1
An introduction

User: A person defined in the system is a single entity


configured with a name and a number
So that no users are similar in their definition.

Group: A group of users defined in the system is a


single-component identifier
Of a name and a number so that no two groups are similar to
each other in their definition.

Each user and group has a set of privileges

T / Ebtisam ALselwi 2
Elements for user identification:

Login : The login name is used to access the system and two
users may not participate in the same The name .
Passwd : The password is used to access the system.
UID(user identification ): is number associated with login the
name.
GID( group identification) : Is a number associated with the
name of the group to which the user belongs it .
Comment: User description or comment does not require
writing.
home directory path: Home directory path for user .
The shell : is executed automatically when the user enters the
system and is usually / bin / bash .

T / Ebtisam ALselwi 3
Files for users:

/ etc / passwd: This file contains information for each


user(Users' names and UIDs ).

/ etc / shadow : This file contains confidential information


about users(users password ) .

T / Ebtisam ALselwi 4
Elements to define the group:
Name:
The group name is used to enter the group and may not share two
groups In the same name.

Passwd:
The password is used to access the group. In most cases, passwd is not
assigned to the group.

GID:
The definition of the group is a number associated with the name of the
group and may not carry two groups same Number .

User list:
List of users subscribed to the group.

T / Ebtisam ALselwi 5
Files for groups:

/ etc / group:

This file contains information for each group(Group


names and GIDs ).

etc / gshadow:
This file contains confidential information about the
groups (Group password ).

T / Ebtisam ALselwi 6
User administration

Manage user accounts and groups which includes (Create new


account and Modify account already exists, delete account ).

Administrator Accounts

In Ubuntu, anyone in the sudo group can run any command (as long as
they use the sudo command when required).
To add an existing user to the "sudo" group (thus giving them admin
permissions) run:
ex:sudo adduser username

T / Ebtisam ALselwi 7
Managing Users and
Groups

Function
Command

id username display user-id and group id's

whoami Used to display the login

sudo su “Switch User”. Allows you to switch to another user's


account temporarily.
The default account to switch to is the root/superuser
account.

T / Ebtisam ALselwi 8
Managing Users and
Groups(con…)

Function
Command

cat /etc/passwd The /etc/passwd file is the system user database. There is
an entry in this file for
every user on the system—a line consisting of attributes like
the Linux user name,
The entries in /etc/passwd have the following format:
⟨user name⟩:⟨password⟩:⟨UID⟩:⟨GID⟩:⟨GECOS⟩:⟨home
directory⟩:⟨shell⟩

T / Ebtisam ALselwi 9
Managing Users and
Groups(con…)
Function
Command

cat /etc/shadow The /etc/shadow File For security, nearly all current Linux
distributions store encrypted user passwords in the
/etc/shadow file (―shadow passwords‖).
this file contains one line for each user, with the following
format:
⟨user name⟩:⟨password⟩:⟨change⟩:⟨min⟩:⟨max⟩_
_:⟨warn⟩:⟨grace⟩:⟨lock⟩:⟨reserved⟩

T / Ebtisam ALselwi 10
Managing Users and
Groups(con…)
Function
Command

cat /etc/passwd | grep Search in the system user database


username
cat /ect/group The /etc/group File By default, Linux keeps group
database information in the /etc/group file. This file
contains one-line entry for each group in the system,
which like the entries in /etc/passwd consists of fields
separated by colons (:). More precisely, /etc/group
contains four fields per line.
⟨group name⟩:⟨password⟩:⟨GID⟩:⟨members⟩

cat /etc/group grep Search in the system group database information in the
groupname /etc/group file.

T / Ebtisam ALselwi 11
Managing Users and
Groups(con…)
Function
Command

useradd username The procedure for creating a new user account

cat Displays information about users.


/etc/default/useradd

cat /etc/login.defs Displays information about setting.

passwd Chang the password for exiting user.

groupadd groupname create new groups

T / Ebtisam ALselwi 12
Managing Users and Groups(con…)
Function
Command

Usermod User accounts and group assignments are traditionally changed


by editing the
/etc/passwd and /etc/group files. However, many systems
contain commands like
usermod and groupmod for the same purpose, and you should
prefer these since they are safer and—mostly—more convenient
to use.
groupmod You can edit existing groups with groupmod without having to
write to /etc/group groupmod directly:
groupmod [-g ⟨GID⟩] [-n ⟨name⟩] ⟨group name⟩
usermod –l new old Change the user name

usermod –g group change a user’s primary group.


id username Usermod –g ⟨group⟩ ⟨user name⟩
The –g option allows you to specify a given group number.
T / Ebtisam ALselwi 13
Managing Users and
Groups(con…)

Function
Command

Usermod –u userid change a user’s id.


username
usermod –d /home/dir Change the name for a user’s directory.

T / Ebtisam ALselwi 14
Managing Users and
Groups(con…)
Function
Command

userdel –fr username To delete a user account, you need to remove the user’s
entries from /etc/passwd and /etc/shadow, delete all
references to that user in /etc/group, and remove the user’s
home directory as well as all other files created or owned by
that user. If the
user has, e. g., a mail box for incoming messages in
/var/mail, that should also be
removed.
groupdel There is also a tool to remove group entries.
groupdel ⟨group name⟩
exit Exit

T / Ebtisam ALselwi 15
Users Commands

T / Ebtisam ALselwi 16
id Command
display user-id and group id's
std@ubuntu:~$ id

uid=1000(std) gid=1000(std)
groups=1000(std),4(adm),24(cdrom),27(sudo),30(dip),46(pl
ugdev),109(lpadmin),125(sambashare)

std@ubuntu:~$ id ebtisam

uid=1001(ebtisam) gid=1001(ebtisam) groups=1001(ebtisam)

T / Ebtisam ALselwi 17
whoami Command
Used to display the login_name

std@ubuntu:~$ whoami

std

T / Ebtisam ALselwi 18
useradd Command
std@ubuntu:~$ sudo adduser ebtisam2
[sudo] password for std:
Adding user `ebtisam2' ...
Adding new group `ebtisam2' (1002) ...
Adding new user `ebtisam2' (1002) with group `ebtisam2' ...
Creating home directory `/home/ebtisam2' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for ebtisam2
Enter the new value, or press ENTER for the default
Full Name []: ebtisam2
Room Number []: 2
Work Phone []: 123
Home Phone []: 1245
Other []: 4
Is the information correct? [Y/n] y
T / Ebtisam ALselwi 19
std@ubuntu:~$ cat /etc/passwd
This file contains information
for each user(Users' names and
UIDs ).

.
.
.
std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash

T / Ebtisam ALselwi 20
cat /etc/passwd(con…)
Visual Shell
UID GID User’s
folder

ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash

User
passwd
name
This user runs services ,
he dos not long in system .

backup:x:34:34:backup:/var/backups:/usr/sbin/nologin

T / Ebtisam ALselwi 21
Add user with options

User Group
name name

std@ubuntu:~$ useradd u2 -g ug2 -u 1000 -d


/home/uh2 -p pass1

UID
Password

T / Ebtisam ALselwi 22
Add user with options

std@ubuntu:~$ useradd u2 -g ug2 -u 1000 -d /home/uh2 -p


pass1
useradd: group 'ug2' does not exist
std@ubuntu:~$ sudo groupadd ug2
std@ubuntu:~$ useradd u2 -g ug2 -u 1000 -d /home/uh2 -p
pass1
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
std@ubuntu:~$ sudo useradd u2 -g ug2 -u 1000 -d /home/uh2
-p pass1
useradd: UID 1000 is not unique
std@ubuntu:~$ sudo useradd u2 -g ug2 -u 1100 -d /home/uh2
-p pass1
std@ubuntu:~$
T / Ebtisam ALselwi 23
std@ubuntu:~$ sudo adduser --uid 1050 --home
/home/lab newuser2
Add user with options(con..)
[sudo] password for std:
Adding user `newuser2' ...
Adding new group `newuser2' (1050) ...
Adding new user `newuser2' (1050) with group `newuser2' ...
Creating home directory `/home/lab' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for newuser2
Enter the new value, or press ENTER for the default
Full Name []: newuser2
Room Number []: 123
Work Phone []: 1452
Home Phone []: 4521
Other []: 4521
Is the information correct? [Y/n] y
T / Ebtisam ALselwi 24
std@ubuntu:~$ cat /etc/passwd
This file contains information
for each user(Users' names and
UIDs ).

std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
u2:x:1080:1005::/home/uh2:
ebtisam5:x:1004:1007::/home/ebtisam4:
newuser2:x:1050:1050:newuser2,123,1452,4521,4521:/home/lab:/bin/bash

T / Ebtisam ALselwi 25
passwd Command Chang the password fo exiting user.

std@ubuntu:~$ sudo passwd ebtisam4

Enter new UNIX password:


Retype new UNIX password:
passwd: password updated successfully
std@ubuntu:~$

T / Ebtisam ALselwi 26
passwd command(con…)

std@ubuntu:~$ passwd ebtisam4


passwd: You may not view or modify password information for
ebtisam4.

std@ubuntu:~$ sudo su
[sudo] password for std:
root@ubuntu:/home/std# passwd ebtisam4
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ubuntu:/home/std# passwd ebtisam4
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ubuntu:/home/std#

T / Ebtisam ALselwi 27
“Switch User”. Allows you to switch to another
user's account

std@ubuntu:~$ sudo su ebtisam

ebtisam@ubuntu:/home/std$

T / Ebtisam ALselwi 28
Exit command Exit

ebtisam@ubuntu:/home/std$ exit

exit
std@ubuntu:~$

T / Ebtisam ALselwi 29
userdel Command To delete a user account

std@ubuntu:~$ sudo userdel ebtisam2

std@ubuntu:~$ sudo userdel ebtisam2


std@ubuntu:~$

std@ubuntu:~$ sudo userdel –fr ebtisam2

To delete a user account


with files

T / Ebtisam ALselwi 30
“Switch User”. Allows
you to switch account to
root/superuser account
std@ubuntu:~$ sudo su

root@ubuntu:/home/std#

T / Ebtisam ALselwi 31
Id & whoami in root account

root@ubuntu:/home/std# id
uid=0(root) gid=0(root) groups=0(root)

root@ubuntu:/home/std# whoami
root

T / Ebtisam ALselwi 32
Id username in root account

root@ubuntu:/home/std# id std

uid=1000(std) gid=1000(std)
groups=1000(std),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmi
n),125(sambashare)
root@ubuntu:/home/std#

T / Ebtisam ALselwi 33
cat /etc/passwd
root@ubuntu:/home/std# cat /etc/passwd
This file contains information for
each user(Users' names and UIDs ).
Use this file in root a account

.
.
.\std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
ebtisam4:x:1004:1007::/home/ebtisam4:

ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash

list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin

T / Ebtisam ALselwi 34
root@ubuntu:/home/std# cat /etc/shadow
root:!:17952:0:99999:7:::
This file contains
daemon:*:16365:0:99999:7:::
confidential
bin:*:16365:0:99999:7:::
information about
sys:*:16365:0:99999:7:::
users(users password
sync:*:16365:0:99999:7:::
).
games:*:16365:0:99999:7:::
man:*:16365:0:99999:7:::
lp:*:16365:0:99999:7:::
mail:*:16365:0:99999:7:::
news:*:16365:0:99999:7:::
uucp:*:16365:0:99999:7:::
proxy:*:16365:0:99999:7:::
www-data:*:16365:0:99999:7:::
backup:*:16365:0:99999:7:::
list:*:16365:0:99999:7:::
irc:*:16365:0:99999:7:::
gnats:*:16365:0:99999:7:::
nobody:*:16365:0:99999:7:::
syslog:*:16365:0:99999:7:::
T / Ebtisam ALselwi 35
cat /etc/passwd|grep command

root@ubuntu:/home/std# cat /etc/passwd|grep


ebtisam Search in the system user database

std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
ebtisam4:x:1004:1007::/home/ebtisam4:
root@ubuntu:/home/std#

T / Ebtisam ALselwi 36
cat /etc/passwd|grep command(con..)

std@ubuntu:~$ cat /etc/passwd|grep ebtisam

std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
ebtisam4:x:1004:1007::/home/ebtisam4:
root@ubuntu:/home/std#

T / Ebtisam ALselwi 37
cat /etc/passwd|grep command(con...)

std@ubuntu:~$ cat /etc/passwd|grep ebtisam2

ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/
bin/bash

T / Ebtisam ALselwi 38
Groups Commands

T / Ebtisam ALselwi 39
Groupadd Command Create a new group:

std@ubuntu:~$ sudo groupadd eng

std@ubuntu:~$ sudo groupadd eng


std@ubuntu:~$

T / Ebtisam ALselwi 40
cat /etc/group command This file contains
information for each
group(Group
std@ubuntu:~$ cat /etc/group names and GIDs ).

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
.
.
std:x:1000:
sambashare:x:125:std
ebtisam:x:1001:
ebtisam2:x:1002:
eng:x:1003:
eng1:x:1004:ebtisam2
eng2:x:1005:ebtisam3
ebtisam3:x:1006:
eng3:x:1007:
T / Ebtisam ALselwi 41
cat /etc/gshadow command

root@ubuntu:/home/std# cat /etc/gshadow


root:x:0:
This file contains
daemon:x:1:
confidential
bin:x:2: information about
sys:x:3: the groups (Group
. password ).
.
std:x:1000:
sambashare:x:125:std
ebtisam:x:1001:
ebtisam2:x:1002:
eng:x:1003:
eng1:x:1004:ebtisam2
eng2:x:1005:ebtisam3
ebtisam3:x:1006:
eng3:x:1007:
T / Ebtisam ALselwi 42
cat /etc/group|grep command

std@ubuntu:~$ cat /etc/group | grep ebtisam


Search in the system group database

ebtisam:x:1001:
ebtisam2:x:1002:
eng1:x:1004:ebtisam2
eng2:x:1005:ebtisam3
ebtisam3:x:1006:

T / Ebtisam ALselwi 43
cat /etc/group|grep command

std@ubuntu:~$ cat /etc/group | grep ebtisam2

ebtisam2:x:1002:
eng1:x:1004:ebtisam2

T / Ebtisam ALselwi 44
Change a group’s name:
groupmod -n newgroupname oldgroupname

std@ubuntu:~$ sudo groupmod -n user1 user


std@ubuntu:~$ cat /etc/group
.
.
eng2:x:1005:ebtisam3
ebtisam3:x:1006:
eng3:x:1007:
ug2:x:1008:
ug:x:1009:
ug3:x:1010:
newuser2:x:1050:
tech:x:1051:
uh2:x:1052:
user1:x:1011:

T / Ebtisam ALselwi 45
Delete a group: sudo delgroup groupname

std@ubuntu:~$ sudo delgroup tech

Removing group `tech' ...

T / Ebtisam ALselwi 46
Add and Change the group password: gpasswd grprname

td@ubuntu:~$ sudo gpasswd user1

Changing the password for group user1


New Password:
Re-enter new password:
std@ubuntu:~$

T / Ebtisam ALselwi 47
Add a user to a new group: Usermod –G groupname username

std@ubuntu:~$ sudo usermod -G user1 newuser

std@ubuntu:~$ cat /etc/group


.
.
ebtisam3:x:1006:
eng3:x:1007:
ug2:x:1008:
ug:x:1009:
ug3:x:1010:
newuser2:x:1050:
uh2:x:1052:
user1:x:1011:newuser
std@ubuntu:~$
T / Ebtisam ALselwi 48
Remove a user from a group: sudo deluser username _groupname

std@ubuntu:~$ sudo deluser newuser user1

Removing user `newuser' from group `user1' ...


Done.
std@ubuntu:~$

std@ubuntu:~$ cat /etc/group


.
.
eng3:x:1007:
ug2:x:1008:
ug:x:1009:
ug3:x:1010:
newuser2:x:1050:
uh2:x:1052:
user1:x:1011:

T / Ebtisam ALselwi 49
Modifying information of
user & group

T / Ebtisam ALselwi 50
Modifying a user:
std@ubuntu:~$ sudo su ebtisam4
ebtisam4@ubuntu:/home/std$ usermod
Usage: usermod [options] LOGIN

Options:
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the
new location (use only with -d)
T / Ebtisam ALselwi 51
Modifying a user(con…):
std@ubuntu:~$ sudo su ebtisam4
ebtisam4@ubuntu:/home/std$ usermod

-m, --move-home move contents of the home directory to the


new location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
-v, --add-subuids FIRST-LAST add range of subordinate uids
-V, --del-subuids FIRST-LAST remvoe range of subordinate uids
-w, --add-subgids FIRST-LAST add range of subordinate gids
-W, --del-subgids FIRST-LAST remvoe range of subordinate gids
-Z, --selinux-user SEUSER new SELinux user mapping for the user account

T / Ebtisam ALselwi 52
Modifying a username :

-std@ubuntu:~$ sudo su

root@ubuntu:/home/std# usermod -l ebtisam5

ebtisam4
root@ubuntu:/home/std#

Usermod newname oldname

T / Ebtisam ALselwi 53
-root@ubuntu:/home/std# cat /etc/passwd

ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
u2:x:1008:1008::/home/uh2:
ebtisam5:x:1004:1007::/home/ebtisam4:

T / Ebtisam ALselwi 54
Modifying uid ‘s user:

root@ubuntu:/home/std# usermod -u 1080 u2


root@ubuntu:/home/std# cat /etc/passwd

.
.
std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
u2:x:1080:1005::/home/uh2:
ebtisam5:x:1004:1007::/home/ebtisam4:

T / Ebtisam ALselwi 55
Modifying a Gid ‘s group :

root@ubuntu:/home/std# usermod -g 1005 u2


root@ubuntu:/home/std# cat /etc/group or passwd

.
.
std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam2:x:1002:1002:ebtisam2,2,123,1245,4:/home/ebtisam2:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
u2:x:1008:1005::/home/uh2:

T / Ebtisam ALselwi 56
cat /etc/default/useradd command

std@ubuntu:~$ cat /etc/default/useradd


GROUP=100
#
# The default home directory. Same as DHOME for adduser
# HOME=/home
#
# The number of days after a password expires until the account
# is permanently disabled
# INACTIVE=-1
#
# The default expire date
# EXPIRE=
#
# The SKEL variable specifies the directory containing "skeletal" user
# files; in other words, files such as a sample .profile that will be
# copied to the new user's home directory when it is created.
# SKEL=/etc/skel
#
# Defines whether the mail spool should be created while
# creating the account
# CREATE_MAIL_SPOOL=yes

T / Ebtisam ALselwi 57
cat /etc/login.defs

std@ubuntu:~$ cat /etc/login.defs


Displays information about setting

T / Ebtisam ALselwi 58
End…
T / Ebtisam ALselwi 59

You might also like