Command to change the default home directory of a user

后端 未结 6 1100
轻奢々
轻奢々 2020-12-07 08:00

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 defaul

相关标签:
6条回答
  • 2020-12-07 08:09

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

    Edit /etc/adduser.conf

    Set DHOME variable

    0 讨论(0)
  • 2020-12-07 08:15

    usermod -m -d /newhome username

    0 讨论(0)
  • 2020-12-07 08:20

    From Linux Change Default User Home Directory While Adding A New User:

    Simply open this file using a text editor, type:

    vi /etc/default/useradd
    

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

    HOME=/home
    

    Replace with:

    HOME=/iscsi/user
    

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

    # useradd vivek
    # passwd vivek
    

    Verify user information:

    # finger vivek
    
    0 讨论(0)
  • 2020-12-07 08:26

    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.

    0 讨论(0)
  • 2020-12-07 08:33

    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
    
    0 讨论(0)
  • 2020-12-07 08:34

    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.

    0 讨论(0)
提交回复
热议问题