DEV Community

Anusha Kuppili
Anusha Kuppili

Posted on

๐Ÿ• How to Create a Temporary Linux User with an Expiry Date (Real DevOps Task)

Ever needed to give someone temporary access to a Linux systemโ€”like a developer, intern, or contractorโ€”but donโ€™t want to manually remember to revoke it?

Let me show you how to do that using a real-world DevOps-style task.

Todayโ€™s goal is simple:

โœ… Create a user named ammar with access only until 15th April 2024.

Letโ€™s jump into the steps. ๐Ÿ› ๏ธ


โœ… Step 1: Create the User with an Expiry Date

Run the following command in your terminal:

sudo useradd -e 2024-04-15 ammar
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” What this does:

  • useradd โ€“ Command to create a new user
  • -e โ€“ Sets the expiry date for the account (format: YYYY-MM-DD)
  • ammar โ€“ The username being created

This ensures the system will automatically disable this account after April 15, 2024. ๐ŸŽฏ


๐Ÿ” Step 2: Set a Password for the User

The user cannot log in until a password is assigned. Use this command:

sudo passwd ammar
Enter fullscreen mode Exit fullscreen mode

Youโ€™ll be prompted to enter and confirm the password.

Make sure to securely share it with the user.


๐Ÿ“… Step 3: Verify the Expiry Date

To confirm the user was created with the correct expiry date:

sudo chage -l ammar
Enter fullscreen mode Exit fullscreen mode

โœ… Sample output:

Account expires    : Apr 15, 2024
Password expires   : never
Password inactive  : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Enter fullscreen mode Exit fullscreen mode

Look for the line:

Account expires    : Apr 15, 2024
Enter fullscreen mode Exit fullscreen mode

That confirms everythingโ€™s set up correctly!


๐Ÿ” (Optional) Step 4: Modify the Expiry Date Later

Need to extend or update the access duration?

sudo usermod -e 2024-05-15 ammar
Enter fullscreen mode Exit fullscreen mode

Just change the date to whatever is appropriate.


๐Ÿง˜ Why Should You Use Expiry Dates?

Setting expiry dates helps you:

  • ๐Ÿ”’ Avoid lingering, forgotten accounts
  • ๐Ÿงน Keep your system clean and secure
  • โœ… Follow enterprise security standards
  • โฐ Automate temporary access control

This is especially important in shared or cloud-based environments.


๐Ÿงช Command Recap

Hereโ€™s a quick summary of everything we did:

# Step 1 - Create user with expiry
sudo useradd -e 2024-04-15 ammar

# Step 2 - Set password
sudo passwd ammar

# Step 3 - Verify expiry
sudo chage -l ammar

# (Optional) Step 4 - Modify expiry
sudo usermod -e 2024-05-15 ammar
Enter fullscreen mode Exit fullscreen mode

โค๏ธ Final Thoughts

Creating temporary users in Linux is one of those small tasks that has huge security impact.

In just a few commands, you can:

  • โœ… Create a user
  • โœ… Set an automatic expiry
  • โœ… Keep your infrastructure secure and tidy

๐Ÿ“บ Want to watch this in action? Head over to my YouTube channel Data Enthusiast Era for more DevOps tutorials, real-world tasks, and beginner-friendly breakdowns! ๐ŸŽฅ

Until next time โ€” stay curious, stay secure. ๐Ÿ”โœจ

Top comments (0)