Practical 5: Perform an operation of operating system for User Management System.
2024, February 21
This documentation guides through common user management operations on Kali Linux, including user creation, modification, deletion, and managing permissions.
Remove any existing test users to ensure a clean environment:
sudo deluser --remove-home testuser
sudo deluser --remove-home anotheruser
sudo groupdel testuser
Create users 'testuser' and 'anotheruser', and set passwords:
# Optional: Create the group if it does not exist
sudo groupadd groupname
# Verify the group was created
getent group groupname
# Create user 'testuser' without creating a group, and set password
sudo useradd -m -N testuser
echo "testuser:password" | sudo chpasswd
# Verify 'testuser' creation and properties
id testuser
grep testuser /etc/passwd
# Create user 'anotheruser' without creating a group, and set password
sudo useradd -m -N anotheruser
echo "anotheruser:password" | sudo chpasswd
# Verify 'anotheruser' creation and properties
id anotheruser
grep anotheruser /etc/passwd
# Add 'testuser' to an existing group
sudo usermod -a -G groupname testuser
# Verify that 'testuser' has been added to 'groupname'
id testuser
Perform operations such as locking, unlocking accounts, and setting password expiry:
echo "Before any changes:"
sudo chage -l testuser
echo "Locking 'testuser':"
sudo usermod -L testuser
echo "After locking 'testuser':"
sudo passwd -S testuser
echo "Setting password expiry for 'testuser' to 30 days:"
sudo chage -M 30 testuser
echo "After setting password expiry for 'testuser':"
sudo chage -l testuser
echo "Unlocking 'testuser':"
sudo usermod -U testuser
echo "After unlocking 'testuser':"
sudo passwd -S testuser
'testuser' creates a file, then change ownership and permissions:
# Create a file as 'testuser' and write to it
sudo -u testuser touch /home/testuser/testfile.txt
echo "This is a file created by testuser." | sudo -u testuser tee /home/testuser/testfile.txt
# Display the permissions and ownership before changing
echo "Before changing ownership and permissions:"
ls -l /home/testuser/testfile.txt
# Change ownership of the file to 'anotheruser'
sudo chown anotheruser /home/testuser/testfile.txt
# Set permissions for the file
sudo chmod 644 /home/testuser/testfile.txt
# Display the permissions and ownership after changes
echo "After changing ownership and permissions:"
ls -l /home/testuser/testfile.txt
Modify user details and verify changes. Check current user sessions and manage password and account expiries:
echo "Before modification:"
id testuser # Verify current user details
who # List logged-in users before changes
w # Check user activities before changes
sudo chage -l testuser # Check current password expiry details
echo "Modifying user details..."
sudo usermod -l newtestuser testuser # Change username
sudo usermod -d /home/newtestuser -m newtestuser # Change home directory
echo "After modification:"
id newtestuser # Verify new user details
who # List logged-in users after changes
w # Check user activities after changes
sudo chage -l newtestuser # Check new password expiry details
Test file access permissions for 'newtestuser' and 'anotheruser':
# Correct the ownership command to only specify the user, not the group
sudo chown newtestuser /home/newtestuser/testfile.txt
# Check file details before attempting access
echo "File details before access attempts:"
ls -l /home/newtestuser/testfile.txt
# Attempt to display the contents of the file as 'newtestuser'
echo "Trying to access file as newtestuser:"
sudo -u newtestuser cat /home/newtestuser/testfile.txt || echo "Access denied."
# Attempt to display the contents of the file as 'anotheruser'
echo "Trying to access file as anotheruser:"
sudo -u anotheruser cat /home/newtestuser/testfile.txt || echo "Access denied."
# Set appropriate permissions for demonstration
sudo chmod 644 /home/newtestuser/testfile.txt
# Check file details after setting permissions
echo "File details after setting permissions:"
ls -l /home/newtestuser/testfile.txt
Remove 'newtestuser' and 'anotheruser' to return the system to a clean state:
echo "Removing users and their home directories:"
sudo deluser --remove-home testuser
if [ $? -eq 0 ]; then
echo "testuser removed successfully."
else
echo "Failed to remove testuser."
fi
sudo deluser --remove-home anotheruser
if [ $? -eq 0 ]; then
echo "anotheruser removed successfully."
else
echo "Failed to remove anotheruser."
fi
sudo deluser --remove-home newtestuser
if [ $? -eq 0 ]; then
echo "newtestuser removed successfully."
else
echo "Failed to remove anotheruser."
fi
echo "Checking and removing group 'testuser':"
getent group testuser
if [ $? -eq 0 ]; then
sudo groupdel testuser
if [ $? -eq 0 ]; then
echo "Group 'testuser' deleted successfully."
else
echo "Failed to delete group 'testuser'."
fi
else
echo "Group 'testuser' does not exist or already removed."
fi