How to Manually Create a User in Linux and Unix (Step-by-Step Guide)

Creating users manually in Linux or Unix is a valuable skill that provides a deeper understanding of how user management works under the hood. While the useradd command offers a quick way to add users, doing it by hand teaches you what changes are made behind the scenes. This method can be particularly useful in minimal environments or custom configurations. For a broader look at foundational commands that help you navigate the terminal more effectively, check out this list of 15 basic Linux commands.

Let’s walk through how to manually create a new user called DarthVader. This process includes editing essential system files such as passwd, shadow, and group, all of which are located in the /etc folder. We’ll also set up a home directory and assign the proper permissions. If you’re also interested in path manipulations or retrieving system info via CLI, refer to this guide on basename and dirname or this article on gathering computer information.

  1. Backup essential system files: Before making any changes, it’s important to back up the passwd, shadow, and group files located in the /etc folder. These hold user data, encrypted passwords, and group information. Run the following as root:
sudo su
cd /etc
cp passwd{,.bak}
cp shadow{,.bak}
cp group{,.bak}
ls -l *.bak

This ensures you have a restore point in case of errors.

  1. Add the user to the passwd file: This file holds basic user account details. Change directory first, then append the new user:
cd /etc
echo 'DarthVader:x:1699:1699:Dark Side:/home/darth/vader:/bin/bash' >> passwd

Here, 1699 is the UID and GID, Dark Side is the full name/comment, the home directory is /home/darth/vader, and the login shell is /bin/bash.

  1. Add the user to the shadow file: This file stores encrypted passwords and password-related info. Change directory first, then use:
cd /etc
echo 'DarthVader:x:15481:0:99999:7:::' >> shadow

This line sets the initial password entry (x placeholder), last password change date (15481), and password expiration fields.

  1. Add the user to the group file: This file contains group definitions. Change to the /etc folder and append:
cd /etc
echo 'DarthVader:x:1699:' >> group

This assigns the group ID 1699 to the DarthVader group.

  1. Create the user’s home directory:
mkdir -p /home/darth/vader

The -p flag ensures parent directories are created if they don’t exist.

  1. Populate the home directory: Copy the default configuration files from /etc/skel, which provides a basic user environment:
cp -r /etc/skel/. /home/darth/vader/
  1. Set ownership: Make the new user and their group the owner of their home directory:
chown DarthVader:DarthVader /home/darth/vader
  1. Set permissions: Assign appropriate access rights:
chmod 755 /home/darth/vader/
chmod -R 644 /home/darth/vader/*

This allows full access for the user, and read/execute for others.

  1. Set the user’s password: Run the passwd command to assign a secure password:
passwd DarthVader

You’ll be prompted to enter and confirm the new password.

By following these steps, you’ve manually created a fully functional user in Linux. This approach is a great way to build deeper system knowledge and understand what tools like useradd do automatically. It also prepares you to handle advanced system administration tasks with more confidence and precision.

5 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *