The useradd
command in Linux is used to create a new user account. One of its options, -k
or --skel
, allows you to specify a custom skeleton directory (SKEL_DIR
) to use as a template for creating the new user’s home directory and initial configuration files.
Using custom skeleton directories with useradd
is particularly useful when you want to predefine certain configurations or provide specific files to new user accounts automatically. Here are several examples of how you can utilize this feature:
Example 1: Creating a new user with a custom skeleton directory:
sudo useradd -m -k /etc/skel_custom john_doe
This command creates a new user account named john_doe
and copies the contents of the /etc/skel_custom
directory to the new user’s home directory. To verify, check if /home/john_doe
contains files from /etc/skel_custom
.
Example 2: Creating a user with a specific default shell and custom skeleton:
sudo useradd -m -s /bin/bash -k /etc/skel_custom2 jane_smith
Here, the command creates a user jane_smith
with /bin/bash
as the default shell and populates her home directory with files from /etc/skel_custom2
.
Example 3: Adding a system user with a custom skeleton directory:
sudo useradd -r -k /etc/skel_service service_user
This example creates a system user service_user
(with a UID less than 1000, typically used for system services) and sets up the home directory using files from /etc/skel_service
.
Example 4: Creating a user without a home directory but with custom skeleton files:
sudo useradd -r -M -k /etc/skel_no_home app_user
Here, app_user
is created as a system user without a home directory (-M
), but with custom files from /etc/skel_no_home
.
Example 5: Specifying a custom skeleton directory with additional options:
sudo useradd -m -k /etc/skel_custom3 -c "Custom Skeleton Example" tom_jones
This command creates a new user tom_jones
with a comment and utilizes /etc/skel_custom3
for the initial home directory structure.
Example 6: Adding a user with a custom skeleton and specifying a different primary group:
sudo useradd -g developers -k /etc/skel_developer bill_gates
This command creates the user bill_gates
with the primary group set to developers
and uses /etc/skel_developer
for initial configuration.
Example 7: Creating a user with custom skeleton files and setting an expiration date:
sudo useradd -m -k /etc/skel_custom4 -e 2025-12-31 amy_wong
The -e
option sets an expiration date for the user amy_wong
, while /etc/skel_custom4
provides the initial files for her home directory.
Example 8: Creating a new user with a custom skeleton directory and a specific UID:
sudo useradd -m -k /etc/skel_custom5 -u 2001 steve_jobs
This command assigns the UID 2001 to the user steve_jobs
and uses /etc/skel_custom5
for the initial setup.
Example 9: Creating a user with a custom skeleton and specifying a different default umask:
sudo useradd -m -k /etc/skel_custom6 -d /home/other_home -o -U other_user
Here, other_user
is created with a different home directory path and custom skeleton files from /etc/skel_custom6
.
Example 10: Adding a user with a custom skeleton and setting a specific login class:
sudo useradd -m -k /etc/skel_custom7 -l special_login linda_jackson
This command assigns the login class special_login
to linda_jackson
and uses /etc/skel_custom7
for initial configurations.
Discussion about this post