linux 终端默认目录,在终端中为现有用户创建默认主目录

问题描述

我该怎么做呢?我创建了一个没有主目录的用户,现在我想创建一个。不只是一个名为new_user的文件夹,我想要默认的主目录及其所有文件夹和隐藏文件等。

最佳解决方案

使用以下内容(以root用户身份,或者如果不是root用户,则使用sudo):

mkhomedir_helper username

为此,文件夹/home/username不能存在。

对于X-related文件夹(桌面,下载等),您需要在图形环境中登录;他们将在您第一次登录时自动生成。

次佳解决方案

当用户首次通过GNOME登录时,会自动创建子目录(文档,下载等),前提是用正确的权限创建主目录。这是一个演示:

alaa@aa-lu:~$ sudo useradd testinguser

alaa@aa-lu:~$ sudo passwd testinguser

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

alaa@aa-lu:~$ sudo ls -l /home

total 20

drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa

drwx------ 2 root root 16384 Jun 5 09:46 lost+found

alaa@aa-lu:~$ sudo mkdir /home/testinguser

alaa@aa-lu:~$ sudo chown testinguser:testinguser /home/testinguser

alaa@aa-lu:~$ ls -l /home

total 24

drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa

drwx------ 2 root root 16384 Jun 5 09:46 lost+found

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:03 testinguser

alaa@aa-lu:~$ ls -l /home/testinguser/

total 0

alaa@aa-lu:~$

您可以通过检查/etc/passwd中的条目来检查用户的主目录是否已正确设置。默认情况下,您应该将主目录设置为/home/testinguser:

alaa@aa-lu:~$ grep testinguser /etc/passwd

testinguser:x:1001:1001::/home/testinguser:/bin/sh

如果您在那里看不到主目录/home/testinguser,则需要执行命令sudo usermod -d /home/testinguser testinguser来更新它,但您不需要使用此命令,因为它应该被默认设置(根据useradd的联机帮助页)。

然后我退出了我的帐户,并使用testinguser重新登录,以下是自动创建的子目录:

ab5d638ae923e9b77604910cdccc993b.png

alaa@aa-lu:~$ ls -l /home/testinguser/

total 36

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Desktop

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Documents

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Downloads

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Music

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:07 Pictures

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Public

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Templates

drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Videos

我不需要复制/etc/skel的内容。

如果可能的话,您可以尝试按照以下步骤创建另一个新用户吗?完成后,您可以通过sudo deluser testinguser && sudo rm -r /home/testinguser删除该新用户。

如果所有这些都不适合你,那么我猜这是一个错误。

第三种解决方案

更新:解决方案已损坏,并且不适合我。

如果要创建用户的主目录(如果它不存在),请使用-m标志运行useradd命令。这将复制/etc/skel目录中的所有文件。

useradd -m username

您可能需要为您的系统配置设置。根据手册页:

-m, --create-home

Create the user's home directory if it does not exist. The files and directories

contained in the skeleton directory (which can be defined with the -k option)

will be copied to the home directory.

By default, if this option is not specified and CREATE_HOME is not enabled, no

home directories are created.

并进一步表明:

CONFIGURATION

The following configuration variables in /etc/login.defs change the behavior

of this tool:

CREATE_HOME (boolean)

Indicate if a home directory should be created by default for new users.

第四种方案

如果使用adduser命令创建新用户,则不需要为新用户创建主目录。将自动创建一个以/home目录中用户名称命名的主目录,其中包含以下子目录:桌面,下载,文档,音乐,图片,视频(以及其他)。

如果使用useradd命令创建新用户,则在终端中运行以下命令:

sudo -i #enter your user password when you are asked

gedit /etc/passwd

以root身份编辑/etc/passwd文件(这是最重要的步骤)。找到具有新用户名称的行,并为他设置默认主目录,如下所示:

new_username:x:1001:1001::/home/new_username:/bin/sh

在这一行中,您还可以为新用户添加一个真实姓名,或者设置默认shell。像这样的东西:

new_username:x:1001:1001:Real New Username,,,:/home/new_username:/bin/bash

小心,不要做任何其他更改。

在保存文件之后,在从root帐户出去之前,运行以下命令:

mkdir /home/new_username #to create the directory /home/new_username

cp /etc/skel/* /home/new_username #to copy copy files from the /home/new_username

chown -R new_username:new_username /home/new_username #to change the owner of /home/new_username to the new user

在所有这些之后,新用户的主文件夹将在首次登录后自动填充。

第五种方案

你需要的所有东西都是使其工作的主文件夹。作为根,

cd /home

cd mkdir username

chown username username

chgrp username username

第一次登录应该创建所有必要的文件,这似乎是那些:

drwxr-xr-x 16 fotanus fotanus 4096 Jun 4 17:46 .

drwxr-xr-x 9 root root 4096 Jun 4 17:43 ..

drwx------ 11 fotanus fotanus 4096 Jun 4 17:46 .cache

drwx------ 13 fotanus fotanus 4096 Jun 4 17:46 .config

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Desktop

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Documents

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Downloads

drwx------ 3 fotanus fotanus 4096 Jun 4 17:46 .gconf

drwxrwxr-x 2 fotanus fotanus 4096 Jun 4 17:46 .gstreamer-0.10

-rw------- 1 fotanus fotanus 334 Jun 4 17:45 .ICEauthority

drwxr-xr-x 3 fotanus fotanus 4096 Jun 4 17:45 .local

drwx------ 4 fotanus fotanus 4096 Jun 4 17:45 .mozilla

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Music

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Pictures

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Public

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Templates

drwxr-xr-x 2 fotanus fotanus 4096 Jun 4 17:45 Videos

-rw------- 1 fotanus fotanus 55 Jun 4 17:45 .Xauthority

-rw------- 1 fotanus fotanus 711 Jun 4 17:45 .xsession-errors

参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值