Linux change default directory

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

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

Читайте также:  Divinity original sin linux

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

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 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:

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.
Читайте также:  Linux find all files with string

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.

Источник

How to Change the Default Home Directory of a User on Linux?

When you create a user on a Linux system, that user is given a default home directory. This home directory is a personal space where the user can store their files and settings. Sometimes it may be necessary to change a user’s default home directory on a Linux system. This could be because you want to provide a different location for the user’s files or because you want to change the user’s name and update the home directory accordingly. In this article, we will discuss how to change a user’s default home directory on a Linux system.

Before you get started, there are some prerequisites you should be aware of −

  • You must have root access on the Linux system. This means that you must be logged in as the root user or have superuser privileges.
  • The new home directory must already exist on the system. This means that the new home directory must be created before a user’s default home directory can be changed. (You can use mkdir command to create a directory)

Changing the Default Home Directory

There are two methods you can use to change a user’s default home directory on a Linux system: using the “usermod” command or editing the “/etc/passwd” file. Let’s discuss both methods in detail.

Use the usermod command

The usermod command is a utility that allows you to modify user accounts on a Linux system. You can use it to change various user attributes, including the home directory.

Читайте также:  Linux проверить версию ядра linux

To change a user’s default home directory using the usermod command, follow these steps −

  • Open a terminal window and log into the Linux system as root.
  • Type the following command to change the default home directory of a user named «john»
$ usermod -d /new/home/directory john
  • Press Enter. The default home directory for user «john» will be changed to «/new/home/directory».

To verify that the home directory has been changed, you can use the following command −

This command will display the entry for user «john» in the /etc/passwd file. The output will look like this −

john:x:1000:1000:John:/new/home/directory:/bin/bash

As you can see, the home directory has been successfully changed to «/new/home/directory».

Editing the /etc/passwd file

The “/etc/passwd” file is a system file that stores user account information on a Linux system. You can edit this file to change a user’s default home directory.

To change a user’s default home directory by editing the “/etc/passwd” file, follow these steps −

  • Open a terminal window and log into the Linux system as root.
  • Type the following command to open the “/etc/passwd” file in a text editor −
  • Find the line that corresponds to the user whose home directory you want to change. The line will look like this −
john:x:1000:1000:John:/home/directory:/bin/bash
john:x:1000:1000:John:/new/home/directory:/bin/bash
  • Press “Ctrl+X” to exit the text editor.
  • When prompted to save changes, press Y, then press Enter.

To verify that the home directory has been changed, you can use the following command −

This command will display the entry for user «john» in the “/etc/passwd” file. The output will look like this −

john:x:1000:1000:John:/new/home/directory:/bin/bash

As you can see, the home directory has been successfully changed to «/new/home/directory».As you can see, the home directory has been successfully changed to «/new/home/directory».

Move user files to new home directory

If you’ve changed a user’s default home directory, you may also want to move the user’s files to the new home directory. To do this, you can use the following commands −

$ mv /old/home/directory/* /new/home/directory/ $ mv /old/home/directory/.* /new/home/directory/

This command will move all files and directories from the old home directory to the new home directory. Make sure you replace «/old/home/directory» with the actual path to the old home directory and «/new/home/directory» with the actual path to the new home directory.

It is important to note that this command will overwrite any existing files in the new home directory with the same names as the files being moved. Make sure you back up any important files in your new home directory before running this command.

Conclusion

In this article, we have discussed how to change a user’s default home directory on a Linux system. We have seen two methods: using the usermod command and editing the /etc/passwd file. Both methods are effective and can be used depending on your specific system requirements and preferences. We also discussed how to move user files to the new home directory using the mv command. Always remember to be careful when making changes to system files, as a mistake could cause problems for your system. Be sure to back up important files before making changes, and test changes thoroughly before deploying them to a production system.

Источник

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