Linux 更改用户默认主目录的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20797819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:42:18  来源:igfitidea点击:

Command to change the default home directory of a user

linuxshellunix

提问by Ibrahim Quraish

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

我想知道是否有任何简单的 shell 命令可以在不触及文件的情况下更改Linux/Unix 中的用户主目录(类似于chsh更改现有有效用户的默认登录 shell)/etc/passwd。谢谢

回答by Maximin

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

Linux 在添加新用户时更改默认用户主目录

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

只需使用文本编辑器打开此文件,键入:

vi /etc/default/useradd

由 HOME 变量定义的默认主目录,找到如下所示的行:

HOME=/home

用。。。来代替:

HOME=/iscsi/user

保存并关闭文件。现在您可以使用常规的 useradd 命令添加用户:

# useradd vivek
# passwd vivek

验证用户信息:

# finger vivek

回答by STW

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

usermodis the command to edit an existing user.
-d(abbreviation for --home) will change the user's home directory.

usermod是编辑现有用户的命令。
-d(缩写--home)将更改用户的主目录。

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.

-m(缩写为--move-home)将内容从用户当前目录移动到新目录。

回答by rafaelvalle

In case other readers look for information on the addusercommand.

以防其他读者查找有关该adduser命令的信息。

Edit /etc/adduser.conf

编辑 /etc/adduser.conf

Set DHOMEvariable

设置DHOME变量

回答by Wilken

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:

除了符号链接,在最近的发行版和文件系统上,作为 root 您还可以使用绑定挂载:

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

这对于允许通过守护进程“通过”/home 目录访问子目录很有用,守护进程被配置为避免通过符号链接(apache、ftpd 等)进行路径。

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

当然,您必须记住(或初始化脚本)在重新启动时进行绑定。

An example init script in /etc/fstabis

中的一个示例初始化脚本/etc/fstab

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

回答by Sukrit Gupta

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.

您不需要使用用户名创建文件夹,这也会将您的文件从初始用户文件夹移动到 /newhome/username 文件夹。

回答by Ahmed Kay

usermod -m -d /newhome username

usermod -m -d /newhome 用户名