The useradd
command in Linux is used to create new user accounts. One of its options, -P
or --prefix PREFIX_DIR
, allows setting a prefix directory where user home directories will be created. This is particularly useful when you want to organize user home directories under a specific directory structure.
Here are several examples demonstrating the usage of -P
option with useradd
:
Example 1: Setting the prefix directory to /home/users
:
useradd -P /home/users john
This command creates a new user john
and sets their home directory to /home/users/john
.
Example 2: Using an existing directory path:
useradd -P /srv/users alice
This command creates a user alice
with the home directory at /srv/users/alice
.
Example 3: Specifying a relative path:
useradd -P relative/path bob
Here, the command creates a user bob
with the home directory at relative/path/bob
.
Example 4: Using an environment variable:
useradd -P $HOME/users charlie
This command creates a user charlie
with the home directory at $HOME/users/charlie
.
Example 5: Combining options with user creation:
useradd -m -P /var/lib/users dave
Here, -m
ensures creation of the user’s home directory and -P
sets it to /var/lib/users/dave
.
Verification: To verify if the user creation with the specified prefix directory was successful, you can check the existence of the user’s home directory using:
ls -ld /path/to/prefix/directory/username
Replace /path/to/prefix/directory/username
with the actual path where the user’s home directory should have been created.
Discussion about this post