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.
Discussion about this post