The useradd
command in Linux is used to create new user accounts. One of its useful options is -m
or --create-home
, which automatically creates a home directory for the new user. This simplifies the process of setting up new accounts by ensuring that essential user directories are in place upon creation.
Let’s explore this option with a few examples:
Example 1: Create a new user ‘john’ with a home directory:
useradd -m john
This command adds a new user ‘john’ and creates a home directory /home/john
. To verify, you can check the existence of the directory using:
ls /home/john
Output (if successful):
Desktop Documents Downloads Music Pictures Videos
Example 2: Create a user ‘mary’ with a custom home directory path:
useradd -m -d /data/users/mary mary
This command creates a user ‘mary’ with a home directory specified as /data/users/mary
. To verify, check the specified directory:
ls /data/users/mary
Output (if successful):
(contents of /data/users/mary)
Example 3: Add a user ‘jane’ with an encrypted home directory:
useradd -m -p '*' jane
This command creates the user ‘jane’ and initializes an encrypted home directory. Verification involves checking the encrypted home directory setup.
Example 4: Create a user ‘test’ with additional group assignments:
useradd -m -G developers,testers test
This adds user ‘test’ with primary group ‘developers’ and secondary group ‘testers’. To verify, check the group memberships of ‘test’:
groups test
Output (if successful):
test : developers testers
Example 5: Specify a custom shell for user ‘admin’ upon creation:
useradd -m -s /bin/bash admin
This sets the shell for ‘admin’ to /bin/bash
. Verification involves checking the shell assignment:
grep admin /etc/passwd
Output (if successful):
admin:x:1001:1001::/home/admin:/bin/bash
Example 6: Create a user ‘backup’ with a specific UID:
useradd -m -u 2000 backup
This assigns the UID ‘2000’ to the user ‘backup’. Verification involves checking the UID assignment:
id backup
Output (if successful):
uid=2000(backup) gid=2000(backup) groups=2000(backup)
Example 7: Create a system user ‘daemon’ without a home directory:
useradd -r daemon
This creates a system user ‘daemon’ with no home directory. Verification involves checking for the absence of a home directory:
ls /home/daemon
Output (if successful):
ls: cannot access '/home/daemon': No such file or directory
Example 8: Create a user ‘ftp’ with a specific comment (GECOS field):
useradd -m -c "FTP User" ftp
This adds user ‘ftp’ with the comment “FTP User”. Verification involves checking the user’s details:
finger ftp
Output (if successful):
Login: ftp Name: FTP User Directory: /home/ftp Shell: /bin/bash
Example 9: Create a user ‘guest’ and specify an expiration date:
useradd -m -e 2025-12-31 guest
This creates user ‘guest’ with an account expiration date of December 31, 2025. Verification involves checking the account expiration status:
chage -l guest
Output (if successful):
Last password change : Jun 21, 2024 Password expires : never Password inactive : never Account expires : Dec 31, 2025 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
Example 10: Create a user ‘sales’ and assign a custom skeleton directory:
useradd -m -k /etc/skel_sales sales
This command adds user ‘sales’ and copies the contents of /etc/skel_sales
to the new user’s home directory. Verification involves checking the contents of ‘sales’ home directory:
ls /home/sales
Output (if successful):
(contents of /home/sales)
Discussion about this post