4

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 ~]#
  • 1
    I see a sudo rmdir /opt/atlassian/; try mkdir -p /opt/atlassian before running the useradd. – Jeff Schaller May 16 '17 at 0:56
  • @JeffSchaller The command sequence that worked was $ sudo mkdir -p /opt/atlassian, followed by $ sudo /usr/sbin/useradd --create-home --home-dir /opt/atlassian/bitbucket --shell /bin/bash atlbitbucket. If you are willing to explain why your solution worked, and write it up as an answer, I would be happy to mark it as accepted and +1. – CodeMed May 16 '17 at 1:31
  • I will have to do so tomorrow (circa +8 hours from now) – Jeff Schaller May 16 '17 at 1:34
2

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
  • You wrote a high quality answer. Thank you. From the links that you posted, I assume that the -p flag means that it is loading a password from a password file, perhaps to avoid SELINUX blocks. Is this true? I might want to frame a separate follow up question to better understand that. Your answer expands the reader's knowledge of Linux. – CodeMed May 16 '17 at 19:05
  • Thank you. No; -p just pre-fills the password (/etc/passwd or /etc/shadow) with the password you give it after -p. Otherwise it leaves the account disabled (! as the password). – Jeff Schaller May 16 '17 at 19:12
2

The commands you use to create the user and their home directory will only be able to create the home directory if the immediate parent of the home directory already exists. It will fail if it can't find the parent folder where the home directory should go, or if the home directory already exists.

This means that

$ mkdir -p /some/dir/myhome
$ useradd --home-dir /some/dir/myhome ...

will fail since /some/dir/myhome exists, whereas

$ rm -rf /some/dir
$ useradd --home-dir /some/dir/myhome ...

will fail as /some/dir does not exist.

Just make sure that the directory where in the home directory is to be created exists, i.e. in your situation,

mkdir -p /opt/atlassian

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.