Creating/Deleting Users
Absolutely! Let’s dive into user management in Ubuntu, breaking down each command used for viewing, creating, modifying, and deleting users. Here’s everything you need as a newcomer to Linux and Ubuntu.
1. Commands to View Existing Users in Ubuntu
/etc/passwd File:
The
/etc/passwdfile contains a list of all users on the system. Each line in this file represents a single user, including the root user and any system or application users created during system setup.To view this file, use the command:
cat /etc/passwdEach entry in
/etc/passwdhas several fields, separated by colons (:), with information such as the username, user ID (UID), group ID (GID), user description, home directory, and default shell.
cut Command to Display Only Usernames:
To focus only on usernames, you can extract the first field of each line in
/etc/passwdwith thecutcommand:cut -d: -f1 /etc/passwdThis command lists only usernames, making it quick to scan the system for existing users.
getent Command:
The
getentcommand retrieves entries from system databases configured in/etc/nsswitch.conf, including thepasswddatabase for users.Run this command to list all users:
getent passwdThe
getentcommand is useful for querying network-based user databases as well if you’re in a corporate environment with directory services like LDAP.
2. Creating a New User in Ubuntu
adduser Command:
The
addusercommand is the most user-friendly way to create new users. It prompts you to enter details like password, full name, room number, and more.To create a user, use:
Example:
Explanation of Steps:
This command will:
Create a new entry for
johndoein/etc/passwdand/etc/shadow.Create a home directory at
/home/johndoe.Copy default configuration files from
/etc/skelto the new home directory.Set up user information and prompt for a password.
useradd Command:
useraddis a low-level command for creating users, and it doesn’t prompt for additional information by default.To create a user with
useradd, use:Example:
After creating the user, set the password manually:
Difference Between
useraddandadduser:useraddonly creates a user account, without creating a home directory or copying files from/etc/skel.adduseris more comprehensive and typically recommended for new users.
3. Modifying Existing Users
usermod Command:
The
usermodcommand modifies user account settings, like username, home directory, or shell.Common options:
Change Username:
Change Home Directory:
Add User to a Group:
Set Default Shell:
Example:
This command adds
johndoeto thesudogroup, granting administrative privileges.
4. Deleting a User in Ubuntu
deluser Command:
The
delusercommand is a simple way to remove a user account.Basic syntax:
Example:
This removes
johndoefrom/etc/passwdand/etc/shadowbut retains the home directory by default.Remove User and Home Directory:
To delete both the user and their home directory, use:
Example:
This command deletes
johndoeand removes/home/johndoe.
userdel Command:
Similar to
deluser, theuserdelcommand deletes a user account.Basic syntax:
Example:
Remove Home Directory:
To remove the home directory as well, use:
Example:
5. Additional Commands and Tips
passwd Command:
The
passwdcommand sets or changes a user’s password.Basic syntax:
Example:
It prompts you to enter a new password for the user and updates it in
/etc/shadow.
id Command:
The
idcommand displays user identity information, including UID, GID, and groups.Basic syntax:
Example:
This command shows details like UID, primary GID, and supplementary groups.
finger Command:
The
fingercommand (if installed) provides detailed information about a user.Install it with:
Example:
Summary
Viewing Users: Use
cat /etc/passwd,cut -d: -f1 /etc/passwd, orgetent passwd.Creating Users: Use
adduserfor guided setup oruseraddfor a low-level approach.Modifying Users: Use
usermodto update usernames, home directories, groups, or default shells.Deleting Users: Use
deluseroruserdelto remove user accounts, with options to delete home directories as well.
This guide should provide a solid foundation for managing users in Ubuntu. Let me know if you want more insights on any specific aspect!
Last updated