• About Us
  • Privacy & Policy
HowTo's
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
HowTo's
No Result
View All Result
Home Commands

Creating Home Directories Automatically with Useradd Command

June 21, 2024
in Commands, Linux, Linux Command Tutorial, Linux Commands
A A
0
12
SHARES
105
VIEWS
Share on FacebookShare on Twitter

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)
    
Tags: CommandsLinuxLinux Command TutorialLinux Commands
Previous Post

Managing User Database Entries with Useradd Command

Next Post

Preventing Home Directory Creation with Useradd Command

Related You may like!

howto

How to Use -iname for Case-Insensitive Filename Searches in find

August 21, 2024
howto

Search for Files with Case-Insensitive Pattern Matching Using -ilname in find

August 21, 2024

Find Files by Group Name with -group in find Command

August 21, 2024

Locate Files by Group ID Using -gid in find Command

August 21, 2024

How to Search for Filesystems with -fstype in find Command

August 21, 2024

Use -false to Test Conditional Expressions in find Command

August 21, 2024
Next Post
howto

Preventing Home Directory Creation with Useradd Command

howto

Managing User and Group Creation with Useradd Command

howto

Allowing Non-Unique User IDs with Useradd Command

Discussion about this post

Latest Updated

howto

How to Use -iname for Case-Insensitive Filename Searches in find

August 21, 2024
howto

Search for Files with Case-Insensitive Pattern Matching Using -ilname in find

August 21, 2024
howto

Find Files by Group Name with -group in find Command

August 21, 2024
howto

Locate Files by Group ID Using -gid in find Command

August 21, 2024
howto

How to Search for Filesystems with -fstype in find Command

August 21, 2024

Trending in Week

  • howto

    Using BTRFS Subvolume for User Home Directory in Linux

    22 shares
    Share 9 Tweet 6
  • Downloading Docker Images from a Registry

    13 shares
    Share 5 Tweet 3
  • Configuring SSL Connection Mode in mysqldump

    17 shares
    Share 7 Tweet 4
  • Omit Tablespace Information in mysqldump Output

    13 shares
    Share 5 Tweet 3
  • Setting MySQL Dump Compatibility Mode

    18 shares
    Share 7 Tweet 5
  • Setting Network Buffer Length in mysqldump

    13 shares
    Share 5 Tweet 3
  • Logging out from Docker Registries

    13 shares
    Share 5 Tweet 3
  • Scheduling Nodes in Kubernetes with kubectl uncordon

    12 shares
    Share 5 Tweet 3
  • Managing Default User Creation Settings in Linux

    15 shares
    Share 6 Tweet 4
  • Using Extended INSERT Syntax in mysqldump

    12 shares
    Share 5 Tweet 3
  • About Us
  • Privacy & Policy

© 2024 All Rights Reserved. Howto.swebtools.com.

No Result
View All Result

© 2024 All Rights Reserved. Howto.swebtools.com.