• 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

Setting Expiry Dates for User Accounts in Linux

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

The useradd command in Linux is used to create new user accounts. One of its options, -e or --expiredate, allows setting an expiry date for the user account, after which the account becomes inactive.

Setting Expiry Dates for User Accounts in Linux: The option -e or --expiredate with useradd specifies the date when the user account should expire. This is particularly useful in environments where temporary access is needed or for enforcing regular password changes.

Here are a few examples demonstrating how to use this option:

Example 1: Setting an expiry date for a user account named testuser1 to expire on July 1, 2024.

    $ sudo useradd -e 2024-07-01 testuser1
    

This command creates a new user account testuser1 and sets its expiry date to July 1, 2024.

Verification: To verify, you can check the user’s account details using commands like grep testuser1 /etc/passwd or sudo chage -l testuser1 to see the expiry date.

Example 2: Creating a user tempuser with an expiry date set to 30 days from now.

    $ sudo useradd -e $(date -d "+30 days" +%Y-%m-%d) tempuser
    

This dynamically calculates the expiry date based on the current date plus 30 days.

Verification: Use the same verification commands as above to confirm the expiry date.

Example 3: Setting an expiry date for a user account using a relative date format.

    $ sudo useradd -e +90 testuser2
    

This sets the expiry date of testuser2 to 90 days from today.

Verification: Check the expiry date using the methods mentioned earlier.

Example 4: Creating a user with no expiry date by setting the expiry to never.

    $ sudo useradd -e never testuser3
    

This command ensures that testuser3 will not have an expiry date.

Verification: Verify by checking the user’s details to confirm no expiry is set.

Example 5: Using a specific time along with the date for expiry.

    $ sudo useradd -e "2024-12-31 12:00:00" testuser4
    

This sets a precise expiry date and time for testuser4.

Verification: Check using the verification commands to ensure the exact expiry time is set.

Example 6: Setting an expiry date for a user account by specifying the number of days since the epoch.

    $ sudo useradd -e $(date -d "2030-01-01" +%s) testuser5
    

This example sets the expiry date of testuser5 using the Unix epoch time format.

Verification: Confirm by checking the expiry date in the user’s details.

Example 7: Setting an expiry date for a user account using a YYYYMMDD format.

    $ sudo useradd -e 20251231 testuser6
    

This sets the expiry date of testuser6 to December 31, 2025.

Verification: Use the verification commands to check if the date is correctly set.

Example 8: Setting an expiry date for a user account to expire at the end of the current month.

    $ sudo useradd -e $(date -d "$(date +%Y-%m-01) +1 month -1 day" +%Y-%m-%d) testuser7
    

This command calculates the last day of the current month as the expiry date for testuser7.

Verification: Verify the expiry date using the methods mentioned earlier.

Example 9: Setting an expiry date for a user account in a script or automation.

    #!/bin/bash
    expiry_date=$(date -d "+60 days" +%Y-%m-%d)
    sudo useradd -e $expiry_date automateduser
    

This script creates a user automateduser with an expiry date 60 days from now.

Verification: Ensure to verify the expiry date after running the script.

Example 10: Using the --expiredate option with additional user creation options.

    $ sudo useradd -m -e 2023-06-30 -c "Temporary User" tempuser2
    

This creates a user tempuser2 with a home directory (-m), a specified expiry date, and a comment (-c).

Verification: Confirm all attributes including the expiry date using the verification methods.

Tags: CommandsLinuxLinux Command TutorialLinux Commands
Previous Post

Managing Default User Creation Settings in Linux

Next Post

Configuring Password Inactivity Periods for Users in Linux

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

Configuring Password Inactivity Periods for Users in Linux

howto

Assigning Primary Group for New User Accounts in Linux

howto

Adding Supplementary Groups to User Accounts in Linux

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.