As a system administrator, creating a user in Linux environment is a fundamental task. The skill to create a Linux user should be seamless. In this guide, we focus on how to create a user in Linux, how to set a password for the user, how to add the user to a group, how to add the user’s home directory, and how to delete the user.
Creating a User in Linux
Access the server on SSH. You need the root access to the user. Run the command below to create a user.
sudo adduser username #creates a user with the specified username.
How to create a password for the user
Run the following command to set a password for the new user
sudo passwd username #changes/sets a new password.
How to create a home Directory for a user
Once the user is created, you will need to confirm if the user’s home directory has been set up. In some cases, you will need to create a home directory manually as it does not create automatically. If the user has been created and the home directory has not been created, run the command below:
sudo mkhomedir_helper username #creates a home directory for user named username. Usually the default location will be /home/username
To check the default home directory for the user, run the command:
cat /etc/password #shows a list of users in your Linux system. Identify the user
After confirming the set home directory, you can now proceed and confirm if the path actually exists. Use the ls or the cd command.
ls /home/username #if the path exists, it returns nothing or a list of files in the path. If you get an error: No such file or directory, then this path does not exist and you will need to create one.
cd /home/username #you can also try and cd to this path. If you get an error "No such file or directory", you will need to create a home directory for your user.
How to create a user with a home directory
As mentioned earlier, creating a user using the previous method, may not automatically create a home directory for this user. To create a user and force the creation of a home directory, run the command:
sudo useradd -m username #with the -m flag, a user and their home directory is created.
How to add a Linux user to an existing group
Users can be granted to specific groups to grant them specific privileges defined for each group. The common and widely used group is the sudo group. Others include wheel, users, adm, root, etc.
sudo usermod -aG groupname username
#To add the user created to the sudo group;
sudo usermod -aG sudo username
How to Update Linux User Properties
We can change the default home directory set for the user.
sudo usermod -d /new/home username #changes the current home directory for the user
We can also update the user with additional information such as full name and phone number
sudo usermod -c "Full Name" username
How to set User IDs (UIDs)
UIDs are unique numeric identifiers assigned to each user on a Linux system. The UIDs determine the level of privilege and access given to the user. The root user has a UID of 0 while other users have a UID starting with 1000.
sudo usermod -u 1198 username #changes the UID of the user to 1198.
sudo useradd -u 1234 username #creates a new user and assigns the specified UID
How to Delete a Linux User
For some reason, you may want to delete an existing user.
sudo userdel username #deletes the user with the specified username.
sudo userdel -r username #deletes the user and their home Directory
Make a donation to support us
Related articles:
- A Practical Tutorial for Dockerizing Software Applications
- How to Configure a Docker App to a Domain Name
- Getting Started with Docker | Docker commands
- How To Run Scripts in Linux
- Deploy a Django Application on EC2 Instance with Nginx
- How to configure a domain to a docker container and install an SSL certificate on AWS