Creating, Modifying, and Deleting User Accounts
There are no resources for this lesson.
In this lesson, we will follow the lifecycle of a user account on a Linux/Unix system from creation, through modification, to deletion. We will also cover how to manage user groups.
Creating a User Account
useradd (Create New User Account) Let's start by creating a new user account named
sudo useradd -m -s /bin/bash john
Here is a breakdown of what that command does:
useradd: This is the command used to create a new user account.
-m: This option instructs
useraddto create a home directory for the new user. The home directory will be created under
/homewith the name of the user, in this case
-s /bin/bash: This option sets the login shell for the new user. Here,
/bin/bashis specified as the login shell, which means that the Bash shell will be launched whenever
johnlogs into the system.
john: This is the name of the user account being created.
We can run list the contents of /home and should see the users new home directory:
ll /home/ # drwxr-x--- 2 john john 4096 Oct 3 01:08 john/
Additionally, we can switch to the john user by running the following command:
sudo su john
This will show us that we have switch to the john user:
We can exit that user by typing 'exit':
This will show that you have switched back to your other user account (in our labs it is
/etc/passwd file is a text file that describes user account information. Each line in the file represents a single user account and contains seven fields separated by colons (
:). Here's the structure:
username: The name of the user.
xcharacter indicates that encrypted password is stored in
userID: The unique user ID (UID).
groupID: The primary group ID (GID).
userInfo: The user's real name or other information.
homeDirectory: The absolute path to the user's home directory.
shell: The absolute path to the user's login shell.
Let's view our new user account we just created by using grep to filter for
grep "john" /etc/passwd
This command will output information like the following:
You will notice that the password field simply shows
x in the
/etc/passwd file's password field indicates that the actual encrypted password is stored in the
/etc/shadow file, which is a more secure file accessible only by the root user. This change was made to enhance security, as the
/etc/passwd file is readable by all users, whereas the
/etc/shadow file has stricter access controls. More on the /etc/shadow file in a future lecture.
Modifying the User Account
usermod (Modify Existing User Account) Now, let’s change
John’s username to
john_doe and update his home directory:
sudo usermod -l john_doe john # Changes the username sudo usermod -d /new/home/dir john_doe # Changes the home directory
Deleting the User Account
deluser (Delete User Account) Finally, let’s delete the
john_doe user account:
sudo deluser --remove-home john_doe
useradd: Create new user accounts.
usermod: Modify existing user accounts and manage group memberships.
deluser: Delete user accounts.
See you in the next lesson!