The useradd
command in Linux is used to create new user accounts. One of the options available with this command is -b, --base-dir BASE_DIR
. This option allows you to specify a base directory where new user home directories will be created.
Here are some examples demonstrating the usage of useradd
with the -b
option:
Example 1: Creating a user with a specified base directory:
useradd -b /home/customusers johndoe
This command creates a new user account named johndoe
with the home directory located at /home/customusers/johndoe
.
To verify: Use grep johndoe /etc/passwd
to check if the user johndoe
exists and see the home directory specified.
Example 2: Specifying a different base directory:
useradd --base-dir /data/users janedoe
Here, the command creates a user janedoe
with the home directory set to /data/users/janedoe
.
To verify: Check with grep janedoe /etc/passwd
and confirm the specified home directory.
Example 3: Using a relative path for the base directory:
useradd -b users newuser
This command creates newuser
with the home directory set to /home/users/newuser
, assuming /home
is the default base directory.
To verify: Verify with grep newuser /etc/passwd
to see the home directory path.
Example 4: Creating a user with an absolute path:
useradd --base-dir /mnt/storage/files userservice
This will create a user userservice
with the home directory /mnt/storage/files/userservice
.
To verify: Check the output of grep userservice /etc/passwd
for the specified home directory.
Example 5: Using a different directory structure:
useradd -b /srv/clients/client1/employees employee1
Here, the user employee1
will have a home directory located at /srv/clients/client1/employees/employee1
.
To verify: Ensure the entry for employee1
is present in /etc/passwd
with the expected home directory.
Example 6: Creating a user with a customized directory:
useradd --base-dir /var/custom/home customuser
This command creates customuser
with the home directory /var/custom/home/customuser
.
To verify: Check grep customuser /etc/passwd
to confirm the home directory path.
Example 7: Using a network share as the base directory:
useradd -b /mnt/nfs/users nfsuser
Creates a user nfsuser
with the home directory /mnt/nfs/users/nfsuser
.
To verify: Verify grep nfsuser /etc/passwd
for the specified home directory path.
Example 8: Creating a user with a nested structure:
useradd --base-dir /home/domain1/users/client1 clientuser
This command sets up clientuser
with the home directory /home/domain1/users/client1/clientuser
.
To verify: Check grep clientuser /etc/passwd
for the home directory information.
Example 9: Assigning a user to a specific system directory:
useradd -b /opt/application/users appuser
This creates a user appuser
with the home directory /opt/application/users/appuser
.
To verify: Use grep appuser /etc/passwd
to see the user details and home directory path.
Example 10: Creating a user in a different partition:
useradd --base-dir /media/storage/users storageuser
This command results in storageuser
having the home directory /media/storage/users/storageuser
.
To verify: Check grep storageuser /etc/passwd
to confirm the home directory path.
Discussion about this post