User Management Basics
User management in Linux revolves around creating, modifying, and controlling user accounts on the system. This involves tasks such as creating new users, modifying user attributes, changing passwords, and more. Let's dive into each of these aspects.
Creating a User
To add a new user, use the useradd
command:
sudo useradd username
For our lab task, we'll create a user named "labuser":
sudo useradd labuser
Setting User Password
Securing user accounts is paramount. To set a password for a user, use the passwd
command:
sudo passwd username
For our lab task, set a password for "labuser":
sudo passwd labuser
Modifying User Attributes
User attributes such as the full name and home directory can be modified with the usermod
command:
sudo usermod -c "Full Name" -d /new/home/directory username
Let's change the full name and home directory for "labuser":
sudo usermod -c "Lab User" -d /home/labuser labuser
Enabling and Disabling Users
You can temporarily disable a user account to prevent login without deleting it. Use the usermod
command with the -L
option to lock an account:
sudo usermod -L username
To unlock the account and enable it again, use the -U
option:
sudo usermod -U username
For our lab task, we'll enable and disable the "labuser" account.
Changing a User's Default Shell
Linux users can have different default shells. To change a user's shell, use the chsh
command:
sudo chsh -s /path/to/new/shell username
In the lab task, we'll change the default shell for "labuser" to /bin/bash
.
Lab Task: Modifying User Attributes, Changing the Default Shell, and Enabling/Disabling Users
Objective: In this lab task, you will practice modifying user attributes, changing the default shell, and enabling/disabling user accounts.
Task 1: Modify User Attributes
Create a new user named "labuser" using the
useradd
command.sudo useradd labuser
Use the
usermod
command to change the full name and home directory of "labuser." Set the full name to "Lab User" and the home directory to "/home/labuser."sudo usermod -c "Lab User" -d /home/labuser labuser
Verify that the user attributes have been modified correctly by viewing the user's information in the
/etc/passwd
file.grep labuser /etc/passwd
Ensure that the output reflects the changes you made.
Task 2: Change Default Shell
Check the current default shell of "labuser" using the
grep
command:grep labuser /etc/passwd
Note the current shell (usually
/bin/sh
or/bin/bash
).Change the default shell of "labuser" to
/bin/bash
using thechsh
command:sudo chsh -s /bin/bash labuser
Verify that the default shell has been changed by checking the user's information again in the
/etc/passwd
file:grep labuser /etc/passwd
Confirm that the shell has been updated to
/bin/bash
.
Task 3: Enable/Disable User Account
Disable the "labuser" account using the
usermod
command with the-L
option:sudo usermod -L labuser
This will lock the account, preventing the user from logging in.
To re-enable the "labuser" account, use the
usermod
command with the-U
option:sudo usermod -U labuser
This unlocks the account, allowing the user to log in again.
Task 4: Test the Changes
Switch to the "labuser" account using the
su
command:su - labuser
Verify that you can or cannot log in based on whether the account is enabled or disabled.