user and group
User Management Commands
Creating a New User
Command Used:
user addis the command used to create a new user.
Example: To add a user named
test001:Enter
sudo user add test001.Note: The command must be prefixed with
sudoto gain permission and management level access.
Password Prompt:
After entering the command, you will be prompted to enter your sudo password.
Result:
Once executed, the user is created if no errors are encountered.
Viewing the List of Users
Command Used: Viewing the user list can be done by checking a specific file.
File Path: The user list is typically stored in
/etc/passwd.Command:
cat /etc/passwddisplays the list of all users, including any newly created users.
Extracting Specific User Information
To display only usernames from the user file, the
cutcommand can be used.Command Format:
cat /etc/passwd | cut -f1 -d':'Explanation: This utilizes the
cutcommand to filter by delimiter:and retrieves the first field (username).
Checking User Existence
Algorithm for Checking: To determine if a user exists:
Prompt user for input of the username.
Utilize
cat /etc/passwdwithgrepto verify existence.Use an
ifstatement to compare the input with existing usernames.
Command:
The command flow could look like:
if grep -q 'username' /etc/passwdIf it returns a result, the user exists.
User ID Command
Purpose: The
idcommand provides information about a specific user including user ID and group ID.Command Format:
id <username>Output: Displays user ID, primary group, and any supplementary groups associated with the user.
Handling Command Outputs
Null Device:
The null device can be used to discard output from a command.
Path:
/dev/nullacts as a garbage destination to redirect unwanted output.
Exit Status:
An exit status of
0indicates successful execution. A non-zero exit status indicates an error.
Deleting a User
Command Format: To remove a user:
Use
sudo userdel <username>Note: If the user does not have a home directory, you may see a warning message.
To create a user with a home directory, the command would be
sudo useradd -m <username>.
Group Management
Creating a New Group
Command:
sudo groupadd <groupname>(e.g.,sudo groupadd tester)
Viewing Groups
File Path for Groups:
Use
cat /etc/groupto view existing groups.
Extracting Group Names:
Similar to extracting usernames from
/etc/passwd, use:cut -f1 -d':' /etc/groupto get just the group names.
Adding a User to a Group
Command:
Use
sudo gpasswd -a <username> <groupname>to add the user to a group.
Deleting a User from a Group
Command Format:
Use
sudo gpasswd -d <username> <groupname>to remove a user from a group.
Checking Group Existence
Command:
Use
getent group <groupname>to check if a specific group exists.