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
whoamicommand to display your current username. - Create a new user account named
newuserusing theuseraddcommand. - New user account creation needs sudo privileges
- Set a password for the new user using the
passwdcommand. - As a sudo user you can change another users password
- Switch to the
newuseraccount using thesucommand. - Display the current user and verify that you are now the
newuser. - Exit the
newuseraccount using theexitcommand.[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/groupcommand. - Create a new group named
developersusing thegroupaddcommand. - Similar to useradd groupadd require sudo privileges as well
- Add your
newuseruser account to thedevelopersgroup using theusermodcommand. - Verify that your user is now part of the
developersgroup. - 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
/homedirectory. - List the contents of the
/homedirectory to view user home directories. - Create a new directory named
projectsin your home directory. - Set the ownership of the
projectsdirectory to your user and the groupdevelopers. - Verify the ownership and permissions of the
projectsdirectory. - Create a text file named
project_plan.txtinside theprojectsdirectory. - Check the ownership and permissions of the new text file.
- Switch to the
newuseraccount using thesucommand. - Attempt to access the
projectsdirectory 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
newuserfrom the system using theuserdelcommand. - Verify that
newuseris no longer listed in/etc/passwd. - Remove the
developersgroup from the system using thegroupdelcommand. - Verify that the
developersgroup 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.