Creating user in linux with home directory

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.

Источник

How do you Add a User to Linux

Linux provides you the most commonly used command that is “useradd” or “adduser.” It allows you to add or create the user to the Linux system and some properties that may differ from another user. These users can access some files or directories, and some users may not depend on what access they have. Whenever a user is added or created, access is given to that user as per the requirement.

The “useradd ” command may vary for various Linux distributions, so it is recommended that you read the documentation properly before you create a new user account in Linux. We highlight the methods on how you can add or create a new user within the Linux system.

Whenever you run the “adduser” command on the terminal, it will perform the following tasks.

  • You are allowed to make changes to the mentioned Linux files – /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow whenever you create a new user account.
  • You are allowed to create and populate the user’s home directory whenever you create the new user.
  • You are allowed to set permissions and ownerships for the new users to the home directory.

Syntax of the “adduser” command as follows

Adding New User in Linux

For adding or creating a new user, use the “adduser” or the “useradd” command along with the username. The username can be a login name for login into the Linux system. You can add only one user and must be different from another user existing on the system.

Suppose you want to add the user named “Sammy,” then run the below-mentioned command on the terminal.

If you use the “useradd” command, the user is created in the locked state, and for unlocking that user account, set a password for that account using the “passwd” command as shown below.

After creating the new user, you will see a new entry automatically added to the “/etc/passwd” file, and it will store the user’s information in a unique format, as shown below.

The above information will specify the following fields separated by a colon in respective order.

  • Username: specifies the user login name for logging into the system and should be up to 32 characters long.
  • Password: this user credential is stored in the /etc/shadow file as an encrypted format.
  • User ID (UID): every user has a User ID known as a User Identification Number.
  • Group ID (GID): stands for Group Identification Number and is stored in /etc/group file along with the other details of the user.
  • User Info (optional) will store extra information, such as full user name and others.
  • Home Directory: it helps in defining the absolute location of the user’s home directory.
  • Shell specifies the absolute location of a user’s shell, i.e.,/bin/bash.
Читайте также:  Драйвер synaptics touchpad linux

Creating a User With a Different Home Directory

Whenever you run the “adduser” command, it will create the user’s home directory under the /home directory along with the username. If you want to change the location of the home directory for the new user, use the “-d” option along with the “adduser” command.

The below command will create the user “nick” with the home directory as “/data/nick.”

If we check the content of the /etc/passwd file, you will find the following information about the user nick.

[ root @ source ~ ] # cat /etc/passwd | grep nick

nick:x: 505 : 505 :: / data / nick: / bin / bash

Creating a User With a Specific User ID

A Unique Identification Number (UID) is borne by every user. While creating the new user in Linux, the system will assign the numbers 500, 501, 502, etc., by default. However use the “-u” option along with the “adduser” command if you want to assign a specific user ID to the newly created user,

Here, we add a user “diva” and assign a new UID as “1122,” as shown below.

To check the added details about the user, check the content of the “/etc/passwd” file, and you will see the following user details.

[ root @ source ~ ] # cat /etc/passwd | grep diva

diva:x: 1122 : 1122 :: / home / diva: / bin / bash

Creating a User With a Specific Group ID

Every user on the Linux system has a Group Identification System. You can use the “-g” option along with the “useradd” command for creating a user with a specific group ID. in the below example; we are adding a user named “geet” with a specific GID and UID using the “-u” and “-g” options.

For checking the detail of the newly added user to the system, check the content of the /etc/passwd file as shown below.

[ root @ source ~ ] # cat /etc/passwd | grep geet

geet:x: 1000 : 500 :: / home / geet: / bin / bash

Adding a User to Multiple Groups

If you wish to add a new user to multiple groups, mention the “-G” option along with the “adduser” command. You can separate the various groups using commas without any space, as shown below.

You can check the multiple groups assigned to the user using the “id” command as shown below.

[ root @ source ~ ] # id downey

uid = 1001 ( downey ) gid = 1001 ( downey )
groups = 1001 ( downey ) , 500 ( admins ) , 501 ( webadmin ) , 502 ( developers )
context =root:system_r:unconfined_t:SystemLow-SystemHigh

Adding a User Without the Home Directory

Suppose you don’t want to assign a home directory to the newly added user due to security users than the “-M” option along with the “adduser” command. Here we are creating a new user, “Nancy” without a home directory for it.

If you want to check the user is created without the home directory, use the below-mentioned “ls” command as shown below.

[ root @ source ~ ] # Is -l/home/nancy

Is: cannot access / home / nancy: No such file or directory

Creating a User with an Account Expiry Date

Whenever we add the user using the “adduser” command, the account will never expire as the expiry date is zero. But you can add the expiry date for the user using the “-e” option and the “useradd” command, as shown below.

Читайте также:  Linux software and hosting

It will create a temporary account for the specified time given in the “YYYY-MM-DD” format.

For checking the account age and password of the user, you can use the “chage” command for the “Nancy” user.

Creating a User with Password Expiry Date

If you want to define the number of days after the expiration of a password, you can use the “-f” option along with the “useradd” command. The “0” value will remain inactive for the user as the password expires. By default, the password expiry value is “-1,” which means it will not expire. But if you want to create a temporary user with some password expiration date, use the “-f” option along with the “useradd” command, as shown below.

Adding a User with Custom Comments

If you want to add some comments to the user’s details in the /etc/passwd file, use the “-c” option and the “useradd” command. Add the comment as a single line without having any space, as shown below.

Here, we add a user “mathew” along with the full name “mathew stellar”.

You can see the added details in the /etc/passwd file as shown below.

[ root @ source ~ ] # tail -1 /etc/passwd

mathew:x: 1006 : 1008 :Mathew Stellarr: / home / mathew: / bin / sh

Changing User Login Shell

Whenever we add a new user, they do not have to do anything with the login shell. However, often there is a need to assign a different login shell to the users. Linux allows assigning different login shells to each user using the “-s” option along with the “useradd” command.

Here we add the user “dash” without the login shell (/sbin/nologin) shell.

You can check the details under the /etc/passwd file.

[ root @ source ~ ] # tail -1 /etc/passwd

dash:x: 1002 : 1002 :: / home / dash: / sbin / nologin

Adding a User With Specific Home Directory, Default Shell, and Customized Comment

We are using the following command for adding a new user “raj” with the home directory as “/var/www/xyz,” with default shell as “/bin/bash,” and adding some extra information regarding the user.

In the above example, the -m and -d options are used for creating a new user with the specified home directory, and the -s option will set the default shell for that user, and the -c option will add the required comment for the user. Check the added information in the /etc/passwd file.

Conclusion

The Linux system has a different way of adding a new user and related details stored in files present on the Linux system. You can use either the “useradd” or “adduser” command as your Linux distribution to add a new user to the system. Also, you will get to use various options along with the command for adding additional details for changing the default settings for the added user. You can go through this article and understand the detailed use of the “useradd” command and its various options.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.

Источник

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