Centos – useradd: cannot create directory

centoshomeuseradd

A CentOS 7 server needs to have a new user created with a specific home directory and shell defined as follows, taken from the instructions at this link:

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

However, when that command is run on a CentOS 7 server, the command fails with the following error:

useradd: cannot create directory /opt/atlassian/bitbucket

Similarly, creating the /opt/atlassian/bitbucket directory before-hand results in the following error:

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

What specific changes need to be made to these commands, so that the new atlbitbucket user can successfully be created?


The Complete Terminal Output:


The following is the complete series of commands and responses in the CentOS 7 terminal:

Manually Creating The Directories:

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/

The Recommended useradd Syntax:

[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 Instead Of useradd

I then tried @terdon's suggestion from this other posting to use adduser instead, but got the same error, as follows:

[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 ~]$

Shorter Syntax:

Then I tried @rajcoumar's suggestion from the same other posting, but got the same following results:

[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 ~]$

Elevating To root:

I even upgraded to root just to see if the problem could be resolved by running the command as root, but I still got the following error:

[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 ~]#

Best Answer

The useradd code calls a mkdir library function to (attempt to) create the specified directory. useradd checks the return code, but only for being non-zero; in this case, I suspect that mkdir is returning ENOENT -- A directory component in pathname does not exist or is a dangling symbolic link because the parent directory (/opt/atlassian) didn't exist, or had been removed during your attempts to add the user.

As Kusalananda / roaima point out, the simplest solution here is to create the parent directory structure before calling useradd:

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