Centos

useradd:無法創建目錄

  • May 16, 2017

CentOS 7 伺服器需要創建一個具有特定主目錄和 shell 的新使用者,定義如下,取自此連結的說明

sudo /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket

但是,當該命令在 CentOS 7 伺服器上執行時,該命令會失敗並出現以下錯誤:

useradd: cannot create directory /opt/atlassian/bitbucket

同樣,預先創建/opt/atlassian/bitbucket目錄會導致以下錯誤:

useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.

需要對這些命令進行哪些具體更改,atlbitbucket才能成功創建新使用者?


完整的終端輸出:


以下是 CentOS 7 終端中完整的一系列命令和響應:

手動創建目錄:

login as: my_sudoer_user
my_sudoer_user@private.lan.ip.addr's password:
Last login: Mon May 15 14:00:18 2017
[my_sudoer_user@localhost ~]$ sudo mkdir /opt/atlassian/
[sudo] password for my_sudoer_user:
[my_sudoer_user@localhost ~]$ sudo mkdir /opt/atlassian/bitbucket
[my_sudoer_user@localhost ~]$ sudo /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.

[my_sudoer_user@localhost ~]$ sudo rmdir /opt/atlassian/bitbucket
[my_sudoer_user@localhost ~]$ sudo rmdir /opt/atlassian/

推薦的useradd語法:

[my_sudoer_user@localhost ~]$ sudo /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket
useradd: user 'atlbitbucket' already exists

[my_sudoer_user@localhost ~]$ sudo userdel -r atlbitbucket
userdel: atlbitbucket home directory (/opt/atlassian/bitbucket) not found

[my_sudoer_user@localhost ~]$ sudo /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket
useradd: cannot create directory /opt/atlassian/bitbucket
[my_sudoer_user@localhost ~]$

adduser代替useradd

然後我嘗試使用其他文章中的@terdon 建議adduser來代替,但得到了同樣的錯誤,如下所示:

[my_sudoer_user@localhost ~]$ sudo userdel -r atlbitbucket
[sudo] password for my_sudoer_user:
userdel: atlbitbucket mail spool (/var/spool/mail/atlbitbucket) not found
userdel: atlbitbucket home directory (/opt/atlassian/bitbucket) not found
[my_sudoer_user@localhost ~]$ sudo adduser --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket
adduser: cannot create directory /opt/atlassian/bitbucket
[my_sudoer_user@localhost ~]$

更短的語法:

然後我從同一個文章中嘗試了@rajcoumar 的建議,但得到了相同的以下結果:

[my_sudoer_user@localhost ~]$ sudo userdel -r atlbitbucket
userdel: atlbitbucket mail spool (/var/spool/mail/atlbitbucket) not found
userdel: atlbitbucket home directory (/opt/atlassian/bitbucket) not found
[my_sudoer_user@localhost ~]$ sudo useradd -m -d /opt/atlassian/bitbucket -s /bin/bash atlbitbucket
useradd: cannot create directory /opt/atlassian/bitbucket
[my_sudoer_user@localhost ~]$

提升至root

我什至升級到root只是為了看看問題是否可以通過以 root 身份執行命令來解決,但我仍然收到以下錯誤:

[my_sudoer_user@localhost ~]$ su -
Password:
Last login: Mon May 15 14:07:11 PDT 2017 on ttyS0
[root@localhost ~]# /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket
useradd: cannot create directory /opt/atlassian/bitbucket
[root@localhost ~]#

useradd 程式碼呼叫mkdir庫函式來(嘗試)創建指定的目錄。useradd 檢查返回碼,但僅用於非零;在這種情況下,我懷疑 mkdir 正在返回ENOENT -- A directory component in pathname does not exist or is a dangling symbolic link,因為父目錄 (/opt/atlassian) 不存在,或者在您嘗試添加使用者時已被刪除。

正如 Kusalananda / roaima 指出的,這裡最簡單的解決方案是在呼叫 useradd 之前創建父目錄結構:

  1. sudo mkdir -p /opt/atlassian
  2. sudo /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket

引用自:https://unix.stackexchange.com/questions/365263