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
๐ 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
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
โ 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
Look for the line:
Account expires : Apr 15, 2024
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
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
โค๏ธ 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)