Create users in linux with home directories

2 ways to Create New User with home directory in Linux

In Linux, the home directory (also called “home” or “home folder”) is a directory designated for a particular user, where their personal files and configuration settings are stored.

Each user on a Linux system has their own home directory, and it is identified by a tilde (~) followed by the user’s username. For example, if the username is “john”, his home directory would be ~john.

In this article, we will explore how to create a new user with a home directory in Linux.

Home directory for New user in Linux

There are two files that define the default info for new users.

  • /etc/login.defs – This file defines entries for mail, password complexity and limitations, UID and GID minimum and maximum values, and whether the user’s home directory is created by default. The CREATE_HOME parameter in the /etc/login.defs file is used to specify whether or not to create a home directory for a user when the user account is created.
  • /etc/default/useradd – This file defines the defaults of shell, home directory, skel file, group id etc. The HOME parameter in the /etc/default/useradd is used to specify the default home directory location for new users.

By default, a user’s home directory is usually located at /home/username where “username” is the name of the user account.

However, we can actually place a user’s home directory just about anywhere you’d like. Linux gives us the option to choose a location for the home directory whenever we are creating a new user.

To add a new user in Linux: Use the command useradd test (“test” is the new user’s name). Use sudo useradd test if we lack the proper privileges.

To create a user with a home directory in Linux:

  • Use sudo useradd -m test to create a user with the default home directory
  • Use sudo useradd -m -d /test test to create a user with a non-default home directory

Create a New User With a default Home Directory location in Linux

The CREATE_HOME parameter in the /etc/login.defs file is used to specify whether or not to create a home directory for a user when the user account is created. The default value of CREATE_HOME is yes, which means that a home directory will be created for a new user account.

If you set CREATE_HOME to no, then the system will not create a home directory for the user when the account is created. This can be useful in situations where you have a large number of users and you do not want to create a home directory for each user.

Читайте также:  Лучший векторный редактор для linux

To change the value of CREATE_HOME in the /etc/login.defs file, you will need to edit the file using a text editor with administrative privileges, such as the vi or nano editor. Once you have opened the file, locate the line that contains the CREATE_HOME parameter and change its value to either yes or no, depending on your requirements.

It is important to note that changing the value of CREATE_HOME in the /etc/login.defs file will only affect new user accounts that are created after the change has been made. Existing user accounts will not be affected.

You can easily create a new user with a home directory in Linux using useradd command with -m option. It will force the creation of a home directory for new users no matter the configuration of CREATE_HOME in /etc/login.defs file.

If the CREATE_HOME is configured to no, a home directory will be not created for the new user. In this case, w e can use the following command:

It will force to create a home directory for the user test .

Here is the man info about this option.

Create the user’s home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory.

Here is an example of how to create a new user with a home directory in Linux:

  • Open a terminal window on your Linux system.
  • Run the following command as a user with administrative privileges to create a new user with a home directory:
    • sudo useradd -m newuser
    • In this command, newuser is the name of the user you want to create.
    • The -m option tells the useradd command to create a new home directory for the user.
    • sudo passwd newuser
    • This will prompt you to enter and confirm a new password for the user.

    Create a New User With a non-default Home Directory location in Linux

    The /etc/default/useradd file is a configuration file for the useradd command in Linux/Unix systems. It contains default settings for user account creation, such as the default home directory, default shell, default user group, etc.

    For example, if the HOME variable is set to /home, then the home directory for a user named “jdoe” would be /home/jdoe.

    The home directory for a user can be changed when the user is createad.

    If we want the user to have a home directory in a different place than the default, use the useradd -d option.

    This command will create a new user test with the home directory under “/test”. The user’s files will be put to this home directory.

    The above command will change the home directory of test user to /test under the root directory.

    Linux Troubleshooting Guide:

    David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

    howtouselinux.com is dedicated to providing comprehensive information on using Linux.

    We hope you find our site helpful and informative.

    Источник

    Creating A User with Different Home Directory in Linux

    Linux is a powerful multi-user operating system. It can have many concurrent users accessing the system at the same time. The system admin can manage permissions for each user to specify which user can access what part of the system.

    This guide will demonstrate how to create a new user with a different home directory in Linux.

    User home directory

    In Linux, each user gets its own home directory with exceptions like various system accounts. The home directory is a dedicated directory for the particular user to store the user-specific files. It’s also referred to as the “login directory”. Whenever logging in, the user will land on the respective home directory.

    By default, all the users in the system have their home directories located at the following location.

    Each home directory is named after the username of the user. For example, the home directory for the user “viktor” will look like this.

    However, we can establish a different location for the user directory. It can be set during the creation of the user account or moved later.

    Creating a user with a different home directory

    Creating a new user

    Each Linux system comes with useradd, a dedicated tool to create and update user accounts. It’s only available to the root user and non-root users with sudo privileges.

    To add a new user to the system, run the following useradd command. The flag “-m” tells useradd to create a dedicated home directory for the new user. If not used, then the user won’t have a dedicated home directory.

    The user is added to the system. The user is also registered to various database files (/etc/passwd, /etc/shadow, /etc/gshadow, and /etc/group).

    The user isn’t accessible yet. The following command will assign a login password for the new user.

    The user is ready and fully functional. Access the new user.

    Check the location of the home directory of the new user.

    Creating a user with a custom home directory

    By default, useradd will create the user’s home directory under “/home”. To specify the home directory in a different location, use the flag “-d”. Note that the directory must exist beforehand.

    As always, use passwd to assign a login password for the new user.

    Verify if the new user has a different home directory.

    Moving existing user home directory

    We can also assign a different home directory for an existing user. It will not move the contents of the existing home directory automatically to the new location.

    Create a new directory. It will be the new home directory of an existing user.

    Allow the new user complete access over the new directory.

    Move all the contents of the existing user home directory to the new one.

    Assign the new directory as the home of the user.

    Verify the change.

    Final thoughts

    The home directory is an important part of a normal user account on Linux. This guide demonstrates how to assign a custom home directory to a new and existing user. These methods apply to any Linux distro.

    About the author

    Sidratul Muntaha

    Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

    Источник

    Adding New Users in Linux With Useradd Command

    The useradd command lets a superuser create a new user account on Linux. Here’s how to use the useradd command with various options.

    The useradd command lets a superuser create a new user account in Linux.

    It is a low level utility that doesn’t do a lot of things by default but provides several options to create users with various configuration.

    Here’s the syntax of useradd command:

    Let’s see how to use useradd command.

    Useradd command examples

    Add User Account In Linux

    Keep in mind that you need to be root or a sudo user in order to use this command.

    Add a new user in Linux with useradd command

    You can use the useradd command without any options like this:

    It will create the user account but:

    • the user’s home directory won’t be created
    • the password has to be set separately
    • the default shell for the user will be sh

    You can also create home directory while creating the user.

    Add a user with home directory with useradd command

    The -m option of useradd command allows to copy all files from your system skeleton directory (/etc/skel) to the newly created home directory.

    In other words, you can create a user with home directory using the -m option as well.

    You can also specify an existing directory as the home directory of the newly created user with option -d.

    useradd -d Path_to_Existing_Directory new_username

    Add a new user with different shell

    The default shell for a user created with useradd command is sh. These days sh shell is hardly used when there is bash and zsh.

    A user can change his/er default shell but you can also create the user with a different default shell with the option -s.

    For example, if you want the new user to use bash as the default shell, here’s what you can do:

    useradd -s /bin/bash new_username

    Add a new user with different group

    Usually, when you create a new user, a group with the same name as the user is created. The new user is added as the member of this group.

    With the option -g, you can add a new user to an already existing group as its default group.

    useradd -g Existing_Group_Name_or_ID new_username

    Suppose you are creating an account for a developer. Adding all the developers to a ‘dev group’ could be a strategy.

    You can also add the user to additional (existing) groups with option -G.

    useradd -G group_1 group_2 new_username

    So if you are creating a sudo user, you can simply add the user to the sudo group while creating it.

    Add a new user with specific user ID (UID)

    You may also create a new user with a specific user ID with the option -u of useradd command:

    useradd -u uid new_username

    Bonus Tip: Combine multiple options to create a new user with different parameters

    You can combine multiple options together to create a new user in Linux with a predefined configuration.

    useradd -d /home/abhishek -s /bin/bash -g my_group 

    As you can see, the useradd command by default doesn’t add much. This is why some people prefer to use the adduser command. You can read about difference between useradd and adduser, if interested.

    I hope you found the useradd command examples useful. You may also want to learn to delete users with userdel command. Questions and suggestions are welcome.

    Источник

Оцените статью
Adblock
detector