Linux change users home 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:

Читайте также:  Запуск install sh linux

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

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

Читайте также:  Avision ad230u драйвер 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.

Читайте также:  Linux dhcp and dns server

Источник

How to change the Home directory of the currently logged In user

I am currently logged in into a CentOS server and I would like to change my home directory from /home/myuserName/ to /var/www/html/ I tried the below command :

> sudo usermod -d /var/www/html myuserName 
usermod: user myUserName is currently logged in 

That helps, But i would like to change my home directory permanently, not for just the current session.

5 Answers 5

short answer : you can’t.

long answer:

HOME dir is set in /etc/passwd , 6th field. It is read upon login; your shell is started with this home dir.

The proper way to change home dir for joe is :

Once session is run, you must do two things:

  • edit $HOME to change home dir for session (to be repeated on all active session).
  • use sudo vipw to edit home dir for next session

Also, be aware you might have an issue with permissions/ownership on /var/www/html .

editing /etc/passwd for an account that is logged in, and then start a new session with that same account, and it obeys the new home dir. For the already logged in account, that session still has the old home location in the Environment.

You need to edit the /etc/passwd file to change home directory of users that are currently logged in.

Edit the /etc/passwd with sudo vipw and change home directory of the user.

vipw highly recommended other than vim or other editors since vipw will set lock to prevent any data corruption.

The usermod command won’t work if you’re logged in with the user you are trying to make changes on.

From the manual page on usermod it says:

CAVEATS usermod will not allow you to change the name of a user who is logged in. You must make certain that the named user is not executing any processes when this command is being executed if the user’s numerical user ID is being changed. You must change the owner of any crontab files manually. You must change the owner of any at jobs manually. You must make any changes involving NIS on the NIS server.

Try logging in with a different user and running the command again.

If that isn’t possible then you can manually edit the /etc/passwd file (which is actually what the usermod command is doing). If you do that make sure you back the file up in case you inadvertently do something silly.

Источник

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