Linux set users home directory

How to make user home folder after account creation? [duplicate]

I created a new user with useradd name and forgot to use -d -m to create their home directory. I tried making one, copying the contents of /etc/skel, and chown ing everything to the new user. Aliases don’t work, such as ll , and I just have a $ at the command prompt, instead of name@server ~$ . Also, using the scroll wheel dumps garbage on the command line 🙁 How do I fix this, or is it easier to delete the user and start over?

I haven’t figured out what else was needed from that point, but I fixed it by deleting the user and directory and starting over with adduser instead of useradd.

I think that would result in a different userid and groupid which may or may not be a problem for you.

6 Answers 6

You have $ at the command prompt because you are using the sh shell.

The shell with name@server is based on the bash shell.

You have to change the default shell for the newly created user via : usermod -s /bin/bash .

Using usermod again to add the user home directory if it wasn’t present. usermod -d /home/username

If the user has no home directory specified in /etc/passwd :

Run mkhomedir_helper to create the home directory. mkhomedir_helper will create the user home directory and copy the stuff in /etc/skel as well.

If the user already has a home directory specified in /etc/passwd :

Such as via usermod -d /some/directory , mkhomedir_helper will not work. The only way is to manually create the home directory for the affected user.

Источник

How to Change Default User Home Directory in Linux

Before we can jump into changing the default user home directory on a Linux operating system environment, we should brief through some theoretical and practical information related to the Linux home directory.

By definition, Linux is a multi-user operating system, which creates the need for a universal directory called the Home directory where different OS users can store and create personalized/user-centered files and directories.

These files and directories are only accessible to the homeowner (currently logged-in user). Therefore, each time a new user is created on a Linux environment, the user is associated with a unique home directory accessible only to that user.

On the other hand, the existence of these universal directories also permits a universal user (other than the homeowner) to have access to them. This user is known as root and can access all homeowner directories within a Linux system.

The syntax associated with a user’s Home directory is as follows:

For instance, if a Linux system has a user called dnyce, then the absolute path to this user’s Home directory will be:

Читайте также:  About fstab in linux

We can even list the permission settings associated with the user in the following manner:

List User Home Directory

This article will walk us through changing the default user home directory on a Linux operating system environment.

Benefits of Having a Linux Home Directory

We can summarize the benefits of having a Linux Home directory in the following manner.

  • User Access Restriction – It is only the homeowner and root user that can access this directory making it impossible for other users to tamper with existing customization settings and stored configuration files. Therefore, privacy and security are guaranteed.
  • Controlled Environment – Since multiple homeowners can exist within a single Linux system, their restriction on their Home directories is very advantageous. For instance, if a homeowner’s Home directory contains malicious code in stored executable files, the execution of this code will only affect that user environment and not the entire system.
  • Controlled Flexibility – The user’s Home directory is automatically created during the Linux installation. During this installation process, a user can decide to assign this directory an exclusive partition. This step makes it easier to execute backup and restoration tasks.

Changing Default User Home Directory in Linux

We will need to create a sample Linux user account for this tutorial to be more practical. Since creating a new user in Linux is associated with a Home directory, let us create one.

# useradd -m homeowner # passwd homeowner

The -m flag makes sure that a default user Home directory is created and set a password to the user.

Create New User in Linux

We can now switch to the newly created user account and confirm the default user home directory.

Check User Home Directory

Switch back to the root user account.

and create a new directory replacement for the default user Home directory.

We will assign user homeowner directory ownership for the newowner.

# chown -R homeowner:homeowner /home/newowner

Now, to change the user’s default Home directory from /home/homeowner to the new directory /home/newowner, we will make use of the usermod -d command in the following manner.

# usermod -d /home/newowner homeowner

The -d flag sets the directory.

Change Linux User Home Directory

We can switch back to the homeowner account and confirm the changes.

Confirm User Home Directory

Alternatively, we could set the preferred user Home directory during user creation in the following manner.

# useradd -m -d /home/newowner homeowner

The -d flag sets the directory.

While changing the default user Home directory, we might also need to migrate/move already existing user content to the new location. The usermod command to implement is:

# usermod -m -d /home/newowner homeowner

The -m flag moves the old directory content to the new one.

We can now easily change the default Home directory in Linux. I hope this article guide was helpful. Feel free to leave a comment or feedback.

Источник

Command to change the default home directory of a user [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

I would like to know whether there is any simple shell command to change the user home directory in Linux/Unix (one similar to chsh which changes the default login shell of an existing valid user) without touching the /etc/passwd file. Thanks

Читайте также:  Indicator netspeed linux mint

stackoverflow.com/questions/20071714/…, says that usermod -d /home/userxx /home/useryy to change linux default login directory to /home/useryy

Why do you ask about the default home directory? Do you want to change the home directory of one particular existing user, or of all users added in the future? Notice that chsh don’t change the default login shell, but just the login shell of a given user. To change the HOME of a particular user, just edit /etc/passwd

I’m voting to close this question because it has nothing to do with programming and would have been best suited for Unix & Linux or Super User.

7 Answers 7

Ibrahim’s comment on the other answer is the correct way to alter an existing user’s home directory.

Change the user’s home directory:

usermod -d /newhome/username username 

usermod is the command to edit an existing user.
-d (abbreviation for —home ) will change the user’s home directory.

Change the user’s home directory + Move the contents of the user’s current directory:

usermod -m -d /newhome/username username 

-m (abbreviation for —move-home ) will move the content from the user’s current directory to the new directory.

Not only move the content, I see it more like move whole folder = original folder ( /home/username typically) is removed.

@STW Is this a reversible task? Meaning if I just run it once and then flip the parameters, will my home directory be exactly the way it was before?

The order of options for the second command is wrong. usermod -m -d /newhome/username username works.

@KolonUK because that’s the only correct way really. When you are logged in as the user, you have applications opened which rely on that home directory. You shouldn’t be changing the location while everything is running. So yeah, creating a new user account and deleting is not that bad at all. Or you could set a password for root and login as root in a new TTY.

Simply open this file using a text editor, type:

The default home directory defined by HOME variable, find line that read as follows:

Save and close the file. Now you can add user using regular useradd command:

# useradd vivek # passwd vivek 

usermod -m -d /path/to/new/login/home/dir user changes existing user home directory to a new login directory which is created if it does not already exist, option -m moves the contents of the current home directory to the new home dir

The accepted answer is faulty, since the contents from the initial user folder are not moved using it. I am going to add another answer to correct it:

sudo usermod -d /newhome/username -m username 

You don’t need to create the folder with username and this will also move your files from the initial user folder to /newhome/username folder.

In case other readers look for information on the adduser command.

Edit the user home directory and then move the required files and directories to it:

cp/mv -r /home/$user/.bash* /home/newdir .bash_profile .ssh/ 

Set the correct permission

chmod -R $user:$user /home/newdir/.bash* 

Found out that this breaks some applications, the better way to do it is

Читайте также:  Просмотр windows сети linux

In addition to symlink, on more recent distros and filesystems, as root you can also use bind-mount:

mkdir /home/username mount --bind --verbose /extra-home/username /home/username 

This is useful for allowing access «through» the /home directory to subdirs via daemons that are otherwise configured to avoid pathing through symlinks (apache, ftpd, etc.).

You have to remember (or init script) to bind upon restarts, of course.

An example init script in /etc/fstab is

/extra-home/username /home/username none defaults,bind 0 0 

Источник

How to Create Home Directory for Existing User in Linux

By default when you create a user in Linux, users default home directory is created under /home. If you noticed on Ubuntu and Debian derivated distribution useradd command won’t create a home directory by default.

Let’s think of a situation where you have already created a user but the home directory is missing. In this tutorial, I will show you how to create a default home directory for an existing user in Linux.

Create default home directory for existing user

Here I am using Ubuntu 22.04 and going to create a user named ‘ bob’ using useradd command:

Useradd command has added an entry home directory in /etc/passwd file

$ grep bob /etc/passwd bob:x:1003:1003::/home/bob:/bin/sh

If I try to login as the user using su — , it shows that it’s logging in with Home=/ . This means the user home directory is not created.

$ su - bob Password: No directory, logging in with HOME=/

In Linux, a user’s default home directory is /home. To create a default home directory use mkhomedir_helper command.

Make sure to run mkhomedir_helper command as root or user with sudo access.

$ sudo mkhomedir_helper bob

The previous command creates a home directory named «/home/bob» and user settings files.

$ ls -al /home/bob total 20 drwxr-xr-x 2 bob bob 4096 Jun 1 02:26 . drwxr-xr-x 5 root root 4096 Jun 1 02:26 .. -rw-r--r-- 1 bob bob 220 Jun 1 02:26 .bash_logout -rw-r--r-- 1 bob bob 3771 Jun 1 02:26 .bashrc -rw-r--r-- 1 bob bob 807 Jun 1 02:26 .profile

For a graphical environment (such as GNOME or XFCE ), if you are missing subdirectories in the home directory, the user needs to log out and log in back.

When the user login the first time all subdirectories such as Pictures, Documents, Videos, and Downloads folders can be created in the home directory.

Another method is to delete the user and create a new user using -m or —create-home option.

The following command creates a home folder (-m) and set the specified home directory (-d) as the value for the new user’s login:

$ sudo useradd -m -d /home/bob01 bob01

Conclusion

To conclude, If you are a Ubuntu fan you should be now using adduser command, it’s recommended by Debian. If you have an existing user, now you should be able to add default directory.

Thanks for reading and please drop your suggestions on the below comment section.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

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