The useradd
command in Linux is used to create a new user account. When combined with the --btrfs-subvolume-home
option, it enables the creation of user home directories as BTRFS subvolumes. BTRFS (B-tree file system) is a modern filesystem with features like snapshots, subvolumes, and integrated RAID support, making it suitable for managing home directories efficiently.
Here are several examples demonstrating the usage of useradd
with the --btrfs-subvolume-home
option:
Example 1: Create a user with a BTRFS subvolume home directory.
sudo useradd --btrfs-subvolume-home john
This command creates a new user account named “john” and sets up its home directory as a BTRFS subvolume.
Verification Steps: After executing the command, verify by listing BTRFS subvolumes:
sudo btrfs subvolume list /home
Output should include a subvolume for the user “john”.
Example 2: Create a user with a specific BTRFS subvolume name.
sudo useradd --btrfs-subvolume-home --btrfs-subvolume-name=myhome jane
Here, a user “jane” is created with a custom-named BTRFS subvolume “myhome” for her home directory.
Verification Steps: Check the subvolume name:
sudo btrfs subvolume list /home
The output should show a subvolume named “myhome”.
Example 3: Specify additional options like user groups and comment.
sudo useradd --btrfs-subvolume-home --groups developers --comment "Developer Account" mike
This command creates a user “mike” belonging to the “developers” group, with a comment indicating its purpose.
Verification Steps: Ensure user details are correctly set:
id mike
Verify the groups and comments associated with the user.
Example 4: Set a specific shell for the user.
sudo useradd --btrfs-subvolume-home --shell /bin/bash sarah
Here, user “sarah” is created with the Bash shell.
Verification Steps: Check the user’s shell:
getent passwd sarah
Ensure the shell field (“/bin/bash”) is correctly set.
Example 5: Create a user with an expiration date.
sudo useradd --btrfs-subvolume-home --expiredate 2025-12-31 anna
This command creates a user “anna” whose account expires on December 31, 2025.
Verification Steps: Verify expiration date:
sudo chage -l anna
Check the “Account expires” field to ensure it reflects the set date.
Example 6: Assign a UID for the new user explicitly.
sudo useradd --btrfs-subvolume-home --uid 2001 tim
This command creates a user “tim” with a specific UID (2001).
Verification Steps: Verify UID assignment:
id tim
Check that the UID is correctly assigned to user “tim”.
Example 7: Create a user with a specified home directory path.
sudo useradd --btrfs-subvolume-home --home /mnt/data/users/emma emma
Here, user “emma” is created with a home directory located at /mnt/data/users/emma
.
Verification Steps: Confirm the home directory path:
getent passwd emma
Check that the home directory field (“/mnt/data/users/emma”) is set correctly.
Example 8: Create a user with a specific default umask.
sudo useradd --btrfs-subvolume-home --create-home --user-group --umask 007 lucas
This command creates a user “lucas” with a default umask of 007, ensuring specific file permission settings.
Verification Steps: Verify umask configuration:
sudo su - lucas
Create a file to verify its permissions align with the umask set (e.g., touch testfile
).
Example 9: Set a password for the new user upon creation.
sudo useradd --btrfs-subvolume-home --password $(openssl passwd -1 mypassword) max
Here, user “max” is created with the password “mypassword”.
Verification Steps: Attempt to log in as “max” using the set password to confirm login functionality.
Example 10: Create a system user without a home directory.
sudo useradd --btrfs-subvolume-home --system --no-create-home --shell /sbin/nologin service
This command creates a system user “service” without a home directory and with a restricted login shell.
Verification Steps: Check user details:
getent passwd service
Ensure the user “service” exists and has no home directory.
Discussion about this post