Practice Exercise: Managing User Accounts, Users, and Groups
Objective
Learn how to manage user accounts, create new users, and work with user groups in a Linux environment.
Task 1: Managing User Accounts
- Open a terminal window.
- Use the
whoami
command to display your current username. - Create a new user account named
newuser
using theuseradd
command. - New user account creation needs sudo privileges
- Set a password for the new user using the
passwd
command. - As a sudo user you can change another users password
- Switch to the
newuser
account using thesu
command. - Display the current user and verify that you are now the
newuser
. - Exit the
newuser
account using theexit
command.[intern@intern-a1t-inf-lnx1 ~]$ whoami intern [intern@intern-a1t-inf-lnx1 ~]$ sudo useradd newuser [sudo] password for intern: [intern@intern-a1t-inf-lnx1 ~]$ sudo passwd newuser New password: Retype new password: passwd: password updated successfully [intern@intern-a1t-inf-lnx1 ~]$ su newuser Password: [intern@intern-a1t-inf-lnx1 ~]$ whoami newuser [intern@intern-a1t-inf-lnx1 ~]$ exit [intern@intern-a1t-inf-lnx1 ~]$
Task 2: User Groups
- List all existing user groups on your system using the
cat /etc/group
command. - Create a new group named
developers
using thegroupadd
command. - Similar to useradd groupadd require sudo privileges as well
- Add your
newuser
user account to thedevelopers
group using theusermod
command. - Verify that your user is now part of the
developers
group. - Display the group memberships of both your user account and
newuser
.[intern@intern-a1t-inf-lnx1 ~]$ cat /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:intern,syslog [intern@intern-a1t-inf-lnx1 ~]$ sudo groupadd developers [intern@intern-a1t-inf-lnx1 ~]$ sudo usermod newuser -G developers [intern@intern-a1t-inf-lnx1 ~]$ groups newuser newuser : newuser developers [intern@intern-a1t-inf-lnx1 ~]$ groups newuser intern newuser : newuser developers intern : intern adm cdrom sudo dip plugdev lxd
Task 3: User Home Directories
- Navigate to the
/home
directory. - List the contents of the
/home
directory to view user home directories. - Create a new directory named
projects
in your home directory. - Set the ownership of the
projects
directory to your user and the groupdevelopers
. - Verify the ownership and permissions of the
projects
directory. - Create a text file named
project_plan.txt
inside theprojects
directory. - Check the ownership and permissions of the new text file.
- Switch to the
newuser
account using thesu
command. - Attempt to access the
projects
directory andproject_plan.txt
. Note any access restrictions.[intern@intern-a1t-inf-lnx1 ~]$ ls /home intern [intern@intern-a1t-inf-lnx1 ~]$ sudo mkdir /home/projects [sudo] password for intern: [intern@intern-a1t-inf-lnx1 ~]$ sudo chown intern:developers /home/projects [intern@intern-a1t-inf-lnx1 ~]$ ls -l /home total 8 drwxr-x--- 10 intern intern 4096 Sep 18 14:53 intern drwxr-xr-x 2 intern developers 4096 Sep 19 09:26 projects [intern@intern-a1t-inf-lnx1 ~]$ touch /home/projects/project_plan.txt [intern@intern-a1t-inf-lnx1 ~]$ ls -l /home/projects/project_plan.txt -rw-rw-r-- 1 intern intern 0 Sep 19 09:28 /home/projects/project_plan.txt [intern@intern-a1t-inf-lnx1 ~]$ su -l newuser Password: [newuser@intern-a1t-inf-lnx1 ~]$ cd /home/projects [newuser@intern-a1t-inf-lnx1 ~]$ touch test.txt touch: cannot touch 'test.txt': Permission denied [newuser@intern-a1t-inf-lnx1 ~]$ ls -l /home total 8 drwxr-x--- 10 intern intern 4096 Sep 18 14:53 intern drwxr-xr-x 2 intern developers 4096 Sep 19 09:28 projects
- As you can see even though we are part of the group developers that own the directory we can't still write since the group only has read and execute access.
- If you don't know how to read the file permission yet we will discuss it in the next topics
Task 4: Deleting User Accounts and Groups
- Remove the user
newuser
from the system using theuserdel
command. - Verify that
newuser
is no longer listed in/etc/passwd
. - Remove the
developers
group from the system using thegroupdel
command. - Verify that the
developers
group is no longer listed in/etc/group
.[intern@intern-a1t-inf-lnx1 ~]$ cat /etc/passwd | grep newuser newuser:x:1001:1001::/home/newuser:/bin/sh [intern@intern-a1t-inf-lnx1 ~]$ sudo userdel newuser [intern@intern-a1t-inf-lnx1 ~]$ cat /etc/passwd | grep newuser [intern@intern-a1t-inf-lnx1 ~]$ cat /etc/group | grep developers developers:x:1002: [intern@intern-a1t-inf-lnx1 ~]$ sudo groupdel developers [intern@intern-a1t-inf-lnx1 ~]$ cat /etc/group | grep developers
Conclusion:
In this lab exercise, you've gained hands-on experience with managing user accounts, creating new users, working with user groups, and configuring user home directories in a Linux environment. Understanding user management is essential for system administration and security.